StackTraceAnalyzer-spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. stackTraceData: IStackTraceData[],
  59. expectedStackTraceData: IStackTraceData[];
  60. it('should returns correct IStackTraceData - variant #1', () => {
  61. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  62. readFileAsString('./test/fixtures/stack-trace-analyzer/variant-1.js'),
  63. false
  64. );
  65. expectedStackTraceData = [
  66. {
  67. name: 'baz',
  68. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  69. stackTrace: []
  70. },
  71. {
  72. name: 'foo',
  73. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  74. stackTrace: []
  75. },
  76. {
  77. name: 'bar',
  78. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  79. stackTrace: [
  80. {
  81. name: 'inner2',
  82. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner2')).body,
  83. stackTrace: [
  84. {
  85. name: 'inner3',
  86. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'inner3')).body,
  87. stackTrace: []
  88. },
  89. ]
  90. },
  91. {
  92. name: 'inner1',
  93. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  94. stackTrace: []
  95. },
  96. ]
  97. }
  98. ];
  99. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  100. assert.deepEqual(stackTraceData, expectedStackTraceData);
  101. });
  102. it('should returns correct BlockScopeTraceData - variant #2', () => {
  103. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  104. readFileAsString('./test/fixtures/stack-trace-analyzer/variant-2.js'),
  105. false
  106. );
  107. expectedStackTraceData = [
  108. {
  109. name: 'bar',
  110. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  111. stackTrace: []
  112. },
  113. {
  114. name: 'baz',
  115. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  116. stackTrace: [
  117. {
  118. name: 'inner1',
  119. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  120. stackTrace: []
  121. },
  122. ]
  123. },
  124. {
  125. name: 'foo',
  126. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  127. stackTrace: []
  128. }
  129. ];
  130. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  131. assert.deepEqual(stackTraceData, expectedStackTraceData);
  132. });
  133. it('should returns correct BlockScopeTraceData - variant #3', () => {
  134. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  135. readFileAsString('./test/fixtures/stack-trace-analyzer/variant-3.js'),
  136. false
  137. );
  138. expectedStackTraceData = [
  139. {
  140. name: 'bar',
  141. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  142. stackTrace: []
  143. }
  144. ];
  145. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  146. assert.deepEqual(stackTraceData, expectedStackTraceData);
  147. });
  148. it('should returns correct BlockScopeTraceData - variant #4', () => {
  149. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  150. readFileAsString('./test/fixtures/stack-trace-analyzer/variant-4.js'),
  151. false
  152. );
  153. expectedStackTraceData = [];
  154. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  155. assert.deepEqual(stackTraceData, expectedStackTraceData);
  156. });
  157. it('should returns correct BlockScopeTraceData - variant #5', () => {
  158. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  159. readFileAsString('./test/fixtures/stack-trace-analyzer/variant-5.js'),
  160. false
  161. );
  162. expectedStackTraceData = [];
  163. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  164. assert.deepEqual(stackTraceData, expectedStackTraceData);
  165. });
  166. it('should returns correct BlockScopeTraceData - variant #6', () => {
  167. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  168. readFileAsString('./test/fixtures/stack-trace-analyzer/variant-6.js'),
  169. false
  170. );
  171. expectedStackTraceData = [];
  172. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  173. assert.deepEqual(stackTraceData, expectedStackTraceData);
  174. });
  175. });
  176. });