123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import * as chai from 'chai';
- import * as estraverse from 'estraverse';
- import * as ESTree from 'estree';
- import { TNodeWithBlockStatement } from '../../../src/types/TNodeWithBlockStatement';
- import { IBlockScopeTraceData } from '../../../src/interfaces/IBlockScopeTraceData';
- import { ASTTreeBlockScopeAnalyzer } from '../../../src/hidden-node-appender/ASTTreeBlockScopeAnalyzer';
- import { Nodes } from '../../../src/Nodes';
- import { NodeUtils } from '../../../src/NodeUtils';
- const assert: any = chai.assert;
- /**
- * @param astTree
- * @param name
- * @returns {ESTree.FunctionDeclaration|null}
- */
- function getFunctionDeclarationByName (astTree: ESTree.Node, name: string): ESTree.FunctionDeclaration|null {
- let functionDeclarationNode: ESTree.FunctionDeclaration|null = null;
- estraverse.traverse(astTree, {
- enter: (node: ESTree.Node): any => {
- if (
- Nodes.isFunctionDeclarationNode(node) &&
- Nodes.isIdentifierNode(node.id) &&
- node.id.name === name
- ) {
- functionDeclarationNode = node;
- return estraverse.VisitorOption.Break;
- }
- }
- });
- return functionDeclarationNode;
- }
- /**
- * @param astTree
- * @param name
- * @returns {ESTree.FunctionExpression|null}
- */
- function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTree.FunctionExpression|null {
- let functionExpressionNode: ESTree.FunctionExpression|null = null;
- estraverse.traverse(astTree, {
- enter: (node: ESTree.Node): any => {
- if (
- Nodes.isVariableDeclaratorNode(node) &&
- node.init &&
- Nodes.isFunctionExpressionNode(node.init) &&
- Nodes.isIdentifierNode(node.id) &&
- node.id.name === name
- ) {
- functionExpressionNode = node.init;
- return estraverse.VisitorOption.Break;
- }
- }
- });
- return functionExpressionNode;
- }
- describe('ASTTreeBlockScopeAnalyzer', () => {
- describe('analyze (): IBlockScopeTraceData[]', () => {
- let ASTTree: TNodeWithBlockStatement,
- blockScopeTraceData: IBlockScopeTraceData[],
- expectedBlockScopeTraceData: IBlockScopeTraceData[];
- it('should returns correct BlockScopeTraceData - variant #1', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- baz();
- foo();
- bar();
- `, false);
- expectedBlockScopeTraceData = [
- {
- name: 'baz',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'baz')).body,
- trace: []
- },
- {
- name: 'foo',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'foo')).body,
- trace: []
- },
- {
- name: 'bar',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
- trace: [
- {
- name: 'inner2',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner2')).body,
- trace: [
- {
- name: 'inner3',
- callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(ASTTree, 'inner3')).body,
- trace: []
- },
- ]
- },
- {
- name: 'inner1',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner1')).body,
- trace: []
- },
- ]
- }
- ];
- blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #2', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- bar();
-
- function foo () {
-
- }
-
- function bar () {
-
- }
-
- function baz () {
- function inner1 () {
-
- }
-
- inner1();
- }
-
- baz();
- foo();
- `, false);
- expectedBlockScopeTraceData = [
- {
- name: 'bar',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
- trace: []
- },
- {
- name: 'baz',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'baz')).body,
- trace: [
- {
- name: 'inner1',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner1')).body,
- trace: []
- },
- ]
- },
- {
- name: 'foo',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'foo')).body,
- trace: []
- }
- ];
- blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #3', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- bar();
-
- function bar () {
-
- }
- `, false);
- expectedBlockScopeTraceData = [
- {
- name: 'bar',
- callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
- trace: []
- }
- ];
- blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #4', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function bar () {
-
- }
- `, false);
- expectedBlockScopeTraceData = [];
- blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- it('should returns correct BlockScopeTraceData - variant #5', () => {
- ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- bar();
- `, false);
- expectedBlockScopeTraceData = [];
- blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
- assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
- });
- });
- });
|