NodeMocks.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. expression: false
  82. };
  83. }
  84. /**
  85. * @param blockStatementNode
  86. * @returns {ESTree.IfStatement}
  87. */
  88. public static getIfStatementNode (blockStatementNode: ESTree.BlockStatement): ESTree.IfStatement {
  89. return {
  90. type: 'IfStatement',
  91. test: {
  92. type: 'Literal',
  93. value: true,
  94. raw: 'true'
  95. },
  96. consequent: blockStatementNode,
  97. alternate: null
  98. };
  99. }
  100. /**
  101. * @param identifierName
  102. * @returns {ESTree.Identifier}
  103. */
  104. public static getIdentifierNode (identifierName: string = 'identifier'): ESTree.Identifier {
  105. return {
  106. type: NodeType.Identifier,
  107. name: identifierName,
  108. };
  109. }
  110. /**
  111. * @param value
  112. * @returns {ESTree.Literal}
  113. */
  114. public static getLiteralNode (value: boolean|number|string = 'value'): ESTree.Literal {
  115. return {
  116. type: NodeType.Literal,
  117. value: value,
  118. raw: `'${value}'`,
  119. 'x-verbatim-property': {
  120. content: `'${value}'`,
  121. precedence: escodegen.Precedence.Primary
  122. }
  123. };
  124. }
  125. /**
  126. * @param object
  127. * @param property
  128. * @return {ESTree.MemberExpression}
  129. */
  130. public static getMemberExpressionNode (
  131. object: ESTree.Identifier,
  132. property: ESTree.Identifier|ESTree.Literal
  133. ): ESTree.MemberExpression {
  134. return {
  135. type: NodeType.MemberExpression,
  136. computed: false,
  137. object: object,
  138. property: property
  139. };
  140. }
  141. /**
  142. * @param declarations
  143. * @param kind
  144. * @returns {ESTree.VariableDeclaration}
  145. */
  146. public static getVariableDeclarationNode (
  147. declarations: ESTree.VariableDeclarator[] = [],
  148. kind: string = 'var'
  149. ): ESTree.VariableDeclaration {
  150. return {
  151. type: NodeType.VariableDeclaration,
  152. declarations: declarations,
  153. kind: kind
  154. };
  155. }
  156. /**
  157. * @param id
  158. * @param init
  159. * @returns {ESTree.VariableDeclarator}
  160. */
  161. public static getVariableDeclaratorNode (id: ESTree.Identifier, init: any): ESTree.VariableDeclarator {
  162. return {
  163. type: NodeType.VariableDeclarator,
  164. id: id,
  165. init: init
  166. };
  167. }
  168. }