NodeMocks.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 blockStatementNode
  44. * @returns {IFunctionDeclarationNode}
  45. */
  46. public static getFunctionDeclarationNode (blockStatementNode: IBlockStatementNode): IFunctionDeclarationNode {
  47. return {
  48. type: NodeType.FunctionDeclaration,
  49. id: NodeMocks.getIdentifierNode('test'),
  50. params: [],
  51. body: blockStatementNode,
  52. generator: false,
  53. expression: false
  54. };
  55. }
  56. /**
  57. * @param blockStatementNode
  58. * @returns {IIfStatementNode}
  59. */
  60. public static getIfStatementNode (blockStatementNode: IBlockStatementNode): IIfStatementNode {
  61. return {
  62. type: 'IfStatement',
  63. test: {
  64. type: 'Literal',
  65. value: true,
  66. raw: 'true'
  67. },
  68. consequent: blockStatementNode,
  69. alternate: null
  70. };
  71. }
  72. /**
  73. * @param identifierName
  74. * @returns {IIdentifierNode}
  75. */
  76. public static getIdentifierNode (identifierName: string = 'identifier'): IIdentifierNode {
  77. return {
  78. type: NodeType.Identifier,
  79. name: identifierName,
  80. };
  81. }
  82. /**
  83. * @returns {ILiteralNode}
  84. */
  85. public static getLiteralNode (): ILiteralNode {
  86. return {
  87. type: NodeType.Literal,
  88. value: 'string',
  89. raw: `'string'`,
  90. 'x-verbatim-property': `'string'`
  91. };
  92. }
  93. }