NodeAppender.spec.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
  2. import * as ESTree from 'estree';
  3. import { assert } from 'chai';
  4. import { TStatement } from '../../../../src/types/node/TStatement';
  5. import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IStackTraceAnalyzer } from '../../../../src/interfaces/analyzers/stack-trace-analyzer/IStackTraceAnalyzer';
  7. import { IStackTraceData } from '../../../../src/interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
  8. import { readFileAsString } from '../../../helpers/readFileAsString';
  9. import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
  10. import { NodeAppender } from '../../../../src/node/NodeAppender';
  11. import { Nodes } from '../../../../src/node/Nodes';
  12. import { NodeUtils } from '../../../../src/node/NodeUtils';
  13. /**
  14. * @param fixturePath
  15. * @return {TStatement[]}
  16. */
  17. const convertCodeToStructure: (fixturePath: string) => TStatement[] = (fixturePath) => {
  18. return NodeUtils.convertCodeToStructure(
  19. readFileAsString(`${__dirname}${fixturePath}`)
  20. );
  21. };
  22. /**
  23. * @param fixturePath
  24. * @return {ESTree.Program}
  25. */
  26. const convertCodeToAst: (fixturePath: string) => ESTree.Program = (fixturePath) => {
  27. return Nodes.getProgramNode(convertCodeToStructure(fixturePath));
  28. };
  29. describe('NodeAppender', () => {
  30. describe('appendNode (blockScopeNode: TNodeWithBlockStatement[], nodeBodyStatements: TStatement[]): void', () => {
  31. let astTree: ESTree.Program,
  32. expectedAstTree: ESTree.Program,
  33. node: TStatement[];
  34. before(() => {
  35. node = convertCodeToStructure('/fixtures/simple-input.js');
  36. astTree = convertCodeToAst('/fixtures/append-node.js');
  37. expectedAstTree = convertCodeToAst('/fixtures/append-node-expected.js');
  38. astTree = NodeUtils.parentize(astTree);
  39. expectedAstTree = NodeUtils.parentize(expectedAstTree);
  40. NodeAppender.appendNode(astTree, node);
  41. });
  42. it('should append given node to a `BlockStatement` node body', () => {
  43. assert.deepEqual(astTree, expectedAstTree);
  44. });
  45. });
  46. describe('appendNodeToOptimalBlockScope (blockScopeStackTraceData: IStackTraceData[], blockScopeNode: TNodeWithBlockStatement, nodeBodyStatements: TStatement[], index: number = 0): void', () => {
  47. let stackTraceAnalyzer: IStackTraceAnalyzer,
  48. astTree: ESTree.Program,
  49. expectedAstTree: ESTree.Program,
  50. node: TStatement[],
  51. stackTraceData: IStackTraceData[];
  52. before(() => {
  53. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  54. inversifyContainerFacade.load('', {});
  55. stackTraceAnalyzer = inversifyContainerFacade
  56. .get<IStackTraceAnalyzer>(ServiceIdentifiers.IStackTraceAnalyzer);
  57. });
  58. beforeEach(() => {
  59. node = convertCodeToStructure('/fixtures/simple-input.js');
  60. });
  61. describe('variant #1: nested function calls', () => {
  62. beforeEach(() => {
  63. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-1.js');
  64. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-1-expected.js');
  65. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  66. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node);
  67. });
  68. it('should append node into first and deepest function call in nested function calls', () => {
  69. assert.deepEqual(astTree, expectedAstTree);
  70. });
  71. });
  72. describe('variant #2: nested function calls', () => {
  73. beforeEach(() => {
  74. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-2.js');
  75. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-2-expected.js');
  76. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  77. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node);
  78. });
  79. it('should append node into first and deepest function call in nested function calls', () => {
  80. assert.deepEqual(astTree, expectedAstTree);
  81. });
  82. });
  83. describe('append by specific index', () => {
  84. let astTree: ESTree.Program;
  85. beforeEach(() => {
  86. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index.js');
  87. });
  88. describe('variant #1: append by specific index in nested function calls', () => {
  89. beforeEach(() => {
  90. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-1-expected.js');
  91. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  92. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node, 2);
  93. });
  94. it('should append node into deepest function call by specified index in nested function calls', () => {
  95. assert.deepEqual(astTree, expectedAstTree);
  96. });
  97. });
  98. describe('variant #2: append by specific index in nested function calls', () => {
  99. beforeEach(() => {
  100. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-2-expected.js');
  101. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  102. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node, 1);
  103. });
  104. it('should append node into deepest function call by specified index in nested function calls', () => {
  105. assert.deepEqual(astTree, expectedAstTree);
  106. });
  107. });
  108. describe('variant #3: append by specific index in nested function calls', () => {
  109. beforeEach(() => {
  110. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-3.js');
  111. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-3-expected.js');
  112. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  113. NodeAppender.appendNodeToOptimalBlockScope(
  114. stackTraceData,
  115. astTree,
  116. node,
  117. stackTraceData.length - 1
  118. );
  119. });
  120. it('should append node into deepest function call by specified index in nested function calls', () => {
  121. assert.deepEqual(astTree, expectedAstTree);
  122. });
  123. });
  124. });
  125. });
  126. describe('insertNodeAtIndex (blockScopeNode: TNodeWithBlockStatement[], nodeBodyStatements: TStatement[], index: number): void', () => {
  127. let astTree: ESTree.Program,
  128. expectedAstTree: ESTree.Program,
  129. node: TStatement[];
  130. before(() => {
  131. node = convertCodeToStructure('/fixtures/simple-input.js');
  132. astTree = convertCodeToAst('/fixtures/insert-node-at-index.js');
  133. expectedAstTree = convertCodeToAst('/fixtures/insert-node-at-index-expected.js');
  134. astTree = NodeUtils.parentize(astTree);
  135. expectedAstTree = NodeUtils.parentize(expectedAstTree);
  136. NodeAppender.insertNodeAtIndex(astTree, node, 2);
  137. });
  138. it('should insert given node in `BlockStatement` node body at index', () => {
  139. assert.deepEqual(astTree, expectedAstTree);
  140. });
  141. });
  142. describe('prependNode (blockScopeNode: TNodeWithBlockStatement[], nodeBodyStatements: TStatement[]): void', () => {
  143. let astTree: ESTree.Program,
  144. expectedAstTree: ESTree.Program,
  145. node: TStatement[];
  146. before(() => {
  147. node = convertCodeToStructure('/fixtures/simple-input.js');
  148. astTree = convertCodeToAst('/fixtures/prepend-node.js');
  149. expectedAstTree = convertCodeToAst('/fixtures/prepend-node-expected.js');
  150. astTree = NodeUtils.parentize(astTree);
  151. expectedAstTree = NodeUtils.parentize(expectedAstTree);
  152. NodeAppender.prependNode(astTree, node);
  153. });
  154. it('should prepend given node to a `BlockStatement` node body', () => {
  155. assert.deepEqual(astTree, expectedAstTree);
  156. });
  157. });
  158. });