NodeUtils.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /**
  34. * @param {T} node
  35. * @returns {T}
  36. */
  37. const cloneRecursive: (node: T) => T = (node: T) => {
  38. if (node === null) {
  39. return node;
  40. }
  41. const copy: TObject = {};
  42. Object
  43. .keys(node)
  44. .forEach((property: string): void => {
  45. if (property === 'parentNode') {
  46. return;
  47. }
  48. const value: any = (<TObject>node)[property];
  49. let clonedValue: any | null;
  50. if (value === null || value instanceof RegExp) {
  51. clonedValue = value;
  52. } else if (Array.isArray(value)) {
  53. clonedValue = value.map(cloneRecursive);
  54. } else if (typeof value === 'object') {
  55. clonedValue = cloneRecursive(value);
  56. } else {
  57. clonedValue = value;
  58. }
  59. copy[property] = clonedValue;
  60. });
  61. return <T>copy;
  62. };
  63. return NodeUtils.parentize(cloneRecursive(astTree));
  64. }
  65. /**
  66. * @param {string} code
  67. * @returns {TStatement[]}
  68. */
  69. public static convertCodeToStructure (code: string): TStatement[] {
  70. let structure: ESTree.Program = esprima.parseScript(code);
  71. structure = NodeUtils.addXVerbatimPropertyToLiterals(structure);
  72. structure = NodeUtils.parentize(structure);
  73. return structure.body;
  74. }
  75. /**
  76. * @param {NodeGuards[]} structure
  77. * @returns {string}
  78. */
  79. public static convertStructureToCode (structure: ESTree.Node[]): string {
  80. return structure.reduce((code: string, node: ESTree.Node) => {
  81. return code + escodegen.generate(node, {
  82. sourceMapWithCode: true
  83. }).code;
  84. }, '');
  85. }
  86. /**
  87. * @param {NodeGuards} node
  88. * @param {TNodeWithBlockScope[]} blockScopes
  89. * @returns {TNodeWithBlockScope[]}
  90. */
  91. public static getBlockScopesOfNode (node: ESTree.Node, blockScopes: TNodeWithBlockScope[] = []): TNodeWithBlockScope[] {
  92. const parentNode: ESTree.Node | undefined = node.parentNode;
  93. if (!parentNode) {
  94. throw new ReferenceError('`parentNode` property of given node is `undefined`');
  95. }
  96. if (NodeGuards.isBlockStatementNode(parentNode) && NodeGuards.isNodeHasBlockScope(parentNode)) {
  97. blockScopes.push(parentNode);
  98. }
  99. if (node !== parentNode) {
  100. return NodeUtils.getBlockScopesOfNode(parentNode, blockScopes);
  101. }
  102. if (NodeGuards.isNodeHasBlockScope(parentNode)) {
  103. blockScopes.push(parentNode);
  104. }
  105. return blockScopes;
  106. }
  107. /**
  108. * @param {NodeGuards} node
  109. * @returns {TNodeWithScope}
  110. */
  111. public static getScopeOfNode (node: ESTree.Node): TNodeWithScope {
  112. const parentNode: ESTree.Node | undefined = node.parentNode;
  113. if (!parentNode) {
  114. throw new ReferenceError('`parentNode` property of given node is `undefined`');
  115. }
  116. if (!NodeGuards.isNodeHasScope(parentNode)) {
  117. return NodeUtils.getScopeOfNode(parentNode);
  118. }
  119. return parentNode;
  120. }
  121. /**
  122. * @param {UnaryExpression} unaryExpressionNode
  123. * @returns {NodeGuards}
  124. */
  125. public static getUnaryExpressionArgumentNode (unaryExpressionNode: ESTree.UnaryExpression): ESTree.Node {
  126. if (NodeGuards.isUnaryExpressionNode(unaryExpressionNode.argument)) {
  127. return NodeUtils.getUnaryExpressionArgumentNode(unaryExpressionNode.argument);
  128. }
  129. return unaryExpressionNode.argument;
  130. }
  131. /**
  132. * @param {T} astTree
  133. * @returns {T}
  134. */
  135. public static parentize <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
  136. estraverse.replace(astTree, {
  137. enter: NodeUtils.parentizeNode
  138. });
  139. return astTree;
  140. }
  141. /**
  142. * @param {T} node
  143. * @param {Node} parentNode
  144. * @returns {T}
  145. */
  146. public static parentizeNode <T extends ESTree.Node = ESTree.Node> (node: T, parentNode: ESTree.Node | null): T {
  147. node.parentNode = parentNode || node;
  148. node.obfuscatedNode = false;
  149. return node;
  150. }
  151. }