MangledIdentifierNamesGenerator.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  4. import { IOptions } from '../../interfaces/options/IOptions';
  5. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  6. import { AbstractIdentifierNamesGenerator } from './AbstractIdentifierNamesGenerator';
  7. import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
  8. @injectable()
  9. export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator {
  10. /**
  11. * @type {string}
  12. */
  13. private static readonly initMangledNameCharacter: string = '9';
  14. /**
  15. * @type {WeakMap<TNodeWithLexicalScope, string>}
  16. */
  17. private static readonly lastMangledNameInScopeMap: WeakMap <TNodeWithLexicalScope, string> = new WeakMap();
  18. /**
  19. * @type {string[]}
  20. */
  21. private static readonly nameSequence: string[] = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  22. /**
  23. * Reserved JS words with length of 2-4 symbols that can be possible generated with this replacer
  24. *
  25. * @type {Set<string>}
  26. */
  27. private static readonly reservedNamesSet: Set<string> = new Set([
  28. 'byte', 'case', 'char', 'do', 'else', 'enum', 'eval', 'for', 'goto',
  29. 'if', 'in', 'int', 'let', 'long', 'new', 'null', 'this', 'true', 'try',
  30. 'var', 'void', 'with'
  31. ]);
  32. /**
  33. * @type {string}
  34. */
  35. private previousMangledName: string = MangledIdentifierNamesGenerator.initMangledNameCharacter;
  36. /**
  37. * @param {IRandomGenerator} randomGenerator
  38. * @param {IOptions} options
  39. */
  40. public constructor (
  41. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  42. @inject(ServiceIdentifiers.IOptions) options: IOptions
  43. ) {
  44. super(randomGenerator, options);
  45. }
  46. /**
  47. * We can only ignore limited nameLength, it has no sense here
  48. * @param {number} nameLength
  49. * @returns {string}
  50. */
  51. public generateNext (nameLength?: number): string {
  52. const identifierName: string = this.generateNewMangledName(this.previousMangledName);
  53. this.previousMangledName = identifierName;
  54. this.preserveName(identifierName);
  55. return identifierName;
  56. }
  57. /**
  58. * @param {number} nameLength
  59. * @returns {string}
  60. */
  61. public generateForGlobalScope (nameLength?: number): string {
  62. const prefix: string = this.options.identifiersPrefix ?
  63. `${this.options.identifiersPrefix}`
  64. : '';
  65. const identifierName: string = this.generateNewMangledName(this.previousMangledName);
  66. const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
  67. this.previousMangledName = identifierName;
  68. if (!this.isValidIdentifierName(identifierNameWithPrefix)) {
  69. return this.generateForGlobalScope(nameLength);
  70. }
  71. this.preserveName(identifierNameWithPrefix);
  72. return identifierNameWithPrefix;
  73. }
  74. /**
  75. * @param {TNodeWithLexicalScope} lexicalScopeNode
  76. * @param {number} nameLength
  77. * @returns {string}
  78. */
  79. public generateForLexicalScope (lexicalScopeNode: TNodeWithLexicalScope, nameLength?: number): string {
  80. const lexicalScopes: TNodeWithLexicalScope[] = [
  81. lexicalScopeNode,
  82. ...NodeLexicalScopeUtils.getLexicalScopes(lexicalScopeNode)
  83. ];
  84. const lastMangledNameForScope: string = this.getLastMangledNameForScopes(lexicalScopes);
  85. let identifierName: string = lastMangledNameForScope;
  86. do {
  87. identifierName = this.generateNewMangledName(identifierName);
  88. } while (!this.isValidIdentifierNameInLexicalScopes(identifierName, lexicalScopes));
  89. MangledIdentifierNamesGenerator.lastMangledNameInScopeMap.set(lexicalScopeNode, identifierName);
  90. this.preserveNameForLexicalScope(identifierName, lexicalScopeNode);
  91. return identifierName;
  92. }
  93. /**
  94. * @param {string} mangledName
  95. * @returns {boolean}
  96. */
  97. public isValidIdentifierName (mangledName: string): boolean {
  98. return super.isValidIdentifierName(mangledName)
  99. && !MangledIdentifierNamesGenerator.reservedNamesSet.has(mangledName);
  100. }
  101. /**
  102. * @param {string} previousMangledName
  103. * @returns {string}
  104. */
  105. private generateNewMangledName (previousMangledName: string): string {
  106. const generateNewMangledName: (name: string) => string = (name: string): string => {
  107. const nameSequence: string[] = MangledIdentifierNamesGenerator.nameSequence;
  108. const nameSequenceLength: number = nameSequence.length;
  109. const nameLength: number = name.length;
  110. const zeroSequence: (num: number) => string = (num: number): string => {
  111. return '0'.repeat(num);
  112. };
  113. let index: number = nameLength - 1;
  114. do {
  115. const character: string = name[index];
  116. const indexInSequence: number = nameSequence.indexOf(character);
  117. const lastNameSequenceIndex: number = nameSequenceLength - 1;
  118. if (indexInSequence !== lastNameSequenceIndex) {
  119. const previousNamePart: string = name.substring(0, index);
  120. const nextCharacter: string = nameSequence[indexInSequence + 1];
  121. const zeroSequenceLength: number = nameLength - (index + 1);
  122. const zeroSequenceCharacters: string = zeroSequence(zeroSequenceLength);
  123. return previousNamePart + nextCharacter + zeroSequenceCharacters;
  124. }
  125. --index;
  126. } while (index >= 0);
  127. return `a${zeroSequence(nameLength)}`;
  128. };
  129. let newMangledName: string = generateNewMangledName(previousMangledName);
  130. if (!this.isValidIdentifierName(newMangledName)) {
  131. newMangledName = this.generateNewMangledName(newMangledName);
  132. }
  133. return newMangledName;
  134. }
  135. /**
  136. * @param {TNodeWithLexicalScope[]} lexicalScopeNodes
  137. * @returns {string}
  138. */
  139. private getLastMangledNameForScopes (lexicalScopeNodes: TNodeWithLexicalScope[]): string {
  140. for (const lexicalScope of lexicalScopeNodes) {
  141. const lastMangledName: string | null = MangledIdentifierNamesGenerator.lastMangledNameInScopeMap
  142. .get(lexicalScope) ?? null;
  143. if (!lastMangledName) {
  144. continue;
  145. }
  146. return lastMangledName;
  147. }
  148. return MangledIdentifierNamesGenerator.initMangledNameCharacter;
  149. }
  150. }