NodeMocks.ts 3.4 KB

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