AbstractIdentifierNamesGenerator.ts 4.3 KB

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