StackTraceAnalyzer-spec.ts 7.5 KB

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