Merge pull request #660 from javascript-obfuscator/mangled-shuffled-identifier-names-generator

Fixed runtime errors with mangled-shuffled identifier names generator
This commit is contained in:
Timofey Kachalov
2020-07-10 01:50:41 +03:00
committed by GitHub
5 changed files with 36 additions and 18 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -1,8 +1,9 @@
import { inject, injectable } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
import { IInitializable } from '../../interfaces/IInitializable';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
@@ -14,7 +15,12 @@ import { AbstractIdentifierNamesGenerator } from './AbstractIdentifierNamesGener
import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
@injectable()
export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator {
export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator implements IInitializable {
/**
* @type {string[]}
*/
protected static nameSequence: string[];
/**
* @type {string}
*/
@@ -36,11 +42,6 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
'var', 'void', 'with'
]);
/**
* @type {string[]}
*/
protected nameSequence: string[] = `${numbersString}${alphabetString}${alphabetStringUppercase}`.split('');
/**
* @type {string}
*/
@@ -57,6 +58,15 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
super(randomGenerator, options);
}
@postConstruct()
public initialize (): void {
if (!MangledIdentifierNamesGenerator.nameSequence) {
MangledIdentifierNamesGenerator.nameSequence = [
...`${numbersString}${alphabetString}${alphabetStringUppercase}`
];
}
}
/**
* We can only ignore limited nameLength, it has no sense here
* @param {number} nameLength
@@ -134,7 +144,7 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
*/
private generateNewMangledName (previousMangledName: string): string {
const generateNewMangledName: (name: string) => string = (name: string): string => {
const nameSequence: string[] = this.nameSequence;
const nameSequence: string[] = MangledIdentifierNamesGenerator.nameSequence;
const nameSequenceLength: number = nameSequence.length;
const nameLength: number = name.length;
@@ -161,7 +171,9 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
--index;
} while (index >= 0);
return `a${zeroSequence(nameLength)}`;
const firstLetterCharacter: string = nameSequence[numbersString.length];
return `${firstLetterCharacter}${zeroSequence(nameLength)}`;
};
let newMangledName: string = generateNewMangledName(previousMangledName);
@@ -1,4 +1,4 @@
import { inject, injectable } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
@@ -31,9 +31,15 @@ export class MangledShuffledIdentifierNamesGenerator extends MangledIdentifierNa
super(randomGenerator, options);
this.arrayUtils = arrayUtils;
this.nameSequence = [
...`${numbersString}`,
...this.arrayUtils.shuffle([...`${alphabetString}${alphabetStringUppercase}`])
];
}
@postConstruct()
public initialize (): void {
if (!MangledIdentifierNamesGenerator.nameSequence) {
MangledIdentifierNamesGenerator.nameSequence = [
...`${numbersString}`,
...this.arrayUtils.shuffle([...`${alphabetString}${alphabetStringUppercase}`])
];
}
}
}