js-confuser 2.0.0-alpha.5 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.md +43 -43
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
- package/.github/workflows/node.js.yml +28 -28
- package/.prettierrc +4 -4
- package/CHANGELOG.md +1015 -989
- package/CODE_OF_CONDUCT.md +131 -131
- package/CONTRIBUTING.md +52 -52
- package/LICENSE +21 -21
- package/Migration.md +72 -71
- package/README.md +86 -78
- package/dist/constants.js +43 -43
- package/dist/index.js +14 -23
- package/dist/obfuscator.js +31 -25
- package/dist/order.js +4 -4
- package/dist/presets.js +31 -31
- package/dist/templates/integrityTemplate.js +4 -4
- package/dist/templates/template.js +1 -2
- package/dist/transforms/astScrambler.js +1 -2
- package/dist/transforms/calculator.js +1 -2
- package/dist/transforms/controlFlowFlattening.js +93 -63
- package/dist/transforms/deadCode.js +1 -2
- package/dist/transforms/dispatcher.js +4 -5
- package/dist/transforms/extraction/duplicateLiteralsRemoval.js +1 -2
- package/dist/transforms/extraction/objectExtraction.js +1 -2
- package/dist/transforms/finalizer.js +1 -2
- package/dist/transforms/flatten.js +1 -2
- package/dist/transforms/identifier/globalConcealing.js +15 -2
- package/dist/transforms/identifier/movedDeclarations.js +8 -7
- package/dist/transforms/identifier/renameVariables.js +7 -7
- package/dist/transforms/lock/integrity.js +11 -10
- package/dist/transforms/lock/lock.js +2 -3
- package/dist/transforms/minify.js +11 -29
- package/dist/transforms/opaquePredicates.js +1 -2
- package/dist/transforms/pack.js +5 -2
- package/dist/transforms/plugin.js +18 -19
- package/dist/transforms/preparation.js +16 -16
- package/dist/transforms/renameLabels.js +1 -2
- package/dist/transforms/rgf.js +8 -9
- package/dist/transforms/shuffle.js +1 -2
- package/dist/transforms/string/encoding.js +1 -2
- package/dist/transforms/string/stringCompression.js +3 -4
- package/dist/transforms/string/stringConcealing.js +8 -3
- package/dist/transforms/string/stringEncoding.js +1 -2
- package/dist/transforms/variableMasking.js +1 -2
- package/dist/utils/NameGen.js +2 -2
- package/dist/utils/PredicateGen.js +1 -2
- package/dist/utils/ast-utils.js +87 -88
- package/dist/utils/function-utils.js +8 -8
- package/dist/utils/node.js +5 -6
- package/dist/utils/object-utils.js +4 -4
- package/dist/utils/random-utils.js +20 -20
- package/dist/utils/static-utils.js +1 -2
- package/dist/validateOptions.js +4 -7
- package/index.d.ts +17 -17
- package/package.json +61 -59
- package/src/constants.ts +168 -168
- package/src/index.ts +118 -118
- package/src/obfuscationResult.ts +49 -49
- package/src/obfuscator.ts +501 -497
- package/src/options.ts +407 -407
- package/src/order.ts +54 -54
- package/src/presets.ts +125 -125
- package/src/templates/bufferToStringTemplate.ts +57 -57
- package/src/templates/deadCodeTemplates.ts +1185 -1185
- package/src/templates/getGlobalTemplate.ts +76 -76
- package/src/templates/integrityTemplate.ts +64 -64
- package/src/templates/setFunctionLengthTemplate.ts +11 -11
- package/src/templates/stringCompressionTemplate.ts +20 -20
- package/src/templates/tamperProtectionTemplates.ts +120 -120
- package/src/templates/template.ts +224 -224
- package/src/transforms/astScrambler.ts +99 -99
- package/src/transforms/calculator.ts +99 -99
- package/src/transforms/controlFlowFlattening.ts +1716 -1664
- package/src/transforms/deadCode.ts +82 -82
- package/src/transforms/dispatcher.ts +450 -450
- package/src/transforms/extraction/duplicateLiteralsRemoval.ts +156 -158
- package/src/transforms/extraction/objectExtraction.ts +186 -186
- package/src/transforms/finalizer.ts +74 -74
- package/src/transforms/flatten.ts +421 -420
- package/src/transforms/identifier/globalConcealing.ts +315 -295
- package/src/transforms/identifier/movedDeclarations.ts +252 -251
- package/src/transforms/identifier/renameVariables.ts +328 -321
- package/src/transforms/lock/integrity.ts +117 -114
- package/src/transforms/lock/lock.ts +418 -425
- package/src/transforms/minify.ts +615 -629
- package/src/transforms/opaquePredicates.ts +100 -100
- package/src/transforms/pack.ts +239 -231
- package/src/transforms/plugin.ts +173 -173
- package/src/transforms/preparation.ts +349 -347
- package/src/transforms/renameLabels.ts +175 -175
- package/src/transforms/rgf.ts +322 -322
- package/src/transforms/shuffle.ts +82 -82
- package/src/transforms/string/encoding.ts +144 -144
- package/src/transforms/string/stringCompression.ts +128 -128
- package/src/transforms/string/stringConcealing.ts +312 -298
- package/src/transforms/string/stringEncoding.ts +80 -80
- package/src/transforms/string/stringSplitting.ts +77 -77
- package/src/transforms/variableMasking.ts +257 -257
- package/src/utils/IntGen.ts +33 -33
- package/src/utils/NameGen.ts +116 -116
- package/src/utils/PredicateGen.ts +61 -61
- package/src/utils/ast-utils.ts +663 -663
- package/src/utils/function-utils.ts +50 -50
- package/src/utils/gen-utils.ts +48 -48
- package/src/utils/node.ts +78 -78
- package/src/utils/object-utils.ts +21 -21
- package/src/utils/random-utils.ts +93 -93
- package/src/utils/static-utils.ts +66 -66
- package/src/validateOptions.ts +256 -259
- package/tsconfig.json +13 -14
- package/dist/probability.js +0 -1
- package/dist/transforms/functionOutlining.js +0 -230
- package/dist/utils/ControlObject.js +0 -125
|
@@ -1,321 +1,328 @@
|
|
|
1
|
-
import { NodePath } from "@babel/traverse";
|
|
2
|
-
import { Visitor } from "@babel/traverse";
|
|
3
|
-
import { PluginArg, PluginObject } from "../plugin";
|
|
4
|
-
import * as t from "@babel/types";
|
|
5
|
-
import { Order } from "../../order";
|
|
6
|
-
import {
|
|
7
|
-
noRenameVariablePrefix,
|
|
8
|
-
placeholderVariablePrefix,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
let
|
|
53
|
-
|
|
54
|
-
if (path.isBindingIdentifier() && isDefiningIdentifier(path)) {
|
|
55
|
-
isDefined = true;
|
|
56
|
-
const binding = path.scope.getBinding(path.node.name);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
path.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
)
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
1
|
+
import { NodePath } from "@babel/traverse";
|
|
2
|
+
import { Visitor } from "@babel/traverse";
|
|
3
|
+
import { PluginArg, PluginObject } from "../plugin";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import { Order } from "../../order";
|
|
6
|
+
import {
|
|
7
|
+
noRenameVariablePrefix,
|
|
8
|
+
placeholderVariablePrefix,
|
|
9
|
+
WITH_STATEMENT,
|
|
10
|
+
} from "../../constants";
|
|
11
|
+
import {
|
|
12
|
+
getParentFunctionOrProgram,
|
|
13
|
+
isDefiningIdentifier,
|
|
14
|
+
isExportedIdentifier,
|
|
15
|
+
isVariableIdentifier,
|
|
16
|
+
} from "../../utils/ast-utils";
|
|
17
|
+
import { isVariableFunctionIdentifier } from "../../utils/function-utils";
|
|
18
|
+
|
|
19
|
+
const RENAMED = Symbol("Renamed");
|
|
20
|
+
|
|
21
|
+
const reusePreviousNames = true;
|
|
22
|
+
|
|
23
|
+
export default ({ Plugin }: PluginArg): PluginObject => {
|
|
24
|
+
const me = Plugin(Order.RenameVariables, {
|
|
25
|
+
changeData: {
|
|
26
|
+
variables: 0,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const definedMap = new Map<t.Node, Set<string>>();
|
|
31
|
+
const referencedMap = new Map<t.Node, Set<string>>();
|
|
32
|
+
const paramMap = new Map<t.Node, Set<string>>(); // Used for default function parameter special case
|
|
33
|
+
const bindingMap = new Map<t.Node, Map<string, NodePath<t.Identifier>>>();
|
|
34
|
+
|
|
35
|
+
const renamedVariables = new Map<t.Node, Map<string, string>>();
|
|
36
|
+
me.obfuscator.globalState.renamedVariables = renamedVariables;
|
|
37
|
+
|
|
38
|
+
const generated = Array.from(me.obfuscator.nameGen.generatedNames);
|
|
39
|
+
|
|
40
|
+
const VariableAnalysisVisitor: Visitor = {
|
|
41
|
+
Program: {
|
|
42
|
+
enter(path) {
|
|
43
|
+
// Analyze all scopes
|
|
44
|
+
path.traverse({
|
|
45
|
+
Identifier(path) {
|
|
46
|
+
if (!isVariableIdentifier(path)) return;
|
|
47
|
+
|
|
48
|
+
let contextPaths: NodePath<t.Node>[] = [
|
|
49
|
+
getParentFunctionOrProgram(path),
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
let isDefined = false;
|
|
53
|
+
|
|
54
|
+
if (path.isBindingIdentifier() && isDefiningIdentifier(path)) {
|
|
55
|
+
isDefined = true;
|
|
56
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
57
|
+
|
|
58
|
+
// Function ID is defined in the parent's function declaration
|
|
59
|
+
if (
|
|
60
|
+
path.key === "id" &&
|
|
61
|
+
path.parentPath.isFunctionDeclaration()
|
|
62
|
+
) {
|
|
63
|
+
contextPaths = [getParentFunctionOrProgram(path.parentPath)];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (
|
|
67
|
+
binding &&
|
|
68
|
+
binding.kind === "let" &&
|
|
69
|
+
binding.path.key === "handler" &&
|
|
70
|
+
binding.path.isCatchClause()
|
|
71
|
+
) {
|
|
72
|
+
contextPaths = [path];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
contextPaths.forEach((contextPath) => {
|
|
77
|
+
// console.log(contextPath.node.type, path.node.name, isDefined);
|
|
78
|
+
|
|
79
|
+
if (isDefined) {
|
|
80
|
+
// Add to defined map
|
|
81
|
+
if (!definedMap.has(contextPath.node)) {
|
|
82
|
+
definedMap.set(contextPath.node, new Set());
|
|
83
|
+
}
|
|
84
|
+
definedMap.get(contextPath.node).add(path.node.name);
|
|
85
|
+
|
|
86
|
+
if (!bindingMap.has(contextPath.node)) {
|
|
87
|
+
bindingMap.set(contextPath.node, new Map());
|
|
88
|
+
}
|
|
89
|
+
bindingMap.get(contextPath.node).set(path.node.name, path);
|
|
90
|
+
} else {
|
|
91
|
+
// Add to reference map
|
|
92
|
+
if (!referencedMap.has(contextPath.node)) {
|
|
93
|
+
referencedMap.set(contextPath.node, new Set());
|
|
94
|
+
}
|
|
95
|
+
referencedMap.get(contextPath.node).add(path.node.name);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
//
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const VariableRenamingVisitor: Visitor = {
|
|
107
|
+
Identifier(identifierPath) {
|
|
108
|
+
if (!isVariableIdentifier(identifierPath)) return;
|
|
109
|
+
const node = identifierPath.node;
|
|
110
|
+
const identifierName = node.name;
|
|
111
|
+
|
|
112
|
+
if (node[RENAMED]) return;
|
|
113
|
+
if (node[WITH_STATEMENT]) return; // Exclude CFF identifiers
|
|
114
|
+
|
|
115
|
+
var contextPaths: NodePath[] = identifierPath.getAncestry();
|
|
116
|
+
|
|
117
|
+
// A Function ID is not in the same context as it's body
|
|
118
|
+
if (
|
|
119
|
+
identifierPath.key === "id" &&
|
|
120
|
+
identifierPath.parentPath.isFunctionDeclaration()
|
|
121
|
+
) {
|
|
122
|
+
contextPaths = contextPaths.filter(
|
|
123
|
+
(x) => x !== identifierPath.parentPath
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
var newName = null;
|
|
128
|
+
|
|
129
|
+
const skippedPaths = new Set();
|
|
130
|
+
|
|
131
|
+
for (let contextPath of contextPaths) {
|
|
132
|
+
if (skippedPaths.has(contextPath)) continue;
|
|
133
|
+
|
|
134
|
+
if (contextPath.isFunction()) {
|
|
135
|
+
var assignmentPattern = contextPath.find(
|
|
136
|
+
(p) => p.listKey === "params" && p.parentPath.isFunction()
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
if (assignmentPattern?.isAssignmentPattern()) {
|
|
140
|
+
var functionPath = assignmentPattern.getFunctionParent();
|
|
141
|
+
|
|
142
|
+
if (functionPath) {
|
|
143
|
+
// The parameters can be still accessed...
|
|
144
|
+
const params = paramMap.get(functionPath.node);
|
|
145
|
+
if (params?.has(identifierName)) {
|
|
146
|
+
} else {
|
|
147
|
+
skippedPaths.add(functionPath);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const { node } = contextPath;
|
|
154
|
+
|
|
155
|
+
const defined = definedMap.get(node);
|
|
156
|
+
if (defined?.has(identifierName)) {
|
|
157
|
+
const renamed = renamedVariables.get(node);
|
|
158
|
+
if (renamed?.has(identifierName)) {
|
|
159
|
+
newName = renamed.get(identifierName);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (newName && typeof newName === "string") {
|
|
166
|
+
// __JS_CONFUSER_VAR__ function
|
|
167
|
+
if (isVariableFunctionIdentifier(identifierPath)) {
|
|
168
|
+
identifierPath.parentPath.replaceWith(t.stringLiteral(newName));
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 5. Update Identifier node's 'name' property
|
|
173
|
+
node.name = newName;
|
|
174
|
+
node[RENAMED] = true;
|
|
175
|
+
|
|
176
|
+
// 6. Additional parameter mapping
|
|
177
|
+
const binding = identifierPath.scope.getBinding(identifierName);
|
|
178
|
+
if (binding?.kind === "param") {
|
|
179
|
+
var mapNode = binding.scope.path.node;
|
|
180
|
+
if (!paramMap.has(mapNode)) {
|
|
181
|
+
paramMap.set(mapNode, new Set([identifierName]));
|
|
182
|
+
} else {
|
|
183
|
+
paramMap.get(mapNode).add(identifierName);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
Scopable(scopePath: NodePath<t.Scopable>) {
|
|
190
|
+
// 2. Notice this is on 'onEnter' (top-down)
|
|
191
|
+
const isGlobal = scopePath.isProgram();
|
|
192
|
+
const { node } = scopePath.scope.path;
|
|
193
|
+
if (renamedVariables.has(node)) return;
|
|
194
|
+
|
|
195
|
+
const defined = definedMap.get(node) || new Set();
|
|
196
|
+
const references = referencedMap.get(node) || new Set();
|
|
197
|
+
const bindings = bindingMap.get(node);
|
|
198
|
+
|
|
199
|
+
// No changes needed here
|
|
200
|
+
if (!defined && !renamedVariables.has(node)) {
|
|
201
|
+
renamedVariables.set(node, Object.create(null));
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const newNames = new Map<string, string>();
|
|
206
|
+
|
|
207
|
+
// Names possible to be re-used here
|
|
208
|
+
var possible = new Set<string>();
|
|
209
|
+
|
|
210
|
+
// 3. Try to re-use names when possible
|
|
211
|
+
if (reusePreviousNames && generated.length && !isGlobal) {
|
|
212
|
+
var allReferences = new Set<string>();
|
|
213
|
+
var nope = new Set(defined);
|
|
214
|
+
|
|
215
|
+
scopePath.traverse({
|
|
216
|
+
Scopable(path) {
|
|
217
|
+
const { node } = path.scope.path;
|
|
218
|
+
|
|
219
|
+
var ref = referencedMap.get(node);
|
|
220
|
+
if (ref) {
|
|
221
|
+
ref.forEach((x) => allReferences.add(x));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
var def = definedMap.get(node);
|
|
225
|
+
if (def) {
|
|
226
|
+
def.forEach((x) => allReferences.add(x));
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
var passed = new Set<string>();
|
|
232
|
+
|
|
233
|
+
const parentPaths = scopePath.getAncestry();
|
|
234
|
+
parentPaths.forEach((p) => {
|
|
235
|
+
if (p === scopePath) return;
|
|
236
|
+
|
|
237
|
+
let changes = renamedVariables.get(p.node);
|
|
238
|
+
if (changes) {
|
|
239
|
+
for (let [oldName, newName] of changes) {
|
|
240
|
+
if (!allReferences.has(oldName) && !references.has(oldName)) {
|
|
241
|
+
passed.add(newName);
|
|
242
|
+
} else {
|
|
243
|
+
nope.add(newName);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
nope.forEach((x) => passed.delete(x));
|
|
250
|
+
|
|
251
|
+
possible = passed;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function shouldRename(name: string) {
|
|
255
|
+
// __NO_JS_CONFUSER_RENAME__
|
|
256
|
+
if (name.startsWith(noRenameVariablePrefix)) return false;
|
|
257
|
+
|
|
258
|
+
// Placeholder variables should always be renamed
|
|
259
|
+
if (name.startsWith(placeholderVariablePrefix)) return true;
|
|
260
|
+
|
|
261
|
+
const binding = bindings?.get(name);
|
|
262
|
+
|
|
263
|
+
if (binding) {
|
|
264
|
+
// Do not rename exports
|
|
265
|
+
if (isExportedIdentifier(binding)) return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (name === me.obfuscator.getStringCompressionLibraryName())
|
|
269
|
+
return false;
|
|
270
|
+
|
|
271
|
+
// Global variables are additionally checked against user option
|
|
272
|
+
if (isGlobal) {
|
|
273
|
+
if (!me.computeProbabilityMap(me.options.renameGlobals, name))
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (
|
|
278
|
+
!me.computeProbabilityMap(me.options.renameVariables, name, isGlobal)
|
|
279
|
+
)
|
|
280
|
+
return false;
|
|
281
|
+
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 4. Defined names to new names
|
|
286
|
+
for (var name of defined) {
|
|
287
|
+
let newName = name;
|
|
288
|
+
|
|
289
|
+
if (shouldRename(name)) {
|
|
290
|
+
me.changeData.variables++;
|
|
291
|
+
|
|
292
|
+
// Create a new name from (1) or (2) methods
|
|
293
|
+
do {
|
|
294
|
+
if (possible.size) {
|
|
295
|
+
// (1) Re-use previously generated name
|
|
296
|
+
var first = possible.values().next().value;
|
|
297
|
+
possible.delete(first);
|
|
298
|
+
newName = first;
|
|
299
|
+
} else {
|
|
300
|
+
// (2) Create a new name with global `nameGen`
|
|
301
|
+
var generatedName = me.obfuscator.nameGen.generate();
|
|
302
|
+
|
|
303
|
+
newName = generatedName;
|
|
304
|
+
generated.push(generatedName);
|
|
305
|
+
}
|
|
306
|
+
} while (
|
|
307
|
+
scopePath.scope.hasGlobal(newName) ||
|
|
308
|
+
me.obfuscator.nameGen.notSafeForReuseNames.has(newName)
|
|
309
|
+
);
|
|
310
|
+
// Ensure global names aren't overridden
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
newNames.set(name, newName);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// console.log(node.type, newNames);
|
|
317
|
+
renamedVariables.set(node, newNames);
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
visitor: {
|
|
323
|
+
...VariableAnalysisVisitor,
|
|
324
|
+
|
|
325
|
+
...VariableRenamingVisitor,
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
};
|