NodeUtils.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import * as escodegen from 'escodegen-wallaby';
  2. import * as esprima from 'esprima';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { TNodeWithBlockScope } from '../types/node/TNodeWithBlockScope';
  6. import { TNodeWithScope } from '../types/node/TNodeWithScope';
  7. import { TObject } from '../types/TObject';
  8. import { TStatement } from '../types/node/TStatement';
  9. import { NodeGuards } from './NodeGuards';
  10. export class NodeUtils {
  11. /**
  12. * @param {T} astTree
  13. * @returns {T}
  14. */
  15. public static addXVerbatimPropertyToLiterals <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
  16. estraverse.replace(astTree, {
  17. leave: (node: ESTree.Node) => {
  18. if (NodeGuards.isLiteralNode(node)) {
  19. node['x-verbatim-property'] = {
  20. content: node.raw,
  21. precedence: escodegen.Precedence.Primary
  22. };
  23. }
  24. }
  25. });
  26. return astTree;
  27. }
  28. /**
  29. * @param {T} astTree
  30. * @returns {T}
  31. */
  32. public static clone <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
  33. return NodeUtils.parentize(NodeUtils.cloneRecursive(astTree));
  34. }
  35. /**
  36. * @param {string} code
  37. * @returns {TStatement[]}
  38. */
  39. public static convertCodeToStructure (code: string): TStatement[] {
  40. let structure: ESTree.Program = esprima.parseScript(code);
  41. structure = NodeUtils.addXVerbatimPropertyToLiterals(structure);
  42. structure = NodeUtils.parentize(structure);
  43. return structure.body;
  44. }
  45. /**
  46. * @param {NodeGuards[]} structure
  47. * @returns {string}
  48. */
  49. public static convertStructureToCode (structure: ESTree.Node[]): string {
  50. return structure.reduce((code: string, node: ESTree.Node) => {
  51. return code + escodegen.generate(node, {
  52. sourceMapWithCode: true
  53. }).code;
  54. }, '');
  55. }
  56. /**
  57. * @param {Node} targetNode
  58. * @returns {TNodeWithBlockScope[]}
  59. */
  60. public static getBlockScopesOfNode (targetNode: ESTree.Node): TNodeWithBlockScope[] {
  61. return NodeUtils.getBlockScopesOfNodeRecursive(targetNode);
  62. }
  63. /**
  64. * @param {NodeGuards} node
  65. * @returns {TNodeWithScope}
  66. */
  67. public static getScopeOfNode (node: ESTree.Node): TNodeWithScope {
  68. const parentNode: ESTree.Node | undefined = node.parentNode;
  69. if (!parentNode) {
  70. throw new ReferenceError('`parentNode` property of given node is `undefined`');
  71. }
  72. if (!NodeGuards.isNodeHasScope(parentNode)) {
  73. return NodeUtils.getScopeOfNode(parentNode);
  74. }
  75. return parentNode;
  76. }
  77. /**
  78. * @param {UnaryExpression} unaryExpressionNode
  79. * @returns {NodeGuards}
  80. */
  81. public static getUnaryExpressionArgumentNode (unaryExpressionNode: ESTree.UnaryExpression): ESTree.Node {
  82. if (NodeGuards.isUnaryExpressionNode(unaryExpressionNode.argument)) {
  83. return NodeUtils.getUnaryExpressionArgumentNode(unaryExpressionNode.argument);
  84. }
  85. return unaryExpressionNode.argument;
  86. }
  87. /**
  88. * @param {T} astTree
  89. * @returns {T}
  90. */
  91. public static parentize <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
  92. estraverse.replace(astTree, {
  93. enter: NodeUtils.parentizeNode
  94. });
  95. return astTree;
  96. }
  97. /**
  98. * @param {T} node
  99. * @param {Node} parentNode
  100. * @returns {T}
  101. */
  102. public static parentizeNode <T extends ESTree.Node = ESTree.Node> (node: T, parentNode: ESTree.Node | null): T {
  103. node.parentNode = parentNode || node;
  104. node.obfuscatedNode = false;
  105. return node;
  106. }
  107. /**
  108. * @param {T} node
  109. * @returns {T}
  110. */
  111. private static cloneRecursive <T> (node: T): T {
  112. if (node === null) {
  113. return node;
  114. }
  115. const copy: TObject = {};
  116. for (const property in node) {
  117. if (!node.hasOwnProperty(property) || property === 'parentNode') {
  118. continue;
  119. }
  120. const value: any = node[property];
  121. let clonedValue: any | null;
  122. if (value === null || value instanceof RegExp) {
  123. clonedValue = value;
  124. } else if (Array.isArray(value)) {
  125. clonedValue = value.map(NodeUtils.cloneRecursive);
  126. } else if (typeof value === 'object') {
  127. clonedValue = NodeUtils.cloneRecursive(value);
  128. } else {
  129. clonedValue = value;
  130. }
  131. copy[property] = clonedValue;
  132. }
  133. return <T>copy;
  134. }
  135. /**
  136. * @param {Node} node
  137. * @param {TNodeWithBlockScope[]} blockScopes
  138. * @param {number} depth
  139. * @returns {TNodeWithBlockScope[]}
  140. */
  141. private static getBlockScopesOfNodeRecursive (
  142. node: ESTree.Node,
  143. blockScopes: TNodeWithBlockScope[] = [],
  144. depth: number = 0
  145. ): TNodeWithBlockScope[] {
  146. const parentNode: ESTree.Node | undefined = node.parentNode;
  147. if (!parentNode) {
  148. throw new ReferenceError('`parentNode` property of given node is `undefined`');
  149. }
  150. /**
  151. * Stage 1: process root block statement node of the slice of AST-tree
  152. */
  153. if (NodeGuards.isBlockStatementNode(node) && parentNode === node) {
  154. blockScopes.push(node);
  155. }
  156. /**
  157. * Stage 2: process any other nodes
  158. */
  159. if (
  160. /**
  161. * we can add program node instantly
  162. */
  163. NodeGuards.isProgramNode(node) ||
  164. /**
  165. * we shouldn't add to the array input node that is node with block scope itself
  166. * so, on depth 0 we will skip push to the array of block scopes
  167. */
  168. (depth && NodeGuards.isNodeHasBlockScope(node, parentNode))
  169. ) {
  170. blockScopes.push(node);
  171. }
  172. if (node !== parentNode) {
  173. return NodeUtils.getBlockScopesOfNodeRecursive(parentNode, blockScopes, ++depth);
  174. }
  175. return blockScopes;
  176. }
  177. }