StackTraceAnalyzer.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. /**
  78. * @param astTree
  79. * @param objectName
  80. * @param name
  81. * @returns {ESTree.FunctionExpression|null}
  82. */
  83. function getObjectFunctionExpressionByName (astTree: ESTree.Node, objectName: string, name: string): ESTree.FunctionExpression|null {
  84. let functionExpressionNode: ESTree.FunctionExpression|null = null,
  85. targetObjectExpressionNode: ESTree.ObjectExpression|null = null;
  86. estraverse.traverse(astTree, {
  87. enter: (node: ESTree.Node): any => {
  88. if (
  89. Nodes.isVariableDeclaratorNode(node) &&
  90. Nodes.isIdentifierNode(node.id) &&
  91. node.init &&
  92. Nodes.isObjectExpressionNode(node.init) &&
  93. node.id.name === objectName
  94. ) {
  95. targetObjectExpressionNode = node.init;
  96. return estraverse.VisitorOption.Break;
  97. }
  98. }
  99. });
  100. if (!targetObjectExpressionNode) {
  101. return null;
  102. }
  103. estraverse.traverse(targetObjectExpressionNode, {
  104. enter: (node: ESTree.Node): any => {
  105. if (
  106. Nodes.isPropertyNode(node) &&
  107. Nodes.isFunctionExpressionNode(node.value) &&
  108. Nodes.isIdentifierNode(node.key) &&
  109. node.key.name === name
  110. ) {
  111. functionExpressionNode = node.value;
  112. return estraverse.VisitorOption.Break;
  113. }
  114. }
  115. });
  116. return functionExpressionNode;
  117. }
  118. describe('StackTraceAnalyzer', () => {
  119. describe('extract (): IStackTraceData[]', () => {
  120. let astTree: TNodeWithBlockStatement,
  121. stackTraceData: IStackTraceData[],
  122. expectedStackTraceData: IStackTraceData[];
  123. it('should returns correct IStackTraceData - variant #1: basic-1', () => {
  124. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  125. readFileAsString('./test/fixtures/stack-trace-analyzer/basic-1.js'),
  126. false
  127. );
  128. expectedStackTraceData = [
  129. {
  130. name: 'baz',
  131. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  132. stackTrace: []
  133. },
  134. {
  135. name: 'foo',
  136. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  137. stackTrace: []
  138. },
  139. {
  140. name: 'bar',
  141. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  142. stackTrace: [
  143. {
  144. name: 'inner2',
  145. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner2')).body,
  146. stackTrace: [
  147. {
  148. name: 'inner3',
  149. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'inner3')).body,
  150. stackTrace: []
  151. },
  152. ]
  153. },
  154. {
  155. name: 'inner1',
  156. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  157. stackTrace: []
  158. },
  159. ]
  160. }
  161. ];
  162. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  163. assert.deepEqual(stackTraceData, expectedStackTraceData);
  164. });
  165. it('should returns correct BlockScopeTraceData - variant #2: basic-2', () => {
  166. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  167. readFileAsString('./test/fixtures/stack-trace-analyzer/basic-2.js'),
  168. false
  169. );
  170. expectedStackTraceData = [
  171. {
  172. name: 'bar',
  173. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  174. stackTrace: []
  175. },
  176. {
  177. name: 'baz',
  178. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  179. stackTrace: [
  180. {
  181. name: 'inner1',
  182. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  183. stackTrace: []
  184. },
  185. ]
  186. },
  187. {
  188. name: 'foo',
  189. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  190. stackTrace: []
  191. }
  192. ];
  193. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  194. assert.deepEqual(stackTraceData, expectedStackTraceData);
  195. });
  196. it('should returns correct BlockScopeTraceData - variant #3: deep conditions nesting', () => {
  197. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  198. readFileAsString('./test/fixtures/stack-trace-analyzer/deep-conditions-nesting.js'),
  199. false
  200. );
  201. expectedStackTraceData = [
  202. {
  203. name: 'bar',
  204. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  205. stackTrace: []
  206. },
  207. {
  208. name: 'baz',
  209. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  210. stackTrace: [
  211. {
  212. name: 'inner1',
  213. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  214. stackTrace: []
  215. },
  216. ]
  217. },
  218. {
  219. name: 'foo',
  220. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  221. stackTrace: []
  222. }
  223. ];
  224. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  225. assert.deepEqual(stackTraceData, expectedStackTraceData);
  226. });
  227. it('should returns correct BlockScopeTraceData - variant #4: call before declaration', () => {
  228. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  229. readFileAsString('./test/fixtures/stack-trace-analyzer/call-before-declaration.js'),
  230. false
  231. );
  232. expectedStackTraceData = [
  233. {
  234. name: 'bar',
  235. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  236. stackTrace: []
  237. }
  238. ];
  239. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  240. assert.deepEqual(stackTraceData, expectedStackTraceData);
  241. });
  242. it('should returns correct BlockScopeTraceData - variant #5: call expression of object member', () => {
  243. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  244. readFileAsString('./test/fixtures/stack-trace-analyzer/call-expression-of-object-member.js'),
  245. false
  246. );
  247. expectedStackTraceData = [
  248. {
  249. name: 'baz',
  250. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
  251. stackTrace: []
  252. },
  253. {
  254. name: 'baz',
  255. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
  256. stackTrace: []
  257. },
  258. {
  259. name: 'bar',
  260. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'bar')).body,
  261. stackTrace: [
  262. {
  263. name: 'inner1',
  264. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  265. stackTrace: [
  266. ]
  267. },
  268. ]
  269. },
  270. {
  271. name: 'bar',
  272. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object', 'bar')).body,
  273. stackTrace: [
  274. {
  275. name: 'inner',
  276. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner')).body,
  277. stackTrace: [
  278. ]
  279. },
  280. ]
  281. }
  282. ];
  283. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  284. assert.deepEqual(stackTraceData, expectedStackTraceData);
  285. });
  286. it('should returns correct BlockScopeTraceData - variant #6: no call expressions', () => {
  287. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  288. readFileAsString('./test/fixtures/stack-trace-analyzer/no-call-expressions.js'),
  289. false
  290. );
  291. expectedStackTraceData = [];
  292. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  293. assert.deepEqual(stackTraceData, expectedStackTraceData);
  294. });
  295. it('should returns correct BlockScopeTraceData - variant #7: only call expression', () => {
  296. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  297. readFileAsString('./test/fixtures/stack-trace-analyzer/only-call-expression.js'),
  298. false
  299. );
  300. expectedStackTraceData = [];
  301. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  302. assert.deepEqual(stackTraceData, expectedStackTraceData);
  303. });
  304. it('should returns correct BlockScopeTraceData - variant #8: self-invoking functions', () => {
  305. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  306. readFileAsString('./test/fixtures/stack-trace-analyzer/self-invoking-functions.js'),
  307. false
  308. );
  309. expectedStackTraceData = [
  310. {
  311. name: null,
  312. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'foo')).body,
  313. stackTrace: [{
  314. name: null,
  315. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'bar')).body,
  316. stackTrace: [{
  317. name: null,
  318. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'baz')).body,
  319. stackTrace: [{
  320. name: 'inner',
  321. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner')).body,
  322. stackTrace: []
  323. }]
  324. }]
  325. }]
  326. }
  327. ];
  328. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  329. assert.deepEqual(stackTraceData, expectedStackTraceData);
  330. });
  331. it('should returns correct BlockScopeTraceData - variant #9: no recursion', () => {
  332. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(
  333. readFileAsString('./test/fixtures/stack-trace-analyzer/no-recursion.js'),
  334. false
  335. );
  336. expectedStackTraceData = [
  337. {
  338. name: 'bar',
  339. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'bar')).body,
  340. stackTrace: []
  341. }
  342. ];
  343. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  344. assert.deepEqual(stackTraceData, expectedStackTraceData);
  345. });
  346. });
  347. });