CallExpressionControlFlowStorageCallNode.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { Expression } from 'estree';
  5. import { TStatement } from '../../../types/node/TStatement';
  6. import { IOptions } from '../../../interfaces/options/IOptions';
  7. import { IRandomGenerator } from "../../../interfaces/utils/IRandomGenerator";
  8. import { initializable } from '../../../decorators/Initializable';
  9. import { AbstractCustomNode } from '../../AbstractCustomNode';
  10. import { Nodes } from '../../../node/Nodes';
  11. import { NodeUtils } from '../../../node/NodeUtils';
  12. @injectable()
  13. export class CallExpressionControlFlowStorageCallNode extends AbstractCustomNode {
  14. /**
  15. * @type {Expression}
  16. */
  17. @initializable()
  18. private callee: Expression;
  19. /**
  20. * @type {string}
  21. */
  22. @initializable()
  23. private controlFlowStorageKey: string;
  24. /**
  25. * @type {string}
  26. */
  27. @initializable()
  28. private controlFlowStorageName: string;
  29. /**
  30. * @type {(ESTree.Expression | ESTree.SpreadElement)[]}
  31. */
  32. @initializable()
  33. private expressionArguments: (ESTree.Expression | ESTree.SpreadElement)[];
  34. /**
  35. * @type {IRandomGenerator}
  36. */
  37. private readonly randomGenerator: IRandomGenerator;
  38. /**
  39. * @param randomGenerator
  40. * @param options
  41. */
  42. constructor (
  43. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  44. @inject(ServiceIdentifiers.IOptions) options: IOptions
  45. ) {
  46. super(options);
  47. this.randomGenerator = randomGenerator;
  48. }
  49. /**
  50. * @param controlFlowStorageName
  51. * @param controlFlowStorageKey
  52. * @param callee
  53. * @param expressionArguments
  54. */
  55. public initialize (
  56. controlFlowStorageName: string,
  57. controlFlowStorageKey: string,
  58. callee: Expression,
  59. expressionArguments: (ESTree.Expression | ESTree.SpreadElement)[]
  60. ): void {
  61. this.controlFlowStorageName = controlFlowStorageName;
  62. this.controlFlowStorageKey = controlFlowStorageKey;
  63. this.callee = callee;
  64. this.expressionArguments = expressionArguments;
  65. }
  66. protected getNodeStructure (): TStatement[] {
  67. const structure: TStatement = Nodes.getExpressionStatementNode(
  68. Nodes.getCallExpressionNode(
  69. Nodes.getMemberExpressionNode(
  70. Nodes.getIdentifierNode(this.controlFlowStorageName),
  71. Nodes.getIdentifierNode(this.controlFlowStorageKey)
  72. ),
  73. [
  74. this.callee,
  75. ...this.expressionArguments
  76. ]
  77. )
  78. );
  79. NodeUtils.parentize(structure);
  80. return [structure];
  81. }
  82. }