AbstractControlFlowReplacer.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { inject, injectable, } 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 { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  6. import { IControlFlowReplacer } from '../../../interfaces/node-transformers/control-flow-transformers/IControlFlowReplacer';
  7. import { IControlFlowStorage } from '../../../interfaces/storages/control-flow-transformers/IControlFlowStorage';
  8. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  9. import { IIdentifierNamesGenerator } from '../../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  10. import { IOptions } from '../../../interfaces/options/IOptions';
  11. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  12. @injectable()
  13. export abstract class AbstractControlFlowReplacer implements IControlFlowReplacer {
  14. /**
  15. * @type {TControlFlowCustomNodeFactory}
  16. */
  17. protected readonly controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory;
  18. /**
  19. * @type {IIdentifierNamesGenerator}
  20. */
  21. protected readonly identifierNamesGenerator: IIdentifierNamesGenerator;
  22. /**
  23. * @type {IOptions}
  24. */
  25. protected readonly options: IOptions;
  26. /**
  27. * @type {IRandomGenerator}
  28. */
  29. protected readonly randomGenerator: IRandomGenerator;
  30. /**
  31. * @type {Map<string, Map<string, string[]>>}
  32. */
  33. protected readonly replacerDataByControlFlowStorageId: Map <string, Map<string, string[]>> = new Map();
  34. /**
  35. * @param {TControlFlowCustomNodeFactory} controlFlowCustomNodeFactory
  36. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  37. * @param {IRandomGenerator} randomGenerator
  38. * @param {IOptions} options
  39. */
  40. public constructor (
  41. @inject(ServiceIdentifiers.Factory__IControlFlowCustomNode)
  42. controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory,
  43. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  44. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  45. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  46. @inject(ServiceIdentifiers.IOptions) options: IOptions
  47. ) {
  48. this.controlFlowCustomNodeFactory = controlFlowCustomNodeFactory;
  49. this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
  50. this.randomGenerator = randomGenerator;
  51. this.options = options;
  52. }
  53. /**
  54. * Generates storage key with length of 5 characters to prevent collisions and to guarantee that
  55. * these keys will be added to the string array storage
  56. *
  57. * @param {IControlFlowStorage} controlFlowStorage
  58. * @returns {string}
  59. */
  60. public generateStorageKey (controlFlowStorage: IControlFlowStorage): string {
  61. const key: string = this.randomGenerator.getRandomString(5);
  62. if (controlFlowStorage.has(key)) {
  63. return this.generateStorageKey(controlFlowStorage);
  64. }
  65. return key;
  66. }
  67. /**
  68. * @param {ICustomNode} customNode
  69. * @param {IControlFlowStorage} controlFlowStorage
  70. * @param {string} replacerId
  71. * @param {number} usingExistingIdentifierChance
  72. * @returns {string}
  73. */
  74. protected insertCustomNodeToControlFlowStorage (
  75. customNode: ICustomNode,
  76. controlFlowStorage: IControlFlowStorage,
  77. replacerId: string,
  78. usingExistingIdentifierChance: number
  79. ): string {
  80. const controlFlowStorageId: string = controlFlowStorage.getStorageId();
  81. const storageKeysById: Map<string, string[]> = this.replacerDataByControlFlowStorageId.get(controlFlowStorageId)
  82. ?? new Map <string, string[]>();
  83. const storageKeysForCurrentId: string[] | null = storageKeysById.get(replacerId) ?? null;
  84. const shouldPickFromStorageKeysById = this.randomGenerator.getMathRandom() < usingExistingIdentifierChance
  85. && storageKeysForCurrentId?.length;
  86. if (shouldPickFromStorageKeysById) {
  87. return this.randomGenerator.getRandomGenerator().pickone(storageKeysForCurrentId);
  88. }
  89. const storageKey: string = this.generateStorageKey(controlFlowStorage);
  90. storageKeysById.set(replacerId, [storageKey]);
  91. this.replacerDataByControlFlowStorageId.set(controlFlowStorageId, storageKeysById);
  92. controlFlowStorage.set(storageKey, customNode);
  93. return storageKey;
  94. }
  95. /**
  96. * @param {Node} node
  97. * @param {Node} parentNode
  98. * @param {TNodeWithLexicalScope} controlFlowStorageLexicalScopeNode
  99. * @param {IControlFlowStorage} controlFlowStorage
  100. * @returns {Node}
  101. */
  102. public abstract replace (
  103. node: ESTree.Node,
  104. parentNode: ESTree.Node,
  105. controlFlowStorage: IControlFlowStorage
  106. ): ESTree.Node;
  107. }