NodeUtils.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 {Statement} node
  65. * @returns {TStatement | null}
  66. */
  67. public static getNextSiblingStatementNode (node: ESTree.Statement): TStatement | null {
  68. return NodeUtils.getSiblingStatementNodeByOffset(node, 1);
  69. }
  70. /**
  71. * @param {Statement} node
  72. * @returns {TStatement | null}
  73. */
  74. public static getPreviousSiblingStatementNode (node: ESTree.Statement): TStatement | null {
  75. return NodeUtils.getSiblingStatementNodeByOffset(node, -1);
  76. }
  77. /**
  78. * @param {NodeGuards} node
  79. * @returns {TNodeWithScope}
  80. */
  81. public static getScopeOfNode (node: ESTree.Node): TNodeWithScope {
  82. const parentNode: ESTree.Node | undefined = node.parentNode;
  83. if (!parentNode) {
  84. throw new ReferenceError('`parentNode` property of given node is `undefined`');
  85. }
  86. if (!NodeGuards.isNodeHasScope(parentNode)) {
  87. return NodeUtils.getScopeOfNode(parentNode);
  88. }
  89. return parentNode;
  90. }
  91. /**
  92. * @param {UnaryExpression} unaryExpressionNode
  93. * @returns {NodeGuards}
  94. */
  95. public static getUnaryExpressionArgumentNode (unaryExpressionNode: ESTree.UnaryExpression): ESTree.Node {
  96. if (NodeGuards.isUnaryExpressionNode(unaryExpressionNode.argument)) {
  97. return NodeUtils.getUnaryExpressionArgumentNode(unaryExpressionNode.argument);
  98. }
  99. return unaryExpressionNode.argument;
  100. }
  101. /**
  102. * @param {T} astTree
  103. * @returns {T}
  104. */
  105. public static parentize <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
  106. estraverse.replace(astTree, {
  107. enter: NodeUtils.parentizeNode
  108. });
  109. return astTree;
  110. }
  111. /**
  112. * @param {T} node
  113. * @param {Node} parentNode
  114. * @returns {T}
  115. */
  116. public static parentizeNode <T extends ESTree.Node = ESTree.Node> (node: T, parentNode: ESTree.Node | null): T {
  117. node.parentNode = parentNode || node;
  118. node.obfuscatedNode = false;
  119. return node;
  120. }
  121. /**
  122. * @param {T} node
  123. * @returns {T}
  124. */
  125. private static cloneRecursive <T> (node: T): T {
  126. if (node === null) {
  127. return node;
  128. }
  129. const copy: TObject = {};
  130. for (const property in node) {
  131. if (!node.hasOwnProperty(property) || property === 'parentNode') {
  132. continue;
  133. }
  134. const value: any = node[property];
  135. let clonedValue: any | null;
  136. if (value === null || value instanceof RegExp) {
  137. clonedValue = value;
  138. } else if (Array.isArray(value)) {
  139. clonedValue = value.map(NodeUtils.cloneRecursive);
  140. } else if (typeof value === 'object') {
  141. clonedValue = NodeUtils.cloneRecursive(value);
  142. } else {
  143. clonedValue = value;
  144. }
  145. copy[property] = clonedValue;
  146. }
  147. return <T>copy;
  148. }
  149. /**
  150. * @param {Node} node
  151. * @param {TNodeWithBlockScope[]} blockScopes
  152. * @param {number} depth
  153. * @returns {TNodeWithBlockScope[]}
  154. */
  155. private static getBlockScopesOfNodeRecursive (
  156. node: ESTree.Node,
  157. blockScopes: TNodeWithBlockScope[] = [],
  158. depth: number = 0
  159. ): TNodeWithBlockScope[] {
  160. const parentNode: ESTree.Node | undefined = node.parentNode;
  161. if (!parentNode) {
  162. throw new ReferenceError('`parentNode` property of given node is `undefined`');
  163. }
  164. /**
  165. * Stage 1: process root block statement node of the slice of AST-tree
  166. */
  167. if (NodeGuards.isBlockStatementNode(node) && parentNode === node) {
  168. blockScopes.push(node);
  169. }
  170. /**
  171. * Stage 2: process any other nodes
  172. */
  173. if (
  174. /**
  175. * we can add program node instantly
  176. */
  177. NodeGuards.isProgramNode(node) ||
  178. /**
  179. * we shouldn't add to the array input node that is node with block scope itself
  180. * so, on depth 0 we will skip push to the array of block scopes
  181. */
  182. (depth && NodeGuards.isNodeHasBlockScope(node, parentNode))
  183. ) {
  184. blockScopes.push(node);
  185. }
  186. if (node !== parentNode) {
  187. return NodeUtils.getBlockScopesOfNodeRecursive(parentNode, blockScopes, ++depth);
  188. }
  189. return blockScopes;
  190. }
  191. /**
  192. * @param {Statement} node
  193. * @param {number} offset
  194. * @returns {TStatement | null}
  195. */
  196. private static getSiblingStatementNodeByOffset (node: ESTree.Statement, offset: number): TStatement | null {
  197. const scopeNode: TNodeWithScope = NodeUtils.getScopeOfNode(node);
  198. const scopeBody: TStatement[] = !NodeGuards.isSwitchCaseNode(scopeNode)
  199. ? scopeNode.body
  200. : scopeNode.consequent;
  201. const indexInScope: number = scopeBody.indexOf(node);
  202. return scopeBody[indexInScope + offset] || null;
  203. }
  204. }