StackTraceAnalyzer.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/stack-trace-analyzer/IStackTraceData';
  6. import { readFileAsString } from '../../helpers/readFileAsString';
  7. import { Nodes } from '../../../src/Nodes';
  8. import { NodeUtils } from '../../../src/NodeUtils';
  9. import { StackTraceAnalyzer } from '../../../src/stack-trace-analyzer/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. /**
  56. * @param astTree
  57. * @param id
  58. * @returns {ESTree.FunctionExpression|null}
  59. */
  60. function getFunctionExpressionById (astTree: ESTree.Node, id: string): ESTree.FunctionExpression|null {
  61. let functionExpressionNode: ESTree.FunctionExpression|null = null;
  62. estraverse.traverse(astTree, {
  63. enter: (node: ESTree.Node): any => {
  64. if (
  65. Nodes.isFunctionExpressionNode(node) &&
  66. node.id &&
  67. Nodes.isIdentifierNode(node.id) &&
  68. node.id.name === id
  69. ) {
  70. functionExpressionNode = node;
  71. return estraverse.VisitorOption.Break;
  72. }
  73. }
  74. });
  75. return functionExpressionNode;
  76. }
  77. describe('StackTraceAnalyzer', () => {
  78. describe('extract (): IStackTraceData[]', () => {
  79. let astTree: TNodeWithBlockStatement,
  80. stackTraceData: IStackTraceData[],
  81. expectedStackTraceData: IStackTraceData[];
  82. it('should returns correct IStackTraceData - variant #1: basic-1', () => {
  83. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  84. readFileAsString('./test/fixtures/stack-trace-analyzer/basic-1.js'),
  85. false
  86. );
  87. expectedStackTraceData = [
  88. {
  89. name: 'baz',
  90. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  91. stackTrace: []
  92. },
  93. {
  94. name: 'foo',
  95. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  96. stackTrace: []
  97. },
  98. {
  99. name: 'bar',
  100. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  101. stackTrace: [
  102. {
  103. name: 'inner2',
  104. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner2')).body,
  105. stackTrace: [
  106. {
  107. name: 'inner3',
  108. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'inner3')).body,
  109. stackTrace: []
  110. },
  111. ]
  112. },
  113. {
  114. name: 'inner1',
  115. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  116. stackTrace: []
  117. },
  118. ]
  119. }
  120. ];
  121. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  122. assert.deepEqual(stackTraceData, expectedStackTraceData);
  123. });
  124. it('should returns correct BlockScopeTraceData - variant #2: basic-2', () => {
  125. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  126. readFileAsString('./test/fixtures/stack-trace-analyzer/basic-2.js'),
  127. false
  128. );
  129. expectedStackTraceData = [
  130. {
  131. name: 'bar',
  132. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  133. stackTrace: []
  134. },
  135. {
  136. name: 'baz',
  137. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  138. stackTrace: [
  139. {
  140. name: 'inner1',
  141. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  142. stackTrace: []
  143. },
  144. ]
  145. },
  146. {
  147. name: 'foo',
  148. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  149. stackTrace: []
  150. }
  151. ];
  152. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  153. assert.deepEqual(stackTraceData, expectedStackTraceData);
  154. });
  155. it('should returns correct BlockScopeTraceData - variant #3: deep conditions nesting', () => {
  156. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  157. readFileAsString('./test/fixtures/stack-trace-analyzer/deep-conditions-nesting.js'),
  158. false
  159. );
  160. expectedStackTraceData = [
  161. {
  162. name: 'bar',
  163. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  164. stackTrace: []
  165. },
  166. {
  167. name: 'baz',
  168. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  169. stackTrace: [
  170. {
  171. name: 'inner1',
  172. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  173. stackTrace: []
  174. },
  175. ]
  176. },
  177. {
  178. name: 'foo',
  179. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  180. stackTrace: []
  181. }
  182. ];
  183. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  184. assert.deepEqual(stackTraceData, expectedStackTraceData);
  185. });
  186. it('should returns correct BlockScopeTraceData - variant #4: call before declaration', () => {
  187. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  188. readFileAsString('./test/fixtures/stack-trace-analyzer/call-before-declaration.js'),
  189. false
  190. );
  191. expectedStackTraceData = [
  192. {
  193. name: 'bar',
  194. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  195. stackTrace: []
  196. }
  197. ];
  198. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  199. assert.deepEqual(stackTraceData, expectedStackTraceData);
  200. });
  201. it('should returns correct BlockScopeTraceData - variant #5: call expression of object member', () => {
  202. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  203. readFileAsString('./test/fixtures/stack-trace-analyzer/call-expression-of-object-member.js'),
  204. false
  205. );
  206. expectedStackTraceData = [];
  207. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  208. assert.deepEqual(stackTraceData, expectedStackTraceData);
  209. });
  210. it('should returns correct BlockScopeTraceData - variant #6: no call expressions', () => {
  211. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  212. readFileAsString('./test/fixtures/stack-trace-analyzer/no-call-expressions.js'),
  213. false
  214. );
  215. expectedStackTraceData = [];
  216. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  217. assert.deepEqual(stackTraceData, expectedStackTraceData);
  218. });
  219. it('should returns correct BlockScopeTraceData - variant #7: only call expression', () => {
  220. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  221. readFileAsString('./test/fixtures/stack-trace-analyzer/only-call-expression.js'),
  222. false
  223. );
  224. expectedStackTraceData = [];
  225. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  226. assert.deepEqual(stackTraceData, expectedStackTraceData);
  227. });
  228. it('should returns correct BlockScopeTraceData - variant #8: self-invoking functions', () => {
  229. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  230. readFileAsString('./test/fixtures/stack-trace-analyzer/self-invoking-functions.js'),
  231. false
  232. );
  233. expectedStackTraceData = [
  234. {
  235. name: null,
  236. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'foo')).body,
  237. stackTrace: [{
  238. name: null,
  239. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'bar')).body,
  240. stackTrace: [{
  241. name: 'baz',
  242. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  243. stackTrace: []
  244. }]
  245. }]
  246. }
  247. ];
  248. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  249. assert.deepEqual(stackTraceData, expectedStackTraceData);
  250. });
  251. });
  252. });