NodeMocks.ts 3.5 KB

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