NodeMocks.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import * as escodegen from 'escodegen';
  2. import * as ESTree from 'estree';
  3. import { NodeType } from '../../src/enums/NodeType';
  4. export class NodeMocks {
  5. /**
  6. * @param bodyNodes
  7. * @returns {ESTree.Program}
  8. */
  9. public static getProgramNode (bodyNodes: ESTree.Statement[] = []): ESTree.Program {
  10. return {
  11. type: NodeType.Program,
  12. body: bodyNodes,
  13. sourceType: 'script',
  14. obfuscated: false
  15. };
  16. }
  17. /**
  18. * @param bodyNodes
  19. * @returns {ESTree.BlockStatement}
  20. */
  21. public static getBlockStatementNode (bodyNodes: ESTree.Statement[] = []): ESTree.BlockStatement {
  22. return {
  23. type: NodeType.BlockStatement,
  24. body: bodyNodes,
  25. obfuscated: false
  26. };
  27. }
  28. /**
  29. * @param bodyNodes
  30. * @returns {ESTree.CatchClause}
  31. */
  32. public static getCatchClauseNode (bodyNodes: ESTree.Statement[] = []): ESTree.CatchClause {
  33. return {
  34. type: NodeType.CatchClause,
  35. param: NodeMocks.getIdentifierNode('err'),
  36. body: NodeMocks.getBlockStatementNode(bodyNodes),
  37. obfuscated: false
  38. };
  39. }
  40. /**
  41. * @param callee
  42. * @param args
  43. * @returns {ESTree.CallExpression}
  44. */
  45. public static getCallExpressionNode (
  46. callee: ESTree.Expression,
  47. args: ESTree.Expression[] | ESTree.SpreadElement[] = []
  48. ): ESTree.CallExpression {
  49. return {
  50. type: NodeType.CallExpression,
  51. callee: callee,
  52. arguments: args,
  53. obfuscated: false
  54. };
  55. }
  56. /**
  57. * @param expression
  58. * @returns {ESTree.ExpressionStatement}
  59. */
  60. public static getExpressionStatementNode (
  61. expression: ESTree.Expression = NodeMocks.getIdentifierNode()
  62. ): ESTree.ExpressionStatement {
  63. return {
  64. type: NodeType.ExpressionStatement,
  65. expression: expression,
  66. obfuscated: false
  67. };
  68. }
  69. /**
  70. * @param functionName
  71. * @param blockStatementNode
  72. * @param params
  73. * @returns {ESTree.FunctionDeclaration}
  74. */
  75. public static getFunctionDeclarationNode (
  76. functionName: string,
  77. blockStatementNode: ESTree.BlockStatement,
  78. params: ESTree.Identifier[] = []
  79. ): ESTree.FunctionDeclaration {
  80. return {
  81. type: NodeType.FunctionDeclaration,
  82. id: NodeMocks.getIdentifierNode(functionName),
  83. params: params,
  84. body: blockStatementNode,
  85. generator: false,
  86. obfuscated: false
  87. };
  88. }
  89. /**
  90. * @param blockStatementNode
  91. * @returns {ESTree.IfStatement}
  92. */
  93. public static getIfStatementNode (blockStatementNode: ESTree.BlockStatement): ESTree.IfStatement {
  94. return {
  95. type: 'IfStatement',
  96. test: {
  97. type: 'Literal',
  98. value: true,
  99. raw: 'true',
  100. obfuscated: false
  101. },
  102. consequent: blockStatementNode,
  103. obfuscated: false
  104. };
  105. }
  106. /**
  107. * @param identifierName
  108. * @returns {ESTree.Identifier}
  109. */
  110. public static getIdentifierNode (identifierName: string = 'identifier'): ESTree.Identifier {
  111. return {
  112. type: NodeType.Identifier,
  113. name: identifierName,
  114. obfuscated: false
  115. };
  116. }
  117. /**
  118. * @param value
  119. * @returns {ESTree.Literal}
  120. */
  121. public static getLiteralNode (value: boolean|number|string = 'value'): ESTree.Literal {
  122. return {
  123. type: NodeType.Literal,
  124. value: value,
  125. raw: `'${value}'`,
  126. 'x-verbatim-property': {
  127. content: `'${value}'`,
  128. precedence: escodegen.Precedence.Primary
  129. },
  130. obfuscated: false
  131. };
  132. }
  133. /**
  134. * @param object
  135. * @param property
  136. * @return {ESTree.MemberExpression}
  137. */
  138. public static getMemberExpressionNode (
  139. object: ESTree.Identifier,
  140. property: ESTree.Identifier|ESTree.Literal
  141. ): ESTree.MemberExpression {
  142. return {
  143. type: NodeType.MemberExpression,
  144. computed: false,
  145. object: object,
  146. property: property,
  147. obfuscated: false
  148. };
  149. }
  150. /**
  151. * @param declarations
  152. * @param kind
  153. * @returns {ESTree.VariableDeclaration}
  154. */
  155. public static getVariableDeclarationNode (
  156. declarations: ESTree.VariableDeclarator[] = [],
  157. kind: 'var' | 'let' | 'const' = 'var'
  158. ): ESTree.VariableDeclaration {
  159. return {
  160. type: NodeType.VariableDeclaration,
  161. declarations: declarations,
  162. kind: kind,
  163. obfuscated: false
  164. };
  165. }
  166. /**
  167. * @param id
  168. * @param init
  169. * @returns {ESTree.VariableDeclarator}
  170. */
  171. public static getVariableDeclaratorNode (id: ESTree.Identifier, init: any): ESTree.VariableDeclarator {
  172. return {
  173. type: NodeType.VariableDeclarator,
  174. id: id,
  175. init: init,
  176. obfuscated: false
  177. };
  178. }
  179. }