StackTraceAnalyzer.spec.ts 16 KB

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