Skip to content

Commit 19696ec

Browse files
committed
Use block scope instead node identifier
1 parent 6311cca commit 19696ec

File tree

14 files changed

+29028
-139
lines changed

14 files changed

+29028
-139
lines changed

dist/index.cli.js

Lines changed: 14801 additions & 1 deletion
Large diffs are not rendered by default.

dist/index.js

Lines changed: 14061 additions & 1 deletion
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import * as ESTree from 'estree';
22

3+
import { TNodeWithBlockScope } from '../../../../types/node/TNodeWithBlockScope';
4+
35
import { IObfuscatingReplacer } from './IObfuscatingReplacer';
46

57
export interface IIdentifierObfuscatingReplacer extends IObfuscatingReplacer <ESTree.Identifier> {
68
/**
79
* @param {string} nodeValue
8-
* @param {number} nodeIdentifier
10+
* @param {TNodeWithBlockScope} blockScopeNode
911
*/
10-
storeGlobalName (nodeValue: string, nodeIdentifier: number): void;
12+
storeGlobalName (nodeValue: string, blockScopeNode: TNodeWithBlockScope): void;
1113

1214
/**
1315
* @param {string} nodeValue
14-
* @param {number} nodeIdentifier
16+
* @param {TNodeWithBlockScope} blockScopeNode
1517
*/
16-
storeLocalName (nodeValue: string, nodeIdentifier: number): void;
18+
storeLocalName (nodeValue: string, blockScopeNode: TNodeWithBlockScope): void;
1719
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as ESTree from 'estree';
22

3+
import { TNodeWithBlockScope } from '../../../../types/node/TNodeWithBlockScope';
4+
35
export interface IObfuscatingReplacer <T = ESTree.Node> {
46
/**
5-
* @param {SimpleLiteral['value']} nodeValue
7+
* @param {SimpleLiteral["value"]} nodeValue
8+
* @param {TNodeWithBlockScope} blockScopeNode
69
* @param {number} nodeIdentifier
710
* @returns {T}
811
*/
9-
replace (nodeValue: ESTree.SimpleLiteral['value'], nodeIdentifier?: number): T;
12+
replace (nodeValue: ESTree.SimpleLiteral['value'], blockScopeNode?: TNodeWithBlockScope, nodeIdentifier?: number): T;
1013
}

src/node-transformers/AbstractNodeTransformer.ts

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

44
import * as estraverse from 'estraverse';
55
import * as ESTree from 'estree';
66

7-
import { IInitializable } from '../interfaces/IInitializable';
87
import { INodeTransformer } from '../interfaces/node-transformers/INodeTransformer';
98
import { IOptions } from '../interfaces/options/IOptions';
109
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
1110
import { IVisitor } from '../interfaces/node-transformers/IVisitor';
1211

13-
import { initializable } from '../decorators/Initializable';
14-
1512
import { TransformationStage } from '../enums/node-transformers/TransformationStage';
1613

1714
@injectable()
18-
export abstract class AbstractNodeTransformer implements INodeTransformer, IInitializable {
19-
/**
20-
* @type {number}
21-
*/
22-
@initializable()
23-
protected nodeIdentifier!: number;
24-
15+
export abstract class AbstractNodeTransformer implements INodeTransformer {
2516
/**
2617
* @type {IOptions}
2718
*/
@@ -44,11 +35,6 @@ export abstract class AbstractNodeTransformer implements INodeTransformer, IInit
4435
this.options = options;
4536
}
4637

47-
@postConstruct()
48-
public initialize (): void {
49-
this.nodeIdentifier = this.randomGenerator.getRandomInteger(0, 10000);
50-
}
51-
5238
/**
5339
* @param {TransformationStage} transformationStage
5440
* @returns {IVisitor | null}

src/node-transformers/obfuscating-transformers/CatchClauseTransformer.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as estraverse from 'estraverse';
55
import * as ESTree from 'estree';
66

77
import { TIdentifierObfuscatingReplacerFactory } from '../../types/container/node-transformers/TIdentifierObfuscatingReplacerFactory';
8+
import { TNodeWithBlockScope } from '../../types/node/TNodeWithBlockScope';
89

910
import { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer';
1011
import { IOptions } from '../../interfaces/options/IOptions';
@@ -17,6 +18,7 @@ import { TransformationStage } from '../../enums/node-transformers/Transformatio
1718
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
1819
import { NodeGuards } from '../../node/NodeGuards';
1920
import { NodeMetadata } from '../../node/NodeMetadata';
21+
import { NodeUtils } from '../../node/NodeUtils';
2022

2123
/**
2224
* replaces:
@@ -77,34 +79,40 @@ export class CatchClauseTransformer extends AbstractNodeTransformer {
7779
* @returns {NodeGuards}
7880
*/
7981
public transformNode (catchClauseNode: ESTree.CatchClause, parentNode: ESTree.Node): ESTree.Node {
80-
const nodeIdentifier: number = this.nodeIdentifier++;
82+
const blockScopeNode: TNodeWithBlockScope = NodeUtils.getBlockScopesOfNode(catchClauseNode)[0];
8183

82-
this.storeCatchClauseParam(catchClauseNode, nodeIdentifier);
83-
this.replaceCatchClauseParam(catchClauseNode, nodeIdentifier);
84+
this.storeCatchClauseParam(catchClauseNode, blockScopeNode);
85+
this.replaceCatchClauseParam(catchClauseNode, blockScopeNode);
8486

8587
return catchClauseNode;
8688
}
8789

8890
/**
8991
* @param {CatchClause} catchClauseNode
90-
* @param {number} nodeIdentifier
92+
* @param {TNodeWithBlockScope} blockScopeNode
9193
*/
92-
private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: number): void {
94+
private storeCatchClauseParam (
95+
catchClauseNode: ESTree.CatchClause,
96+
blockScopeNode: TNodeWithBlockScope
97+
): void {
9398
if (NodeGuards.isIdentifierNode(catchClauseNode.param)) {
94-
this.identifierObfuscatingReplacer.storeLocalName(catchClauseNode.param.name, nodeIdentifier);
99+
this.identifierObfuscatingReplacer.storeLocalName(catchClauseNode.param.name, blockScopeNode);
95100
}
96101
}
97102

98103
/**
99104
* @param {CatchClause} catchClauseNode
100-
* @param {number} nodeIdentifier
105+
* @param {TNodeWithBlockScope} blockScopeNode
101106
*/
102-
private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: number): void {
107+
private replaceCatchClauseParam (
108+
catchClauseNode: ESTree.CatchClause,
109+
blockScopeNode: TNodeWithBlockScope
110+
): void {
103111
estraverse.replace(catchClauseNode, {
104112
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
105113
if (parentNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
106114
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
107-
.replace(node.name, nodeIdentifier);
115+
.replace(node.name, blockScopeNode);
108116
const newIdentifierName: string = newIdentifier.name;
109117

110118
if (node.name !== newIdentifierName) {

src/node-transformers/obfuscating-transformers/ClassDeclarationTransformer.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,53 +90,52 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
9090
* @returns {NodeGuards}
9191
*/
9292
public transformNode (classDeclarationNode: ESTree.ClassDeclaration, parentNode: ESTree.Node): ESTree.Node {
93-
const nodeIdentifier: number = this.nodeIdentifier++;
9493
const blockScopeNode: TNodeWithBlockScope = NodeUtils.getBlockScopesOfNode(classDeclarationNode)[0];
9594
const isGlobalDeclaration: boolean = blockScopeNode.type === NodeType.Program;
9695

9796
if (!this.options.renameGlobals && isGlobalDeclaration) {
9897
return classDeclarationNode;
9998
}
10099

101-
this.storeClassName(classDeclarationNode, isGlobalDeclaration, nodeIdentifier);
100+
this.storeClassName(classDeclarationNode, blockScopeNode, isGlobalDeclaration);
102101

103102
// check for cached identifiers for current scope node. If exist - loop through them.
104103
if (this.replaceableIdentifiers.has(blockScopeNode)) {
105-
this.replaceScopeCachedIdentifiers(blockScopeNode, nodeIdentifier);
104+
this.replaceScopeCachedIdentifiers(blockScopeNode);
106105
} else {
107-
this.replaceScopeIdentifiers(blockScopeNode, nodeIdentifier);
106+
this.replaceScopeIdentifiers(blockScopeNode);
108107
}
109108

110109
return classDeclarationNode;
111110
}
112111

113112
/**
114113
* @param {ClassDeclaration} classDeclarationNode
114+
* @param {TNodeWithBlockScope} blockScopeNode
115115
* @param {boolean} isGlobalDeclaration
116-
* @param {number} nodeIdentifier
117116
*/
118117
private storeClassName (
119118
classDeclarationNode: ESTree.ClassDeclaration,
120-
isGlobalDeclaration: boolean,
121-
nodeIdentifier: number
119+
blockScopeNode: TNodeWithBlockScope,
120+
isGlobalDeclaration: boolean
122121
): void {
123122
if (isGlobalDeclaration) {
124-
this.identifierObfuscatingReplacer.storeGlobalName(classDeclarationNode.id.name, nodeIdentifier);
123+
this.identifierObfuscatingReplacer.storeGlobalName(classDeclarationNode.id.name, blockScopeNode);
125124
} else {
126-
this.identifierObfuscatingReplacer.storeLocalName(classDeclarationNode.id.name, nodeIdentifier);
125+
this.identifierObfuscatingReplacer.storeLocalName(classDeclarationNode.id.name, blockScopeNode);
127126
}
128127
}
129128

130129
/**
131130
* @param {TNodeWithBlockScope} blockScopeNode
132-
* @param {number} nodeIdentifier
133131
*/
134-
private replaceScopeCachedIdentifiers (blockScopeNode: TNodeWithBlockScope, nodeIdentifier: number): void {
135-
const cachedReplaceableIdentifiers: ESTree.Identifier[] = <ESTree.Identifier[]>this.replaceableIdentifiers.get(blockScopeNode);
132+
private replaceScopeCachedIdentifiers (blockScopeNode: TNodeWithBlockScope): void {
133+
const cachedReplaceableIdentifiers: ESTree.Identifier[] =
134+
<ESTree.Identifier[]>this.replaceableIdentifiers.get(blockScopeNode);
136135

137136
cachedReplaceableIdentifiers.forEach((replaceableIdentifier: ESTree.Identifier) => {
138137
const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
139-
.replace(replaceableIdentifier.name, nodeIdentifier);
138+
.replace(replaceableIdentifier.name, blockScopeNode);
140139

141140
replaceableIdentifier.name = newReplaceableIdentifier.name;
142141
NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
@@ -145,9 +144,8 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
145144

146145
/**
147146
* @param {TNodeWithBlockScope} blockScopeNode
148-
* @param {number} nodeIdentifier
149147
*/
150-
private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockScope, nodeIdentifier: number): void {
148+
private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockScope): void {
151149
const storedReplaceableIdentifiers: ESTree.Identifier[] = [];
152150

153151
estraverse.replace(blockScopeNode, {
@@ -158,7 +156,7 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
158156
&& !NodeMetadata.isRenamedIdentifier(node)
159157
) {
160158
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
161-
.replace(node.name, nodeIdentifier);
159+
.replace(node.name, blockScopeNode);
162160
const newIdentifierName: string = newIdentifier.name;
163161

164162
if (node.name !== newIdentifierName) {

src/node-transformers/obfuscating-transformers/FunctionDeclarationTransformer.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,54 +92,52 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
9292
* @returns {NodeGuards}
9393
*/
9494
public transformNode (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): ESTree.Node {
95-
const nodeIdentifier: number = this.nodeIdentifier++;
9695
const blockScopeNode: TNodeWithBlockScope = NodeUtils.getBlockScopesOfNode(functionDeclarationNode)[0];
9796
const isGlobalDeclaration: boolean = blockScopeNode.type === NodeType.Program;
9897

9998
if (!this.options.renameGlobals && isGlobalDeclaration) {
10099
return functionDeclarationNode;
101100
}
102101

103-
this.storeFunctionName(functionDeclarationNode, isGlobalDeclaration, nodeIdentifier);
102+
this.storeFunctionName(functionDeclarationNode, blockScopeNode, isGlobalDeclaration);
104103

105104
// check for cached identifiers for current scope node. If exist - loop through them.
106105
if (this.replaceableIdentifiers.has(blockScopeNode)) {
107-
this.replaceScopeCachedIdentifiers(functionDeclarationNode, blockScopeNode, nodeIdentifier);
106+
this.replaceScopeCachedIdentifiers(functionDeclarationNode, blockScopeNode);
108107
} else {
109-
this.replaceScopeIdentifiers(blockScopeNode, nodeIdentifier);
108+
this.replaceScopeIdentifiers(blockScopeNode);
110109
}
111110

112111
return functionDeclarationNode;
113112
}
114113

115114
/**
116115
* @param {FunctionDeclaration} functionDeclarationNode
116+
* @param {TNodeWithBlockScope} blockScopeNode
117117
* @param {boolean} isGlobalDeclaration
118-
* @param {number} nodeIdentifier
119118
*/
120119
private storeFunctionName (
121120
functionDeclarationNode: ESTree.FunctionDeclaration,
122-
isGlobalDeclaration: boolean,
123-
nodeIdentifier: number
121+
blockScopeNode: TNodeWithBlockScope,
122+
isGlobalDeclaration: boolean
124123
): void {
125124
if (isGlobalDeclaration) {
126-
this.identifierObfuscatingReplacer.storeGlobalName(functionDeclarationNode.id.name, nodeIdentifier);
125+
this.identifierObfuscatingReplacer.storeGlobalName(functionDeclarationNode.id.name, blockScopeNode);
127126
} else {
128-
this.identifierObfuscatingReplacer.storeLocalName(functionDeclarationNode.id.name, nodeIdentifier);
127+
this.identifierObfuscatingReplacer.storeLocalName(functionDeclarationNode.id.name, blockScopeNode);
129128
}
130129
}
131130

132131
/**
133132
* @param {FunctionDeclaration} functionDeclarationNode
134133
* @param {TNodeWithBlockScope} blockScopeNode
135-
* @param {number} nodeIdentifier
136134
*/
137135
private replaceScopeCachedIdentifiers (
138136
functionDeclarationNode: ESTree.FunctionDeclaration,
139-
blockScopeNode: TNodeWithBlockScope,
140-
nodeIdentifier: number
137+
blockScopeNode: TNodeWithBlockScope
141138
): void {
142-
const cachedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames | undefined = this.replaceableIdentifiers.get(blockScopeNode);
139+
const cachedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames | undefined =
140+
this.replaceableIdentifiers.get(blockScopeNode);
143141

144142
if (!cachedReplaceableIdentifiersNamesMap) {
145143
return;
@@ -157,7 +155,7 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
157155
for (let i: number = 0; i < cachedReplaceableIdentifierLength; i++) {
158156
const replaceableIdentifier: ESTree.Identifier = cachedReplaceableIdentifiers[i];
159157
const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
160-
.replace(replaceableIdentifier.name, nodeIdentifier);
158+
.replace(replaceableIdentifier.name, blockScopeNode);
161159

162160
replaceableIdentifier.name = newReplaceableIdentifier.name;
163161
NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
@@ -166,9 +164,8 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
166164

167165
/**
168166
* @param {TNodeWithBlockScope} blockScopeNode
169-
* @param {number} nodeIdentifier
170167
*/
171-
private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockScope, nodeIdentifier: number): void {
168+
private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockScope): void {
172169
const storedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames = new Map();
173170

174171
estraverse.replace(blockScopeNode, {
@@ -179,14 +176,15 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
179176
&& !NodeMetadata.isRenamedIdentifier(node)
180177
) {
181178
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
182-
.replace(node.name, nodeIdentifier);
179+
.replace(node.name, blockScopeNode);
183180
const newIdentifierName: string = newIdentifier.name;
184181

185182
if (node.name !== newIdentifierName) {
186183
node.name = newIdentifierName;
187184
NodeMetadata.set(node, { renamedIdentifier: true });
188185
} else {
189-
const storedReplaceableIdentifiers: ESTree.Identifier[] = storedReplaceableIdentifiersNamesMap.get(node.name) || [];
186+
const storedReplaceableIdentifiers: ESTree.Identifier[] =
187+
storedReplaceableIdentifiersNamesMap.get(node.name) || [];
190188

191189
storedReplaceableIdentifiers.push(node);
192190
storedReplaceableIdentifiersNamesMap.set(node.name, storedReplaceableIdentifiers);

0 commit comments

Comments
 (0)