NodeAppender.spec.ts 9.6 KB

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