NodeMocks.ts 4.7 KB

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