Skip to content

Commit be99740

Browse files
authored
Merge pull request javascript-obfuscator#660 from javascript-obfuscator/mangled-shuffled-identifier-names-generator
Fixed runtime errors with mangled-shuffled identifier names generator
2 parents 8f2d891 + b09cf5c commit be99740

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

dist/index.browser.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/generators/identifier-names-generators/MangledIdentifierNamesGenerator.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { inject, injectable } from 'inversify';
1+
import { inject, injectable, postConstruct } from 'inversify';
22
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
33

44
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
55

6+
import { IInitializable } from '../../interfaces/IInitializable';
67
import { IOptions } from '../../interfaces/options/IOptions';
78
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
89

@@ -14,7 +15,12 @@ import { AbstractIdentifierNamesGenerator } from './AbstractIdentifierNamesGener
1415
import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
1516

1617
@injectable()
17-
export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator {
18+
export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator implements IInitializable {
19+
/**
20+
* @type {string[]}
21+
*/
22+
protected static nameSequence: string[];
23+
1824
/**
1925
* @type {string}
2026
*/
@@ -36,11 +42,6 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
3642
'var', 'void', 'with'
3743
]);
3844

39-
/**
40-
* @type {string[]}
41-
*/
42-
protected nameSequence: string[] = `${numbersString}${alphabetString}${alphabetStringUppercase}`.split('');
43-
4445
/**
4546
* @type {string}
4647
*/
@@ -57,6 +58,15 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
5758
super(randomGenerator, options);
5859
}
5960

61+
@postConstruct()
62+
public initialize (): void {
63+
if (!MangledIdentifierNamesGenerator.nameSequence) {
64+
MangledIdentifierNamesGenerator.nameSequence = [
65+
...`${numbersString}${alphabetString}${alphabetStringUppercase}`
66+
];
67+
}
68+
}
69+
6070
/**
6171
* We can only ignore limited nameLength, it has no sense here
6272
* @param {number} nameLength
@@ -134,7 +144,7 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
134144
*/
135145
private generateNewMangledName (previousMangledName: string): string {
136146
const generateNewMangledName: (name: string) => string = (name: string): string => {
137-
const nameSequence: string[] = this.nameSequence;
147+
const nameSequence: string[] = MangledIdentifierNamesGenerator.nameSequence;
138148
const nameSequenceLength: number = nameSequence.length;
139149
const nameLength: number = name.length;
140150

@@ -161,7 +171,9 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
161171
--index;
162172
} while (index >= 0);
163173

164-
return `a${zeroSequence(nameLength)}`;
174+
const firstLetterCharacter: string = nameSequence[numbersString.length];
175+
176+
return `${firstLetterCharacter}${zeroSequence(nameLength)}`;
165177
};
166178

167179
let newMangledName: string = generateNewMangledName(previousMangledName);

src/generators/identifier-names-generators/MangledShuffledIdentifierNamesGenerator.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inject, injectable } from 'inversify';
1+
import { inject, injectable, postConstruct } from 'inversify';
22
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
33

44
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
@@ -31,9 +31,15 @@ export class MangledShuffledIdentifierNamesGenerator extends MangledIdentifierNa
3131
super(randomGenerator, options);
3232

3333
this.arrayUtils = arrayUtils;
34-
this.nameSequence = [
35-
...`${numbersString}`,
36-
...this.arrayUtils.shuffle([...`${alphabetString}${alphabetStringUppercase}`])
37-
];
34+
}
35+
36+
@postConstruct()
37+
public initialize (): void {
38+
if (!MangledIdentifierNamesGenerator.nameSequence) {
39+
MangledIdentifierNamesGenerator.nameSequence = [
40+
...`${numbersString}`,
41+
...this.arrayUtils.shuffle([...`${alphabetString}${alphabetStringUppercase}`])
42+
];
43+
}
3844
}
3945
}

0 commit comments

Comments
 (0)