BinaryExpressionControlFlowReplacer.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { TCustomNodeFactory } from '../../../types/container/TCustomNodeFactory';
  5. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  6. import { IOptions } from '../../../interfaces/options/IOptions';
  7. import { IStorage } from '../../../interfaces/storages/IStorage';
  8. import { CustomNodes } from '../../../enums/container/CustomNodes';
  9. import { ExpressionWithOperatorControlFlowReplacer } from './ExpressionWithOperatorControlFlowReplacer';
  10. @injectable()
  11. export class BinaryExpressionControlFlowReplacer extends ExpressionWithOperatorControlFlowReplacer {
  12. /**
  13. * @type {number}
  14. */
  15. private static readonly usingExistingIdentifierChance: number = 0.5;
  16. /**
  17. * @param customNodeFactory
  18. * @param options
  19. */
  20. constructor (
  21. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  22. @inject(ServiceIdentifiers.IOptions) options: IOptions
  23. ) {
  24. super(customNodeFactory, options);
  25. }
  26. /**
  27. * @param binaryExpressionNode
  28. * @param parentNode
  29. * @param controlFlowStorage
  30. * @returns {ESTree.Node}
  31. */
  32. public replace (
  33. binaryExpressionNode: ESTree.BinaryExpression,
  34. parentNode: ESTree.Node,
  35. controlFlowStorage: IStorage <ICustomNode>
  36. ): ESTree.Node {
  37. const replacerId: string = binaryExpressionNode.operator;
  38. const binaryExpressionFunctionCustomNode: ICustomNode = this.customNodeFactory(CustomNodes.BinaryExpressionFunctionNode);
  39. binaryExpressionFunctionCustomNode.initialize(replacerId);
  40. const storageKey: string = this.insertCustomNodeToControlFlowStorage(
  41. binaryExpressionFunctionCustomNode,
  42. controlFlowStorage,
  43. replacerId,
  44. BinaryExpressionControlFlowReplacer.usingExistingIdentifierChance
  45. );
  46. return this.getControlFlowStorageCallNode(
  47. controlFlowStorage.getStorageId(),
  48. storageKey,
  49. binaryExpressionNode.left,
  50. binaryExpressionNode.right
  51. );
  52. }
  53. }