NodeMocks.ts 3.0 KB

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