MangledIdentifierNamesGenerator.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 { ISetUtils } from '../../interfaces/utils/ISetUtils';
  7. import { alphabetString } from '../../constants/AlphabetString';
  8. import { alphabetStringUppercase } from '../../constants/AlphabetStringUppercase';
  9. import { numbersString } from '../../constants/NumbersString';
  10. import { reservedIdentifierNames } from '../../constants/ReservedIdentifierNames';
  11. import { AbstractIdentifierNamesGenerator } from './AbstractIdentifierNamesGenerator';
  12. import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
  13. @injectable()
  14. export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator {
  15. /**
  16. * @type {number}
  17. */
  18. private static readonly maxRegenerationAttempts: number = 20;
  19. /**
  20. * @type {string}
  21. */
  22. private static readonly initMangledNameCharacter: string = '9';
  23. /**
  24. * @type {string[]}
  25. */
  26. private static readonly nameSequence: string[] = [
  27. ...`${numbersString}${alphabetString}${alphabetStringUppercase}`
  28. ];
  29. /**
  30. * Reserved JS words with length of 2-4 symbols that can be possible generated with this replacer
  31. * + reserved DOM names like `Set`, `Map`, `Date`, etc
  32. *
  33. * @type {Set<string>}
  34. */
  35. private static readonly reservedNamesSet: Set<string> = new Set(reservedIdentifierNames);
  36. /**
  37. * @type {string}
  38. */
  39. private lastMangledName: string = MangledIdentifierNamesGenerator.initMangledNameCharacter;
  40. /**
  41. * @type {WeakMap<TNodeWithLexicalScope, string>}
  42. */
  43. private readonly lastMangledNameForScopeMap: WeakMap <TNodeWithLexicalScope, string> = new WeakMap();
  44. /**
  45. * @type {WeakMap<string, string>}
  46. */
  47. private readonly lastMangledNameForLabelMap: Map <string, string> = new Map();
  48. /**
  49. * @type {ISetUtils}
  50. */
  51. private readonly setUtils: ISetUtils;
  52. /**
  53. * @param {IRandomGenerator} randomGenerator
  54. * @param {IOptions} options
  55. * @param {ISetUtils} setUtils
  56. */
  57. public constructor (
  58. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  59. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  60. @inject(ServiceIdentifiers.ISetUtils) setUtils: ISetUtils,
  61. ) {
  62. super(randomGenerator, options);
  63. this.setUtils = setUtils;
  64. }
  65. /**
  66. * Generates next name based on a global previous mangled name
  67. * We can ignore nameLength parameter here, it hasn't sense with this generator
  68. *
  69. * @param {number} nameLength
  70. * @returns {string}
  71. */
  72. public generateNext (nameLength?: number): string {
  73. const identifierName: string = this.generateNewMangledName(this.lastMangledName);
  74. this.updatePreviousMangledName(identifierName);
  75. this.preserveName(identifierName);
  76. return identifierName;
  77. }
  78. /**
  79. * @param {number} nameLength
  80. * @returns {string}
  81. */
  82. public generateForGlobalScope (nameLength?: number): string {
  83. const prefix: string = this.options.identifiersPrefix ?
  84. `${this.options.identifiersPrefix}`
  85. : '';
  86. const identifierName: string = this.generateNewMangledName(
  87. this.lastMangledName,
  88. (newIdentifierName: string) => {
  89. const identifierNameWithPrefix: string = `${prefix}${newIdentifierName}`;
  90. return this.isValidIdentifierName(identifierNameWithPrefix);
  91. }
  92. );
  93. const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
  94. this.updatePreviousMangledName(identifierName);
  95. this.preserveName(identifierNameWithPrefix);
  96. return identifierNameWithPrefix;
  97. }
  98. /**
  99. * @param {TNodeWithLexicalScope} lexicalScopeNode
  100. * @param {number} nameLength
  101. * @returns {string}
  102. */
  103. public generateForLexicalScope (lexicalScopeNode: TNodeWithLexicalScope, nameLength?: number): string {
  104. const lexicalScopes: TNodeWithLexicalScope[] = [
  105. lexicalScopeNode,
  106. ...NodeLexicalScopeUtils.getLexicalScopes(lexicalScopeNode)
  107. ];
  108. const lastMangledNameForScope: string = this.getLastMangledNameForScopes(lexicalScopes);
  109. const identifierName: string = this.generateNewMangledName(
  110. lastMangledNameForScope,
  111. (newIdentifierName: string) =>
  112. this.isValidIdentifierNameInLexicalScopes(newIdentifierName, lexicalScopes)
  113. );
  114. this.lastMangledNameForScopeMap.set(lexicalScopeNode, identifierName);
  115. this.updatePreviousMangledName(identifierName);
  116. this.preserveNameForLexicalScope(identifierName, lexicalScopeNode);
  117. return identifierName;
  118. }
  119. /**
  120. * @param {string} label
  121. * @param {number} nameLength
  122. * @returns {string}
  123. */
  124. public generateForLabel (label: string, nameLength?: number): string {
  125. const lastMangledNameForLabel: string = this.getLastMangledNameForLabel(label);
  126. const identifierName: string = this.generateNewMangledName(lastMangledNameForLabel);
  127. this.updatePreviousMangledNameForLabel(identifierName, label, lastMangledNameForLabel);
  128. return identifierName;
  129. }
  130. /**
  131. * @param {string} nextName
  132. * @param {string} prevName
  133. * @returns {boolean}
  134. */
  135. // eslint-disable-next-line complexity
  136. public isIncrementedMangledName (nextName: string, prevName: string): boolean {
  137. if (nextName === prevName) {
  138. return false;
  139. }
  140. const nextNameLength: number = nextName.length;
  141. const prevNameLength: number = prevName.length;
  142. if (nextNameLength !== prevNameLength) {
  143. return nextNameLength > prevNameLength;
  144. }
  145. const nameSequence: string[] = this.getNameSequence();
  146. for (let i: number = 0; i < nextNameLength; i++) {
  147. const nextNameCharacter: string = nextName[i];
  148. const prevNameCharacter: string = prevName[i];
  149. if (nextNameCharacter === prevNameCharacter) {
  150. continue;
  151. }
  152. const indexOfNextNameCharacter: number = nameSequence.indexOf(nextNameCharacter);
  153. const indexOfPrevNameCharacter: number = nameSequence.indexOf(prevNameCharacter);
  154. return indexOfNextNameCharacter > indexOfPrevNameCharacter;
  155. }
  156. throw new Error('Something goes wrong during comparison of mangled names');
  157. }
  158. /**
  159. * @param {string} mangledName
  160. * @returns {boolean}
  161. */
  162. public override isValidIdentifierName (mangledName: string): boolean {
  163. return super.isValidIdentifierName(mangledName)
  164. && !MangledIdentifierNamesGenerator.reservedNamesSet.has(mangledName);
  165. }
  166. /**
  167. * @returns {string[]}
  168. */
  169. protected getNameSequence (): string[] {
  170. return MangledIdentifierNamesGenerator.nameSequence;
  171. }
  172. /**
  173. * @param {string} name
  174. */
  175. protected updatePreviousMangledName (name: string): void {
  176. if (!this.isIncrementedMangledName(name, this.lastMangledName)) {
  177. return;
  178. }
  179. this.lastMangledName = name;
  180. }
  181. /**
  182. * @param {string} name
  183. * @param {string} label
  184. * @param {string} lastMangledNameForLabel
  185. */
  186. protected updatePreviousMangledNameForLabel (name: string, label: string, lastMangledNameForLabel: string): void {
  187. if (!this.isIncrementedMangledName(name, lastMangledNameForLabel)) {
  188. return;
  189. }
  190. this.lastMangledNameForLabelMap.set(label, name);
  191. }
  192. /**
  193. * @param {string} previousMangledName
  194. * @param {(newIdentifierName: string) => boolean} validationFunction
  195. * @returns {string}
  196. */
  197. protected generateNewMangledName (
  198. previousMangledName: string,
  199. validationFunction?: (newIdentifierName: string) => boolean
  200. ): string {
  201. const generateNewMangledName = (name: string, regenerationAttempt: number = 0): string => {
  202. /**
  203. * Attempt to decrease amount of regeneration tries because of large preserved names set
  204. * When we reached the limit, we're trying to generate next mangled name based on the latest
  205. * preserved name
  206. */
  207. if (regenerationAttempt > MangledIdentifierNamesGenerator.maxRegenerationAttempts) {
  208. const lastPreservedName = this.setUtils.getLastElement(this.preservedNamesSet);
  209. if (lastPreservedName) {
  210. return this.generateNewMangledName(lastPreservedName);
  211. }
  212. }
  213. const nameSequence: string[] = this.getNameSequence();
  214. const nameSequenceLength: number = nameSequence.length;
  215. const nameLength: number = name.length;
  216. const zeroSequence: (num: number) => string = (num: number): string => {
  217. return '0'.repeat(num);
  218. };
  219. let index: number = nameLength - 1;
  220. do {
  221. const character: string = name[index];
  222. const indexInSequence: number = nameSequence.indexOf(character);
  223. const lastNameSequenceIndex: number = nameSequenceLength - 1;
  224. if (indexInSequence !== lastNameSequenceIndex) {
  225. const previousNamePart: string = name.slice(0, index);
  226. const nextCharacter: string = nameSequence[indexInSequence + 1];
  227. const zeroSequenceLength: number = nameLength - (index + 1);
  228. const zeroSequenceCharacters: string = zeroSequence(zeroSequenceLength);
  229. return previousNamePart + nextCharacter + zeroSequenceCharacters;
  230. }
  231. --index;
  232. } while (index >= 0);
  233. const firstLetterCharacter: string = nameSequence[numbersString.length];
  234. return `${firstLetterCharacter}${zeroSequence(nameLength)}`;
  235. };
  236. let identifierName: string = previousMangledName;
  237. let isValidIdentifierName: boolean;
  238. do {
  239. identifierName = generateNewMangledName(identifierName);
  240. isValidIdentifierName = validationFunction?.(identifierName)
  241. ?? this.isValidIdentifierName(identifierName);
  242. } while (!isValidIdentifierName);
  243. return identifierName;
  244. }
  245. /**
  246. * @param {TNodeWithLexicalScope[]} lexicalScopeNodes
  247. * @returns {string}
  248. */
  249. private getLastMangledNameForScopes (lexicalScopeNodes: TNodeWithLexicalScope[]): string {
  250. for (const lexicalScope of lexicalScopeNodes) {
  251. const lastMangledName: string | null = this.lastMangledNameForScopeMap.get(lexicalScope) ?? null;
  252. if (!lastMangledName) {
  253. continue;
  254. }
  255. return lastMangledName;
  256. }
  257. return MangledIdentifierNamesGenerator.initMangledNameCharacter;
  258. }
  259. /**
  260. * @param {string} label
  261. * @returns {string}
  262. */
  263. private getLastMangledNameForLabel (label: string): string {
  264. const lastMangledName: string | null = this.lastMangledNameForLabelMap.get(label) ?? null;
  265. return lastMangledName ?? MangledIdentifierNamesGenerator.initMangledNameCharacter;
  266. }
  267. }