NodeMocks.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import * as escodegen from 'escodegen';
  2. import { TExpression } from "../../src/types/nodes/TExpression";
  3. import { TStatement } from "../../src/types/nodes/TStatement";
  4. import { IBlockStatementNode } from "../../src/interfaces/nodes/IBlockStatementNode";
  5. import { ICallExpressionNode } from "../../src/interfaces/nodes/ICallExpressionNode";
  6. import { ICatchClauseNode } from "../../src/interfaces/nodes/ICatchClauseNode";
  7. import { IExpressionStatementNode } from "../../src/interfaces/nodes/IExpressionStatementNode";
  8. import { IFunctionDeclarationNode } from "../../src/interfaces/nodes/IFunctionDeclarationNode";
  9. import { IIdentifierNode } from "../../src/interfaces/nodes/IIdentifierNode";
  10. import { IIfStatementNode } from "../../src/interfaces/nodes/IIfStatementNode";
  11. import { ILiteralNode } from "../../src/interfaces/nodes/ILiteralNode";
  12. import { IProgramNode } from "../../src/interfaces/nodes/IProgramNode";
  13. import { ISpreadElementNode } from "../../src/interfaces/nodes/ISpreadElementNode";
  14. import { NodeType } from "../../src/enums/NodeType";
  15. export class NodeMocks {
  16. /**
  17. * @param bodyNodes
  18. * @returns {IProgramNode}
  19. */
  20. public static getProgramNode (bodyNodes: TStatement[] = []): IProgramNode {
  21. return {
  22. type: NodeType.Program,
  23. body: bodyNodes
  24. };
  25. }
  26. /**
  27. * @param bodyNodes
  28. * @returns {IBlockStatementNode}
  29. */
  30. public static getBlockStatementNode (bodyNodes: TStatement[] = []): IBlockStatementNode {
  31. return {
  32. type: NodeType.BlockStatement,
  33. body: bodyNodes
  34. };
  35. }
  36. /**
  37. * @param bodyNodes
  38. * @returns {ICatchClauseNode}
  39. */
  40. public static getCatchClauseNode (bodyNodes: TStatement[] = []): ICatchClauseNode {
  41. return {
  42. type: NodeType.CatchClause,
  43. param: NodeMocks.getIdentifierNode('err'),
  44. body: NodeMocks.getBlockStatementNode(bodyNodes)
  45. };
  46. }
  47. /**
  48. * @param callee
  49. * @param args
  50. * @returns {ICallExpressionNode}
  51. */
  52. public static getCallExpressionNode (
  53. callee: TExpression,
  54. args: TExpression[] | ISpreadElementNode[] = []
  55. ): ICallExpressionNode {
  56. return {
  57. type: NodeType.CallExpression,
  58. callee: callee,
  59. arguments: args
  60. };
  61. }
  62. /**
  63. * @param expression
  64. * @returns {IExpressionStatementNode}
  65. */
  66. public static getExpressionStatementNode (
  67. expression: TExpression = NodeMocks.getIdentifierNode()
  68. ): IExpressionStatementNode {
  69. return {
  70. type: NodeType.ExpressionStatement,
  71. expression: expression
  72. };
  73. }
  74. /**
  75. * @param functionName
  76. * @param blockStatementNode
  77. * @param params
  78. * @returns {IFunctionDeclarationNode}
  79. */
  80. public static getFunctionDeclarationNode (
  81. functionName: string,
  82. blockStatementNode: IBlockStatementNode,
  83. params: IIdentifierNode[] = []
  84. ): IFunctionDeclarationNode {
  85. return {
  86. type: NodeType.FunctionDeclaration,
  87. id: NodeMocks.getIdentifierNode(functionName),
  88. params: params,
  89. body: blockStatementNode,
  90. generator: false,
  91. expression: false
  92. };
  93. }
  94. /**
  95. * @param blockStatementNode
  96. * @returns {IIfStatementNode}
  97. */
  98. public static getIfStatementNode (blockStatementNode: IBlockStatementNode): IIfStatementNode {
  99. return {
  100. type: 'IfStatement',
  101. test: {
  102. type: 'Literal',
  103. value: true,
  104. raw: 'true'
  105. },
  106. consequent: blockStatementNode,
  107. alternate: null
  108. };
  109. }
  110. /**
  111. * @param identifierName
  112. * @returns {IIdentifierNode}
  113. */
  114. public static getIdentifierNode (identifierName: string = 'identifier'): IIdentifierNode {
  115. return {
  116. type: NodeType.Identifier,
  117. name: identifierName,
  118. };
  119. }
  120. /**
  121. * @returns {ILiteralNode}
  122. */
  123. public static getLiteralNode (): ILiteralNode {
  124. return {
  125. type: NodeType.Literal,
  126. value: 'string',
  127. raw: `'string'`,
  128. 'x-verbatim-property': {
  129. content: `'string'`,
  130. precedence: escodegen.Precedence.Primary
  131. }
  132. };
  133. }
  134. }