StackTraceAnalyzer.spec.ts 15 KB

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