ASTTreeBlockScopeAnalyzer-spec.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import * as chai from 'chai';
  2. import * as estraverse from 'estraverse';
  3. import * as ESTree from 'estree';
  4. import { TNodeWithBlockStatement } from '../../../src/types/TNodeWithBlockStatement';
  5. import { IBlockScopeTraceData } from '../../../src/interfaces/IBlockScopeTraceData';
  6. import { ASTTreeBlockScopeAnalyzer } from '../../../src/hidden-node-appender/ASTTreeBlockScopeAnalyzer';
  7. import { Nodes } from '../../../src/Nodes';
  8. import { NodeUtils } from '../../../src/NodeUtils';
  9. const assert: any = chai.assert;
  10. /**
  11. * @param astTree
  12. * @param name
  13. * @returns {ESTree.FunctionDeclaration|null}
  14. */
  15. function getFunctionDeclarationByName (astTree: ESTree.Node, name: string): ESTree.FunctionDeclaration|null {
  16. let functionDeclarationNode: ESTree.FunctionDeclaration|null = null;
  17. estraverse.traverse(astTree, {
  18. enter: (node: ESTree.Node): any => {
  19. if (
  20. Nodes.isFunctionDeclarationNode(node) &&
  21. Nodes.isIdentifierNode(node.id) &&
  22. node.id.name === name
  23. ) {
  24. functionDeclarationNode = node;
  25. return estraverse.VisitorOption.Break;
  26. }
  27. }
  28. });
  29. return functionDeclarationNode;
  30. }
  31. /**
  32. * @param astTree
  33. * @param name
  34. * @returns {ESTree.FunctionExpression|null}
  35. */
  36. function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTree.FunctionExpression|null {
  37. let functionExpressionNode: ESTree.FunctionExpression|null = null;
  38. estraverse.traverse(astTree, {
  39. enter: (node: ESTree.Node): any => {
  40. if (
  41. Nodes.isVariableDeclaratorNode(node) &&
  42. node.init &&
  43. Nodes.isFunctionExpressionNode(node.init) &&
  44. Nodes.isIdentifierNode(node.id) &&
  45. node.id.name === name
  46. ) {
  47. functionExpressionNode = node.init;
  48. return estraverse.VisitorOption.Break;
  49. }
  50. }
  51. });
  52. return functionExpressionNode;
  53. }
  54. describe('ASTTreeBlockScopeAnalyzer', () => {
  55. describe('analyze (): IBlockScopeTraceData[]', () => {
  56. let ASTTree: TNodeWithBlockStatement,
  57. blockScopeTraceData: IBlockScopeTraceData[],
  58. expectedBlockScopeTraceData: IBlockScopeTraceData[];
  59. it('should returns correct BlockScopeTraceData - variant #1', () => {
  60. ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  61. function foo () {
  62. }
  63. function bar () {
  64. function inner1 () {
  65. }
  66. function inner2 () {
  67. var inner3 = function () {
  68. }
  69. inner3();
  70. }
  71. inner2();
  72. inner1();
  73. }
  74. function baz () {
  75. }
  76. baz();
  77. foo();
  78. bar();
  79. `, false);
  80. expectedBlockScopeTraceData = [
  81. {
  82. name: 'baz',
  83. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'baz')).body,
  84. trace: []
  85. },
  86. {
  87. name: 'foo',
  88. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'foo')).body,
  89. trace: []
  90. },
  91. {
  92. name: 'bar',
  93. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
  94. trace: [
  95. {
  96. name: 'inner2',
  97. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner2')).body,
  98. trace: [
  99. {
  100. name: 'inner3',
  101. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(ASTTree, 'inner3')).body,
  102. trace: []
  103. },
  104. ]
  105. },
  106. {
  107. name: 'inner1',
  108. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner1')).body,
  109. trace: []
  110. },
  111. ]
  112. }
  113. ];
  114. blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
  115. assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
  116. });
  117. it('should returns correct BlockScopeTraceData - variant #2', () => {
  118. ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  119. bar();
  120. function foo () {
  121. }
  122. function bar () {
  123. }
  124. function baz () {
  125. function inner1 () {
  126. }
  127. inner1();
  128. }
  129. baz();
  130. foo();
  131. `, false);
  132. expectedBlockScopeTraceData = [
  133. {
  134. name: 'bar',
  135. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
  136. trace: []
  137. },
  138. {
  139. name: 'baz',
  140. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'baz')).body,
  141. trace: [
  142. {
  143. name: 'inner1',
  144. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'inner1')).body,
  145. trace: []
  146. },
  147. ]
  148. },
  149. {
  150. name: 'foo',
  151. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'foo')).body,
  152. trace: []
  153. }
  154. ];
  155. blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
  156. assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
  157. });
  158. it('should returns correct BlockScopeTraceData - variant #3', () => {
  159. ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  160. bar();
  161. function bar () {
  162. }
  163. `, false);
  164. expectedBlockScopeTraceData = [
  165. {
  166. name: 'bar',
  167. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(ASTTree, 'bar')).body,
  168. trace: []
  169. }
  170. ];
  171. blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
  172. assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
  173. });
  174. it('should returns correct BlockScopeTraceData - variant #4', () => {
  175. ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  176. function bar () {
  177. }
  178. `, false);
  179. expectedBlockScopeTraceData = [];
  180. blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
  181. assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
  182. });
  183. it('should returns correct BlockScopeTraceData - variant #5', () => {
  184. ASTTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  185. bar();
  186. `, false);
  187. expectedBlockScopeTraceData = [];
  188. blockScopeTraceData = new ASTTreeBlockScopeAnalyzer<IBlockScopeTraceData>(ASTTree.body).analyze();
  189. assert.deepEqual(blockScopeTraceData, expectedBlockScopeTraceData);
  190. });
  191. });
  192. });