AbstractIdentifierNamesGenerator.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  4. import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  5. import { IOptions } from '../../interfaces/options/IOptions';
  6. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  7. @injectable()
  8. export abstract class AbstractIdentifierNamesGenerator implements IIdentifierNamesGenerator {
  9. /**
  10. * @type {IOptions}
  11. */
  12. protected readonly options: IOptions;
  13. /**
  14. * @type {IRandomGenerator}
  15. */
  16. protected readonly randomGenerator: IRandomGenerator;
  17. /**
  18. * @type {Set<string>}
  19. */
  20. protected readonly preservedNamesSet: Set<string> = new Set();
  21. /**
  22. * @type {Map<TNodeWithLexicalScope, Set<string>>}
  23. */
  24. protected readonly lexicalScopesPreservedNamesMap: Map<TNodeWithLexicalScope, Set<string>> = new Map();
  25. /**
  26. * @param {IRandomGenerator} randomGenerator
  27. * @param {IOptions} options
  28. */
  29. public constructor (
  30. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  31. @inject(ServiceIdentifiers.IOptions) options: IOptions
  32. ) {
  33. this.randomGenerator = randomGenerator;
  34. this.options = options;
  35. }
  36. /**
  37. * @param {string} name
  38. */
  39. public preserveName (name: string): void {
  40. this.preservedNamesSet.add(name);
  41. }
  42. /**
  43. * @param {string} name
  44. * @param {TNodeWithLexicalScope} lexicalScopeNode
  45. */
  46. public preserveNameForLexicalScope (name: string, lexicalScopeNode: TNodeWithLexicalScope): void {
  47. const preservedNamesForLexicalScopeSet: Set<string> =
  48. this.lexicalScopesPreservedNamesMap.get(lexicalScopeNode) ?? new Set();
  49. preservedNamesForLexicalScopeSet.add(name);
  50. this.lexicalScopesPreservedNamesMap.set(lexicalScopeNode, preservedNamesForLexicalScopeSet);
  51. }
  52. /**
  53. * @param {string} name
  54. * @returns {boolean}
  55. */
  56. public isValidIdentifierName (name: string): boolean {
  57. return this.notReservedName(name) && !this.preservedNamesSet.has(name);
  58. }
  59. /**
  60. * @param {string} name
  61. * @param {TNodeWithLexicalScope[]} lexicalScopeNodes
  62. * @returns {boolean}
  63. */
  64. public isValidIdentifierNameInLexicalScopes (name: string, lexicalScopeNodes: TNodeWithLexicalScope[]): boolean {
  65. if (!this.isValidIdentifierName(name)) {
  66. return false;
  67. }
  68. for (const lexicalScope of lexicalScopeNodes) {
  69. const preservedNamesForLexicalScopeSet: Set<string> | null =
  70. this.lexicalScopesPreservedNamesMap.get(lexicalScope) ?? null;
  71. if (!preservedNamesForLexicalScopeSet) {
  72. continue;
  73. }
  74. if (preservedNamesForLexicalScopeSet.has(name)) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. /**
  81. * @param {string} name
  82. * @returns {boolean}
  83. */
  84. private notReservedName (name: string): boolean {
  85. return this.options.reservedNames.length
  86. ? !this.options.reservedNames.some((reservedName: string) =>
  87. new RegExp(reservedName, 'g').exec(name) !== null
  88. )
  89. : true;
  90. }
  91. /**
  92. * @param {number} nameLength
  93. * @returns {string}
  94. */
  95. public abstract generate (nameLength?: number): string;
  96. /**
  97. * @param {TNodeWithLexicalScope} lexicalScopeNode
  98. * @param {number} nameLength
  99. * @returns {string}
  100. */
  101. public abstract generateForLexicalScope (lexicalScopeNode: TNodeWithLexicalScope, nameLength?: number): string;
  102. /**
  103. * @param {number} nameLength
  104. * @returns {string}
  105. */
  106. public abstract generateWithPrefix (nameLength?: number): string;
  107. }