StackTraceAnalyzer.spec.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. import 'reflect-metadata';
  2. import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { assert } from 'chai';
  6. import { TNodeWithStatements } from '../../../../src/types/node/TNodeWithStatements';
  7. import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
  8. import { IStackTraceAnalyzer } from '../../../../src/interfaces/analyzers/stack-trace-analyzer/IStackTraceAnalyzer';
  9. import { IStackTraceData } from '../../../../src/interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
  10. import { readFileAsString } from '../../../helpers/readFileAsString';
  11. import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
  12. import { NodeFactory } from '../../../../src/node/NodeFactory';
  13. import { NodeGuards } from '../../../../src/node/NodeGuards';
  14. import { NodeUtils } from '../../../../src/node/NodeUtils';
  15. /**
  16. * @param astTree
  17. * @param name
  18. * @returns {ESTree.FunctionDeclaration|null}
  19. */
  20. function getFunctionDeclarationByName (astTree: ESTree.Node, name: string): ESTree.FunctionDeclaration|null {
  21. let functionDeclarationNode: ESTree.FunctionDeclaration|null = null;
  22. estraverse.traverse(astTree, {
  23. enter: (node: ESTree.Node): any => {
  24. if (
  25. NodeGuards.isFunctionDeclarationNode(node) &&
  26. NodeGuards.isIdentifierNode(node.id) &&
  27. node.id.name === name
  28. ) {
  29. functionDeclarationNode = node;
  30. return estraverse.VisitorOption.Break;
  31. }
  32. }
  33. });
  34. return functionDeclarationNode;
  35. }
  36. /**
  37. * @param astTree
  38. * @param name
  39. * @returns {ESTree.FunctionExpression|null}
  40. */
  41. function getFunctionExpressionByName (astTree: ESTree.Node, name: string): ESTree.FunctionExpression|null {
  42. let functionExpressionNode: ESTree.FunctionExpression|null = null;
  43. estraverse.traverse(astTree, {
  44. enter: (node: ESTree.Node): any => {
  45. if (
  46. NodeGuards.isVariableDeclaratorNode(node) &&
  47. node.init &&
  48. NodeGuards.isFunctionExpressionNode(node.init) &&
  49. NodeGuards.isIdentifierNode(node.id) &&
  50. node.id.name === name
  51. ) {
  52. functionExpressionNode = node.init;
  53. return estraverse.VisitorOption.Break;
  54. }
  55. }
  56. });
  57. return functionExpressionNode;
  58. }
  59. /**
  60. * @param astTree
  61. * @param id
  62. * @returns {ESTree.FunctionExpression|null}
  63. */
  64. function getFunctionExpressionById (astTree: ESTree.Node, id: string): ESTree.FunctionExpression|null {
  65. let functionExpressionNode: ESTree.FunctionExpression|null = null;
  66. estraverse.traverse(astTree, {
  67. enter: (node: ESTree.Node): any => {
  68. if (
  69. NodeGuards.isFunctionExpressionNode(node) &&
  70. node.id &&
  71. NodeGuards.isIdentifierNode(node.id) &&
  72. node.id.name === id
  73. ) {
  74. functionExpressionNode = node;
  75. return estraverse.VisitorOption.Break;
  76. }
  77. }
  78. });
  79. return functionExpressionNode;
  80. }
  81. /**
  82. * @param astTree
  83. * @param objectName
  84. * @param name
  85. * @returns {ESTree.FunctionExpression|null}
  86. */
  87. function getObjectFunctionExpressionByName (astTree: ESTree.Node, objectName: string, name: string|number): ESTree.FunctionExpression|null {
  88. let functionExpressionNode: ESTree.FunctionExpression|null = null,
  89. targetObjectExpressionNode: ESTree.ObjectExpression|null = null;
  90. estraverse.traverse(astTree, {
  91. enter: (node: ESTree.Node): any => {
  92. if (
  93. NodeGuards.isVariableDeclaratorNode(node) &&
  94. NodeGuards.isIdentifierNode(node.id) &&
  95. node.init &&
  96. NodeGuards.isObjectExpressionNode(node.init) &&
  97. node.id.name === objectName
  98. ) {
  99. targetObjectExpressionNode = node.init;
  100. return estraverse.VisitorOption.Break;
  101. }
  102. }
  103. });
  104. if (!targetObjectExpressionNode) {
  105. return null;
  106. }
  107. estraverse.traverse(targetObjectExpressionNode, {
  108. enter: (node: ESTree.Node): any => {
  109. if (
  110. NodeGuards.isPropertyNode(node) &&
  111. NodeGuards.isFunctionExpressionNode(node.value) &&
  112. (
  113. (NodeGuards.isIdentifierNode(node.key) && node.key.name === name) ||
  114. (NodeGuards.isLiteralNode(node.key) && node.key.value === name)
  115. )
  116. ) {
  117. functionExpressionNode = node.value;
  118. return estraverse.VisitorOption.Break;
  119. }
  120. }
  121. });
  122. return functionExpressionNode;
  123. }
  124. describe('StackTraceAnalyzer', () => {
  125. describe('extract', () => {
  126. let stackTraceAnalyzer: IStackTraceAnalyzer,
  127. expectedStackTraceData: IStackTraceData[],
  128. stackTraceData: IStackTraceData[];
  129. before(() => {
  130. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  131. inversifyContainerFacade.load('', '', {});
  132. stackTraceAnalyzer = inversifyContainerFacade
  133. .get<IStackTraceAnalyzer>(ServiceIdentifiers.IStackTraceAnalyzer);
  134. });
  135. describe('Variant #1: basic-1', () => {
  136. before(() => {
  137. const code: string = readFileAsString(__dirname + '/fixtures/basic-1.js');
  138. const astTree: TNodeWithStatements = NodeFactory.programNode(
  139. NodeUtils.convertCodeToStructure(code)
  140. );
  141. expectedStackTraceData = [
  142. {
  143. name: 'baz',
  144. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  145. stackTrace: []
  146. },
  147. {
  148. name: 'foo',
  149. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  150. stackTrace: []
  151. },
  152. {
  153. name: 'bar',
  154. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  155. stackTrace: [
  156. {
  157. name: 'inner2',
  158. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner2')).body,
  159. stackTrace: [
  160. {
  161. name: 'inner3',
  162. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'inner3')).body,
  163. stackTrace: []
  164. },
  165. ]
  166. },
  167. {
  168. name: 'inner1',
  169. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  170. stackTrace: []
  171. },
  172. ]
  173. }
  174. ];
  175. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  176. });
  177. it('should return correct stack trace data', () => {
  178. assert.deepEqual(stackTraceData, expectedStackTraceData);
  179. });
  180. });
  181. describe('Variant #2: basic-2', () => {
  182. before(() => {
  183. const code: string = readFileAsString(__dirname + '/fixtures/basic-2.js');
  184. const astTree: TNodeWithStatements = NodeFactory.programNode(
  185. NodeUtils.convertCodeToStructure(code)
  186. );
  187. expectedStackTraceData = [
  188. {
  189. name: 'bar',
  190. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  191. stackTrace: []
  192. },
  193. {
  194. name: 'baz',
  195. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  196. stackTrace: [
  197. {
  198. name: 'inner1',
  199. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  200. stackTrace: []
  201. },
  202. ]
  203. },
  204. {
  205. name: 'foo',
  206. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  207. stackTrace: []
  208. }
  209. ];
  210. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  211. });
  212. it('should return correct stack trace data', () => {
  213. assert.deepEqual(stackTraceData, expectedStackTraceData);
  214. });
  215. });
  216. describe('Variant #3: deep conditions nesting', () => {
  217. before(() => {
  218. const code: string = readFileAsString(__dirname + '/fixtures/deep-conditions-nesting.js');
  219. const astTree: TNodeWithStatements = NodeFactory.programNode(
  220. NodeUtils.convertCodeToStructure(code)
  221. );
  222. expectedStackTraceData = [
  223. {
  224. name: 'bar',
  225. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  226. stackTrace: []
  227. },
  228. {
  229. name: 'baz',
  230. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'baz')).body,
  231. stackTrace: [
  232. {
  233. name: 'inner1',
  234. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  235. stackTrace: []
  236. },
  237. ]
  238. },
  239. {
  240. name: 'foo',
  241. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'foo')).body,
  242. stackTrace: []
  243. }
  244. ];
  245. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  246. });
  247. it('should return correct stack trace data', () => {
  248. assert.deepEqual(stackTraceData, expectedStackTraceData);
  249. });
  250. });
  251. describe('Variant #4: call before declaration', () => {
  252. before(() => {
  253. const code: string = readFileAsString(__dirname + '/fixtures/call-before-declaration.js');
  254. const astTree: TNodeWithStatements = NodeFactory.programNode(
  255. NodeUtils.convertCodeToStructure(code)
  256. );
  257. expectedStackTraceData = [
  258. {
  259. name: 'bar',
  260. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'bar')).body,
  261. stackTrace: []
  262. }
  263. ];
  264. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  265. });
  266. it('should return correct stack trace data', () => {
  267. assert.deepEqual(stackTraceData, expectedStackTraceData);
  268. });
  269. });
  270. describe('Variant #5: call expression of object member #1', () => {
  271. before(() => {
  272. const code: string = readFileAsString(__dirname + '/fixtures/call-expression-of-object-member-1.js');
  273. const astTree: TNodeWithStatements = NodeFactory.programNode(
  274. NodeUtils.convertCodeToStructure(code)
  275. );
  276. expectedStackTraceData = [
  277. {
  278. name: 'baz',
  279. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
  280. stackTrace: []
  281. },
  282. {
  283. name: 'baz',
  284. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'baz')).body,
  285. stackTrace: []
  286. },
  287. {
  288. name: 'func',
  289. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'func')).body,
  290. stackTrace: []
  291. },
  292. {
  293. name: 'bar',
  294. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 'bar')).body,
  295. stackTrace: [
  296. {
  297. name: 'inner1',
  298. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner1')).body,
  299. stackTrace: [
  300. ]
  301. },
  302. ]
  303. },
  304. {
  305. name: 'bar',
  306. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object', 'bar')).body,
  307. stackTrace: [
  308. {
  309. name: 'inner',
  310. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner')).body,
  311. stackTrace: [
  312. ]
  313. },
  314. ]
  315. }
  316. ];
  317. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  318. });
  319. it('should return correct stack trace data', () => {
  320. assert.deepEqual(stackTraceData, expectedStackTraceData);
  321. });
  322. });
  323. describe('Variant #5: call expression of object member #2', () => {
  324. before(() => {
  325. const code: string = readFileAsString(__dirname + '/fixtures/call-expression-of-object-member-2.js');
  326. const astTree: TNodeWithStatements = NodeFactory.programNode(
  327. NodeUtils.convertCodeToStructure(code)
  328. );
  329. expectedStackTraceData = [
  330. {
  331. name: 'baz',
  332. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object', 'baz')).body,
  333. stackTrace: []
  334. },
  335. {
  336. name: 1,
  337. callee: (<ESTree.FunctionExpression>getObjectFunctionExpressionByName(astTree, 'object1', 1)).body,
  338. stackTrace: []
  339. },
  340. ];
  341. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  342. });
  343. it('should return correct stack trace data', () => {
  344. assert.deepEqual(stackTraceData, expectedStackTraceData);
  345. });
  346. });
  347. describe('Variant #6: no call expressions', () => {
  348. before(() => {
  349. const code: string = readFileAsString(__dirname + '/fixtures/no-call-expressions.js');
  350. const astTree: TNodeWithStatements = NodeFactory.programNode(
  351. NodeUtils.convertCodeToStructure(code)
  352. );
  353. expectedStackTraceData = [];
  354. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  355. });
  356. it('should return correct stack trace data', () => {
  357. assert.deepEqual(stackTraceData, expectedStackTraceData);
  358. });
  359. });
  360. describe('Variant #7: only call expression', () => {
  361. before(() => {
  362. const code: string = readFileAsString(__dirname + '/fixtures/only-call-expression.js');
  363. const astTree: TNodeWithStatements = NodeFactory.programNode(
  364. NodeUtils.convertCodeToStructure(code)
  365. );
  366. expectedStackTraceData = [];
  367. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  368. });
  369. it('should return correct stack trace data', () => {
  370. assert.deepEqual(stackTraceData, expectedStackTraceData);
  371. });
  372. });
  373. describe('Variant #8: self-invoking functions', () => {
  374. before(() => {
  375. const code: string = readFileAsString(__dirname + '/fixtures/self-invoking-functions.js');
  376. const astTree: TNodeWithStatements = NodeFactory.programNode(
  377. NodeUtils.convertCodeToStructure(code)
  378. );
  379. expectedStackTraceData = [
  380. {
  381. name: null,
  382. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'foo')).body,
  383. stackTrace: [{
  384. name: null,
  385. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'bar')).body,
  386. stackTrace: [{
  387. name: null,
  388. callee: (<ESTree.FunctionExpression>getFunctionExpressionById(astTree, 'baz')).body,
  389. stackTrace: [{
  390. name: 'inner',
  391. callee: (<ESTree.FunctionDeclaration>getFunctionDeclarationByName(astTree, 'inner')).body,
  392. stackTrace: []
  393. }]
  394. }]
  395. }]
  396. }
  397. ];
  398. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  399. });
  400. it('should return correct stack trace data', () => {
  401. assert.deepEqual(stackTraceData, expectedStackTraceData);
  402. });
  403. });
  404. describe('Variant #9: no recursion', () => {
  405. before(() => {
  406. const code: string = readFileAsString(__dirname + '/fixtures/no-recursion.js');
  407. const astTree: TNodeWithStatements = NodeFactory.programNode(
  408. NodeUtils.convertCodeToStructure(code)
  409. );
  410. expectedStackTraceData = [
  411. {
  412. name: 'bar',
  413. callee: (<ESTree.FunctionExpression>getFunctionExpressionByName(astTree, 'bar')).body,
  414. stackTrace: []
  415. }
  416. ];
  417. stackTraceData = stackTraceAnalyzer.analyze(astTree);
  418. });
  419. it('should return correct stack trace data', () => {
  420. assert.deepEqual(stackTraceData, expectedStackTraceData);
  421. });
  422. });
  423. });
  424. });