javascript-obfuscator-lite 0.0.0-stage → 1.0.0
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/CHANGELOG.md +88 -0
- package/LICENSE +21 -0
- package/README.md +470 -2
- package/bin/cli.js +325 -0
- package/lib/index.d.ts +57 -0
- package/lib/index.js +140 -0
- package/lib/options.js +159 -0
- package/lib/transforms.js +757 -0
- package/lib/utils.js +452 -0
- package/package.json +56 -4
|
@@ -0,0 +1,757 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const traverse = require('@babel/traverse').default;
|
|
4
|
+
const generate = require('@babel/generator').default;
|
|
5
|
+
const t = require('@babel/types');
|
|
6
|
+
|
|
7
|
+
const utils = require('./utils');
|
|
8
|
+
|
|
9
|
+
/** Return a random integer in [0, max). */
|
|
10
|
+
function randomInt(max) {
|
|
11
|
+
return Math.floor(Math.random() * max);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Return true when a name is bound in the local scope chain (not a global). */
|
|
15
|
+
function hasLocalBinding(scope, name) {
|
|
16
|
+
let current = scope;
|
|
17
|
+
while (current) {
|
|
18
|
+
if (current.hasOwnBinding(name)) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
current = current.parent;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Replace string literals with a reverse/split/join expression.
|
|
28
|
+
*
|
|
29
|
+
* Strings containing surrogate pairs are skipped: reversing UTF-16 code units
|
|
30
|
+
* would corrupt emoji and other non-BMP characters.
|
|
31
|
+
*/
|
|
32
|
+
function stringReverse(ast) {
|
|
33
|
+
traverse(ast, {
|
|
34
|
+
StringLiteral(path) {
|
|
35
|
+
if (!utils.isSafeStringExpression(path)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const value = path.node.value;
|
|
40
|
+
if (value.length === 0 || value.length === 1 || value.length >= 512) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (/[\uD800-\uDFFF]/.test(value)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const reversed = value.split('').reverse().join('');
|
|
48
|
+
const split = t.callExpression(
|
|
49
|
+
t.memberExpression(t.stringLiteral(reversed), t.identifier('split')),
|
|
50
|
+
[t.stringLiteral('')]
|
|
51
|
+
);
|
|
52
|
+
const reverse = t.callExpression(
|
|
53
|
+
t.memberExpression(split, t.identifier('reverse')),
|
|
54
|
+
[]
|
|
55
|
+
);
|
|
56
|
+
const join = t.callExpression(
|
|
57
|
+
t.memberExpression(reverse, t.identifier('join')),
|
|
58
|
+
[t.stringLiteral('')]
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
path.replaceWith(join);
|
|
62
|
+
path.skip();
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Return a harmless literal/expression used as zombie-code filler. */
|
|
68
|
+
function zombieValue(zombieName) {
|
|
69
|
+
const roll = randomInt(3);
|
|
70
|
+
if (roll === 0) {
|
|
71
|
+
return t.stringLiteral(zombieName);
|
|
72
|
+
}
|
|
73
|
+
if (roll === 1) {
|
|
74
|
+
return t.numericLiteral(randomInt(10));
|
|
75
|
+
}
|
|
76
|
+
return t.binaryExpression(
|
|
77
|
+
'+',
|
|
78
|
+
t.numericLiteral(randomInt(10)),
|
|
79
|
+
t.numericLiteral(randomInt(10))
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Insert dead variable declarations before and assignments after existing
|
|
85
|
+
* variable declarations. Only declarations in statement-list positions are
|
|
86
|
+
* touched, so the AST stays valid.
|
|
87
|
+
*/
|
|
88
|
+
function zombieCode(ast, options, context = {}) {
|
|
89
|
+
if (context.hasWith) {
|
|
90
|
+
// A `with` object can shadow injected variable names.
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
traverse(ast, {
|
|
94
|
+
VariableDeclaration(path) {
|
|
95
|
+
if (!['var', 'let', 'const'].includes(path.node.kind)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (path.node.declarations.length !== 1) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const parent = path.parentPath;
|
|
103
|
+
if (
|
|
104
|
+
!parent ||
|
|
105
|
+
!(
|
|
106
|
+
parent.isBlockStatement() ||
|
|
107
|
+
parent.isProgram() ||
|
|
108
|
+
parent.isSwitchCase() ||
|
|
109
|
+
(typeof parent.isStaticBlock === 'function' && parent.isStaticBlock())
|
|
110
|
+
)
|
|
111
|
+
) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const declarator = path.node.declarations[0];
|
|
116
|
+
if (!t.isIdentifier(declarator.id)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const originalName = declarator.id.name;
|
|
120
|
+
if (!originalName || originalName.startsWith('_')) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const zombieName = path.scope.generateUidIdentifier('_').name;
|
|
125
|
+
const declaration = t.variableDeclaration('var', [
|
|
126
|
+
t.variableDeclarator(
|
|
127
|
+
utils.markGenerated(t.identifier(zombieName)),
|
|
128
|
+
zombieValue(zombieName)
|
|
129
|
+
),
|
|
130
|
+
]);
|
|
131
|
+
const assignment = t.expressionStatement(
|
|
132
|
+
t.assignmentExpression(
|
|
133
|
+
'=',
|
|
134
|
+
utils.markGenerated(t.identifier(zombieName)),
|
|
135
|
+
zombieValue(zombieName)
|
|
136
|
+
)
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
path.insertBefore(declaration);
|
|
141
|
+
path.insertAfter(assignment);
|
|
142
|
+
path.skip();
|
|
143
|
+
} catch {
|
|
144
|
+
// Some unusual statement positions cannot accept sibling nodes.
|
|
145
|
+
// The declaration is simply left untouched in that case.
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Convert dot property access into computed, hexadecimal string access:
|
|
153
|
+
* `console.log` -> `console['\x6c\x6f\x67']`.
|
|
154
|
+
*/
|
|
155
|
+
function memberExpression(ast) {
|
|
156
|
+
traverse(ast, {
|
|
157
|
+
'MemberExpression|OptionalMemberExpression'(path) {
|
|
158
|
+
if (path.node.computed) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const property = path.node.property;
|
|
162
|
+
if (!t.isIdentifier(property) || !property.name) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const encoded = utils.encodePropertyName(property.name);
|
|
167
|
+
path.node.property = utils.stringLiteralWithRaw(
|
|
168
|
+
property.name,
|
|
169
|
+
`'${encoded}'`
|
|
170
|
+
);
|
|
171
|
+
path.node.computed = true;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Wrap literal right-hand sides of assignments in an immediately invoked
|
|
178
|
+
* function: `a = "x"` -> `a = function () { return "x"; }()`.
|
|
179
|
+
*/
|
|
180
|
+
function assignmentJunk(ast) {
|
|
181
|
+
traverse(ast, {
|
|
182
|
+
AssignmentExpression(path) {
|
|
183
|
+
const right = path.node.right;
|
|
184
|
+
let value;
|
|
185
|
+
if (t.isStringLiteral(right)) {
|
|
186
|
+
value = t.stringLiteral(right.value);
|
|
187
|
+
} else if (t.isNumericLiteral(right)) {
|
|
188
|
+
value = t.numericLiteral(right.value);
|
|
189
|
+
} else if (t.isBooleanLiteral(right)) {
|
|
190
|
+
value = t.booleanLiteral(right.value);
|
|
191
|
+
} else {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const fn = t.functionExpression(
|
|
196
|
+
null,
|
|
197
|
+
[],
|
|
198
|
+
t.blockStatement([t.returnStatement(value)])
|
|
199
|
+
);
|
|
200
|
+
path.get('right').replaceWith(t.callExpression(fn, []));
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Replace small integer literals with XOR expressions:
|
|
207
|
+
* `123` -> `(cipher ^ key)`.
|
|
208
|
+
*/
|
|
209
|
+
function numericLiteral(ast) {
|
|
210
|
+
traverse(ast, {
|
|
211
|
+
NumericLiteral(path) {
|
|
212
|
+
if (!utils.isSafeNumericExpression(path)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const value = path.node.value;
|
|
217
|
+
if (!Number.isInteger(value) || Math.abs(value) > 0xffff) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const key = randomInt(900000) + 100000;
|
|
222
|
+
const cipher = value ^ key;
|
|
223
|
+
path.replaceWith(
|
|
224
|
+
t.binaryExpression('^', t.numericLiteral(cipher), t.numericLiteral(key))
|
|
225
|
+
);
|
|
226
|
+
path.skip();
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Wrap binary expressions used as variable initializers in a call expression:
|
|
233
|
+
* `var x = a + b` -> `var x = function (s, h) { return s + h; }(a, b)`.
|
|
234
|
+
*/
|
|
235
|
+
function binaryExpression(ast) {
|
|
236
|
+
traverse(ast, {
|
|
237
|
+
VariableDeclarator(path) {
|
|
238
|
+
const init = path.get('init');
|
|
239
|
+
if (!init.isBinaryExpression()) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const { operator, left, right } = init.node;
|
|
244
|
+
const first = t.identifier('s');
|
|
245
|
+
const second = t.identifier('h');
|
|
246
|
+
const fn = t.functionExpression(
|
|
247
|
+
null,
|
|
248
|
+
[first, second],
|
|
249
|
+
t.blockStatement([
|
|
250
|
+
t.returnStatement(t.binaryExpression(operator, first, second)),
|
|
251
|
+
])
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
init.replaceWith(t.callExpression(fn, [left, right]));
|
|
255
|
+
init.skip();
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Replace boolean literals with `!![]` / `![]`. */
|
|
261
|
+
function booleanLiteral(ast) {
|
|
262
|
+
traverse(ast, {
|
|
263
|
+
BooleanLiteral(path) {
|
|
264
|
+
if (!utils.isSafeBooleanExpression(path)) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const array = t.arrayExpression();
|
|
269
|
+
const replacement = path.node.value
|
|
270
|
+
? t.unaryExpression('!', t.unaryExpression('!', array))
|
|
271
|
+
: t.unaryExpression('!', array);
|
|
272
|
+
path.replaceWith(replacement);
|
|
273
|
+
path.skip();
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Rewrite string literals using \uXXXX escapes. Non-BMP characters are
|
|
280
|
+
* emitted as surrogate pairs so the generated code remains valid.
|
|
281
|
+
*/
|
|
282
|
+
function stringUnicode(ast) {
|
|
283
|
+
traverse(ast, {
|
|
284
|
+
StringLiteral(path) {
|
|
285
|
+
const node = path.node;
|
|
286
|
+
|
|
287
|
+
// JSX attribute strings are raw text: escape sequences are not
|
|
288
|
+
// interpreted by JSX, so rewriting the raw value would change it.
|
|
289
|
+
const parent = path.parentPath && path.parentPath.node;
|
|
290
|
+
if (parent && parent.type === 'JSXAttribute' && path.key === 'value') {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (node.value.length >= 512) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (!node.extra || !node.extra.raw || /\\[ux]/i.test(node.extra.raw)) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const encoded = utils.unicodeEscape(node.value);
|
|
302
|
+
node.extra.raw = `'${encoded}'`;
|
|
303
|
+
node.extra.rawValue = node.value;
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function isFlattenableStatement(statement) {
|
|
309
|
+
if (statement.type === 'VariableDeclaration' && statement.kind !== 'var') {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
if (
|
|
313
|
+
statement.type === 'FunctionDeclaration' ||
|
|
314
|
+
statement.type === 'ClassDeclaration' ||
|
|
315
|
+
statement.type === 'ImportDeclaration' ||
|
|
316
|
+
statement.type.startsWith('Export')
|
|
317
|
+
) {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
return !utils.hasUnsafeDeclaration(statement);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Flatten a function body into a switch-based dispatcher.
|
|
325
|
+
*
|
|
326
|
+
* The original statement order is preserved at runtime; only the physical
|
|
327
|
+
* layout changes. Functions containing lexical declarations, classes, or
|
|
328
|
+
* nested function declarations are skipped because moving those declarations
|
|
329
|
+
* can change scoping/hoisting behavior.
|
|
330
|
+
*/
|
|
331
|
+
function controlFlow(ast, options, context = {}) {
|
|
332
|
+
if (context.hasWith) {
|
|
333
|
+
// Generated dispatcher names could be shadowed by a `with` object.
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
traverse(ast, {
|
|
337
|
+
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression'(path) {
|
|
338
|
+
const body = path.node.body;
|
|
339
|
+
if (!t.isBlockStatement(body)) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (body.directives && body.directives.length) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (body.body.length <= 1) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (!body.body.every(isFlattenableStatement)) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const statements = body.body.map((statement, index) => ({
|
|
353
|
+
statement,
|
|
354
|
+
index,
|
|
355
|
+
}));
|
|
356
|
+
for (let i = statements.length - 1; i > 0; i -= 1) {
|
|
357
|
+
const j = randomInt(i + 1);
|
|
358
|
+
[statements[i], statements[j]] = [statements[j], statements[i]];
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const order = new Array(statements.length);
|
|
362
|
+
const cases = statements.map((entry, caseIndex) => {
|
|
363
|
+
order[entry.index] = caseIndex;
|
|
364
|
+
return t.switchCase(t.numericLiteral(caseIndex), [
|
|
365
|
+
entry.statement,
|
|
366
|
+
t.continueStatement(),
|
|
367
|
+
]);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const arrayName = path.scope.generateUidIdentifier('_').name;
|
|
371
|
+
const indexName = path.scope.generateUidIdentifier('_').name;
|
|
372
|
+
|
|
373
|
+
const dispatcher = t.variableDeclaration('var', [
|
|
374
|
+
t.variableDeclarator(
|
|
375
|
+
utils.markGenerated(t.identifier(arrayName)),
|
|
376
|
+
t.callExpression(
|
|
377
|
+
t.memberExpression(
|
|
378
|
+
t.stringLiteral(order.join('|')),
|
|
379
|
+
t.identifier('split')
|
|
380
|
+
),
|
|
381
|
+
[t.stringLiteral('|')]
|
|
382
|
+
)
|
|
383
|
+
),
|
|
384
|
+
t.variableDeclarator(
|
|
385
|
+
utils.markGenerated(t.identifier(indexName)),
|
|
386
|
+
t.numericLiteral(0)
|
|
387
|
+
),
|
|
388
|
+
]);
|
|
389
|
+
|
|
390
|
+
const discriminant = t.unaryExpression(
|
|
391
|
+
'+',
|
|
392
|
+
t.memberExpression(
|
|
393
|
+
t.identifier(arrayName),
|
|
394
|
+
t.updateExpression('++', t.identifier(indexName)),
|
|
395
|
+
true
|
|
396
|
+
)
|
|
397
|
+
);
|
|
398
|
+
const switchStatement = t.switchStatement(discriminant, cases);
|
|
399
|
+
const condition = t.unaryExpression(
|
|
400
|
+
'!',
|
|
401
|
+
t.unaryExpression('!', t.arrayExpression())
|
|
402
|
+
);
|
|
403
|
+
const whileStatement = t.whileStatement(
|
|
404
|
+
condition,
|
|
405
|
+
t.blockStatement([switchStatement, t.breakStatement()])
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
path.get('body').replaceWith(
|
|
409
|
+
t.blockStatement([dispatcher, whileStatement])
|
|
410
|
+
);
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Move eligible string literals into a single file-level array and replace
|
|
417
|
+
* each occurrence with an indexed lookup.
|
|
418
|
+
*/
|
|
419
|
+
function stringArray(ast, options, context = {}) {
|
|
420
|
+
if (context.hasWith) {
|
|
421
|
+
// The file-level array name could be shadowed by a `with` object.
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const candidates = [];
|
|
425
|
+
traverse(ast, {
|
|
426
|
+
StringLiteral(path) {
|
|
427
|
+
if (!utils.isSafeStringExpression(path)) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
candidates.push({ path, value: path.node.value });
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
if (candidates.length === 0) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const indexByValue = new Map();
|
|
439
|
+
const values = [];
|
|
440
|
+
for (const candidate of candidates) {
|
|
441
|
+
if (!indexByValue.has(candidate.value)) {
|
|
442
|
+
indexByValue.set(candidate.value, values.length);
|
|
443
|
+
values.push(candidate.value);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const nameGenerator = utils.createNameGenerator(ast);
|
|
448
|
+
const arrayName = nameGenerator.next();
|
|
449
|
+
|
|
450
|
+
for (const candidate of candidates) {
|
|
451
|
+
const index = indexByValue.get(candidate.value);
|
|
452
|
+
candidate.path.replaceWith(
|
|
453
|
+
t.memberExpression(
|
|
454
|
+
t.identifier(arrayName),
|
|
455
|
+
t.numericLiteral(index),
|
|
456
|
+
true
|
|
457
|
+
)
|
|
458
|
+
);
|
|
459
|
+
candidate.path.skip();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const declaration = t.variableDeclaration('var', [
|
|
463
|
+
t.variableDeclarator(
|
|
464
|
+
utils.markGenerated(t.identifier(arrayName)),
|
|
465
|
+
t.arrayExpression(values.map((value) => t.stringLiteral(value)))
|
|
466
|
+
),
|
|
467
|
+
]);
|
|
468
|
+
ast.program.body.unshift(declaration);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Rename variable and function bindings to short, globally unique names.
|
|
473
|
+
*
|
|
474
|
+
* File-wide uniqueness is important: renaming a global to a name that already
|
|
475
|
+
* exists in a nested scope would capture references in that scope.
|
|
476
|
+
*/
|
|
477
|
+
function rename(ast, options, context = {}) {
|
|
478
|
+
if (context.hasWith) {
|
|
479
|
+
// `with` enables dynamic name resolution; renaming bindings could change
|
|
480
|
+
// which property is read or written.
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const exportedNames =
|
|
484
|
+
context.exportedNames || utils.collectExportedNames(ast);
|
|
485
|
+
const commonJsExports =
|
|
486
|
+
context.commonJsExports !== undefined
|
|
487
|
+
? context.commonJsExports
|
|
488
|
+
: utils.hasCommonJsExports(ast);
|
|
489
|
+
const allowGlobals = options.renameGlobals && !commonJsExports;
|
|
490
|
+
|
|
491
|
+
// Refresh binding information after structural transforms have inserted
|
|
492
|
+
// declarations that the original scopes did not know about.
|
|
493
|
+
traverse(ast, {
|
|
494
|
+
Program(path) {
|
|
495
|
+
path.scope.crawl();
|
|
496
|
+
},
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
const nameGenerator = utils.createNameGenerator(ast);
|
|
500
|
+
const mappings = new Map();
|
|
501
|
+
const declarationNodes = new Map();
|
|
502
|
+
|
|
503
|
+
function nextShorterName(oldName) {
|
|
504
|
+
if (oldName.length <= 2) {
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
for (let attempt = 0; attempt < 64; attempt += 1) {
|
|
508
|
+
const candidate = nameGenerator.next();
|
|
509
|
+
if (candidate.length < oldName.length) {
|
|
510
|
+
return candidate;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function plan(binding, oldName, declarationNode) {
|
|
517
|
+
if (!binding) {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
let newName = mappings.get(binding);
|
|
522
|
+
if (!newName) {
|
|
523
|
+
const isGlobal = binding.scope.path.isProgram();
|
|
524
|
+
if (isGlobal) {
|
|
525
|
+
if (!allowGlobals || exportedNames.has(oldName)) {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
} else if (!options.renameLocals) {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
newName = nextShorterName(oldName);
|
|
533
|
+
if (!newName) {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
mappings.set(binding, newName);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (declarationNode) {
|
|
540
|
+
declarationNodes.set(declarationNode, newName);
|
|
541
|
+
}
|
|
542
|
+
if (binding.identifier) {
|
|
543
|
+
declarationNodes.set(binding.identifier, newName);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
traverse(ast, {
|
|
548
|
+
VariableDeclarator(path) {
|
|
549
|
+
const id = path.node.id;
|
|
550
|
+
if (!t.isIdentifier(id) || utils.isGenerated(id)) {
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
plan(path.scope.getBinding(id.name), id.name, id);
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
traverse(ast, {
|
|
558
|
+
FunctionDeclaration(path) {
|
|
559
|
+
const id = path.node.id;
|
|
560
|
+
if (!t.isIdentifier(id)) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Block-level function declarations have Annex B hoisting rules in
|
|
565
|
+
// sloppy mode. Renaming one can leave outer references pointing at the
|
|
566
|
+
// old name, so only rename declarations directly in a program or a
|
|
567
|
+
// function body.
|
|
568
|
+
const parent = path.parentPath;
|
|
569
|
+
const directInProgram = parent.isProgram();
|
|
570
|
+
const directInFunctionBody =
|
|
571
|
+
parent.isBlockStatement() &&
|
|
572
|
+
parent.parentPath &&
|
|
573
|
+
parent.parentPath.isFunction();
|
|
574
|
+
if (!directInProgram && !directInFunctionBody) {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const parentScope = path.scope.parent || path.scope;
|
|
579
|
+
plan(parentScope.getBinding(id.name), id.name, id);
|
|
580
|
+
},
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
if (mappings.size === 0) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// A binding can be declared by several nodes (for example, a plain `var`
|
|
588
|
+
// plus a later destructuring declaration). Collect every declaration
|
|
589
|
+
// identifier that belongs to a renamed binding so all of them are updated.
|
|
590
|
+
traverse(ast, {
|
|
591
|
+
VariableDeclarator(path) {
|
|
592
|
+
const identifiers = path.getBindingIdentifiers();
|
|
593
|
+
for (const name of Object.keys(identifiers)) {
|
|
594
|
+
const binding = path.scope.getBinding(name);
|
|
595
|
+
const newName = binding && mappings.get(binding);
|
|
596
|
+
if (!newName) {
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
const nodes = Array.isArray(identifiers[name])
|
|
600
|
+
? identifiers[name]
|
|
601
|
+
: [identifiers[name]];
|
|
602
|
+
for (const node of nodes) {
|
|
603
|
+
if (node) {
|
|
604
|
+
declarationNodes.set(node, newName);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
function renameIdentifier(path) {
|
|
612
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
613
|
+
const newName = binding && mappings.get(binding);
|
|
614
|
+
if (newName) {
|
|
615
|
+
path.node.name = newName;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function renameOuterBindingIdentifiers(path) {
|
|
620
|
+
const identifiers = path.getOuterBindingIdentifiers();
|
|
621
|
+
for (const name of Object.keys(identifiers)) {
|
|
622
|
+
const binding = path.scope.getBinding(name);
|
|
623
|
+
const newName = binding && mappings.get(binding);
|
|
624
|
+
if (!newName) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const nodes = Array.isArray(identifiers[name])
|
|
629
|
+
? identifiers[name]
|
|
630
|
+
: [identifiers[name]];
|
|
631
|
+
for (const node of nodes) {
|
|
632
|
+
if (node) {
|
|
633
|
+
node.name = newName;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// Apply all renames in one pass. Babel's Scope#rename rescans the entire
|
|
640
|
+
// program for each global binding, which is quadratic; a single
|
|
641
|
+
// binding-aware pass is dramatically faster for large files.
|
|
642
|
+
traverse(ast, {
|
|
643
|
+
ObjectProperty(path) {
|
|
644
|
+
const node = path.node;
|
|
645
|
+
if (!node.shorthand) {
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
const value = node.value;
|
|
650
|
+
let identifier = null;
|
|
651
|
+
if (t.isIdentifier(value)) {
|
|
652
|
+
identifier = value;
|
|
653
|
+
} else if (t.isAssignmentPattern(value) && t.isIdentifier(value.left)) {
|
|
654
|
+
identifier = value.left;
|
|
655
|
+
}
|
|
656
|
+
if (!identifier) {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const binding = path.scope.getBinding(identifier.name);
|
|
661
|
+
if (!binding || !mappings.has(binding)) {
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// Preserve the property key while renaming the shorthand value:
|
|
666
|
+
// { longName } -> { longName: _0 }.
|
|
667
|
+
node.shorthand = false;
|
|
668
|
+
if (node.extra) {
|
|
669
|
+
node.extra.shorthand = false;
|
|
670
|
+
}
|
|
671
|
+
if (node.key === identifier) {
|
|
672
|
+
node.key = t.identifier(identifier.name);
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
BindingIdentifier(path) {
|
|
676
|
+
// Declaration identifiers must be matched by node identity. Looking up
|
|
677
|
+
// the name through the scope can resolve a shadowing inner binding (for
|
|
678
|
+
// example, a function name shadowed by a var declaration in its body).
|
|
679
|
+
const direct = declarationNodes.get(path.node);
|
|
680
|
+
if (direct) {
|
|
681
|
+
path.node.name = direct;
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
ReferencedIdentifier: renameIdentifier,
|
|
685
|
+
AssignmentExpression: {
|
|
686
|
+
exit: renameOuterBindingIdentifiers,
|
|
687
|
+
},
|
|
688
|
+
'ForInStatement|ForOfStatement': {
|
|
689
|
+
exit: renameOuterBindingIdentifiers,
|
|
690
|
+
},
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* Encode expressions as `eval(String.fromCharCode(...))`.
|
|
696
|
+
*
|
|
697
|
+
* This transform is opt-in and is skipped in strict-mode/module contexts,
|
|
698
|
+
* around syntax that cannot appear in eval code, and when eval/String are
|
|
699
|
+
* shadowed by local bindings.
|
|
700
|
+
*/
|
|
701
|
+
function evalEncode(ast) {
|
|
702
|
+
traverse(ast, {
|
|
703
|
+
'UpdateExpression|BinaryExpression'(path) {
|
|
704
|
+
if (utils.isStrictContext(path)) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (path.findParent((parent) => parent.isWithStatement())) {
|
|
708
|
+
// A `with` object can shadow eval/String dynamically.
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
if (utils.containsForbiddenEvalNode(path.node)) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
if (hasLocalBinding(path.scope, 'eval') || hasLocalBinding(path.scope, 'String')) {
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const code = generate(path.node, {
|
|
719
|
+
compact: true,
|
|
720
|
+
comments: false,
|
|
721
|
+
}).code;
|
|
722
|
+
if (code.length > 1024) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// Use UTF-16 code units so astral characters round-trip correctly.
|
|
727
|
+
const characters = [];
|
|
728
|
+
for (let index = 0; index < code.length; index += 1) {
|
|
729
|
+
characters.push(t.numericLiteral(code.charCodeAt(index)));
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const decrypt = t.callExpression(
|
|
733
|
+
t.memberExpression(t.identifier('String'), t.identifier('fromCharCode')),
|
|
734
|
+
characters
|
|
735
|
+
);
|
|
736
|
+
path.replaceWith(
|
|
737
|
+
t.callExpression(t.identifier('eval'), [decrypt])
|
|
738
|
+
);
|
|
739
|
+
path.skip();
|
|
740
|
+
},
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
module.exports = {
|
|
745
|
+
stringReverse,
|
|
746
|
+
zombieCode,
|
|
747
|
+
memberExpression,
|
|
748
|
+
assignmentJunk,
|
|
749
|
+
numericLiteral,
|
|
750
|
+
binaryExpression,
|
|
751
|
+
booleanLiteral,
|
|
752
|
+
stringUnicode,
|
|
753
|
+
controlFlow,
|
|
754
|
+
stringArray,
|
|
755
|
+
rename,
|
|
756
|
+
evalEncode,
|
|
757
|
+
};
|