ExpressionWithOperatorControlFlowReplacer.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { TControlFlowCustomNodeFactory } from '../../../types/container/custom-nodes/TControlFlowCustomNodeFactory';
  5. import { TStatement } from '../../../types/node/TStatement';
  6. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  7. import { IOptions } from '../../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  9. import { ControlFlowCustomNode } from '../../../enums/container/custom-nodes/ControlFlowCustomNode';
  10. import { AbstractControlFlowReplacer } from './AbstractControlFlowReplacer';
  11. import { Node } from '../../../node/Node';
  12. @injectable()
  13. export abstract class ExpressionWithOperatorControlFlowReplacer extends AbstractControlFlowReplacer {
  14. /**
  15. * @type {IOptions}
  16. */
  17. protected readonly options: IOptions;
  18. /**
  19. * @type {Map<string, Map<string, string[]>>}
  20. */
  21. protected readonly replacerDataByControlFlowStorageId: Map <string, Map<string, string[]>> = new Map();
  22. /**
  23. * @param {TControlFlowCustomNodeFactory} controlFlowCustomNodeFactory
  24. * @param {IRandomGenerator} randomGenerator
  25. * @param {IOptions} options
  26. */
  27. constructor (
  28. @inject(ServiceIdentifiers.Factory__IControlFlowCustomNode)
  29. controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory,
  30. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  31. @inject(ServiceIdentifiers.IOptions) options: IOptions
  32. ) {
  33. super(controlFlowCustomNodeFactory, randomGenerator, options);
  34. }
  35. /**
  36. * @param {string} controlFlowStorageId
  37. * @param {string} storageKey
  38. * @param {Expression} leftExpression
  39. * @param {Expression} rightExpression
  40. * @returns {Node}
  41. */
  42. protected getControlFlowStorageCallNode (
  43. controlFlowStorageId: string,
  44. storageKey: string,
  45. leftExpression: ESTree.Expression,
  46. rightExpression: ESTree.Expression
  47. ): ESTree.Node {
  48. const controlFlowStorageCallCustomNode: ICustomNode = this.controlFlowCustomNodeFactory(
  49. ControlFlowCustomNode.ExpressionWithOperatorControlFlowStorageCallNode
  50. );
  51. controlFlowStorageCallCustomNode.initialize(controlFlowStorageId, storageKey, leftExpression, rightExpression);
  52. const statementNode: TStatement = controlFlowStorageCallCustomNode.getNode()[0];
  53. if (!statementNode || !Node.isExpressionStatementNode(statementNode)) {
  54. throw new Error(`\`controlFlowStorageCallCustomNode.getNode()[0]\` should returns array with \`ExpressionStatement\` node`);
  55. }
  56. return statementNode.expression;
  57. }
  58. }