ExpressionWithOperatorControlFlowStorageCallNode.ts 2.7 KB

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