NodeMocks.ts 5.1 KB

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