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,420 +1,421 @@
|
|
|
1
|
-
import * as t from "@babel/types";
|
|
2
|
-
import { NodePath } from "@babel/traverse";
|
|
3
|
-
import {
|
|
4
|
-
ensureComputedExpression,
|
|
5
|
-
getFunctionName,
|
|
6
|
-
isDefiningIdentifier,
|
|
7
|
-
isModifiedIdentifier,
|
|
8
|
-
isStrictMode,
|
|
9
|
-
isVariableIdentifier,
|
|
10
|
-
prepend,
|
|
11
|
-
prependProgram,
|
|
12
|
-
} from "../utils/ast-utils";
|
|
13
|
-
import { PluginArg, PluginObject } from "./plugin";
|
|
14
|
-
import { Order } from "../order";
|
|
15
|
-
import { NodeSymbol, PREDICTABLE, UNSAFE } from "../constants";
|
|
16
|
-
import {
|
|
17
|
-
computeFunctionLength,
|
|
18
|
-
isVariableFunctionIdentifier,
|
|
19
|
-
} from "../utils/function-utils";
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
me.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (
|
|
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
|
-
|
|
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
|
-
t.
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
false
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
t.
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
false
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
t.
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
false
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
"
|
|
297
|
-
t.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
false
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
t.
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
t.identifier(
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
fnPath.
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
(
|
|
368
|
-
|
|
369
|
-
(
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
t.
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
};
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import { NodePath } from "@babel/traverse";
|
|
3
|
+
import {
|
|
4
|
+
ensureComputedExpression,
|
|
5
|
+
getFunctionName,
|
|
6
|
+
isDefiningIdentifier,
|
|
7
|
+
isModifiedIdentifier,
|
|
8
|
+
isStrictMode,
|
|
9
|
+
isVariableIdentifier,
|
|
10
|
+
prepend,
|
|
11
|
+
prependProgram,
|
|
12
|
+
} from "../utils/ast-utils";
|
|
13
|
+
import { PluginArg, PluginObject } from "./plugin";
|
|
14
|
+
import { Order } from "../order";
|
|
15
|
+
import { NodeSymbol, PREDICTABLE, UNSAFE } from "../constants";
|
|
16
|
+
import {
|
|
17
|
+
computeFunctionLength,
|
|
18
|
+
isVariableFunctionIdentifier,
|
|
19
|
+
} from "../utils/function-utils";
|
|
20
|
+
import { NameGen } from "../utils/NameGen";
|
|
21
|
+
|
|
22
|
+
export default ({ Plugin }: PluginArg): PluginObject => {
|
|
23
|
+
const me = Plugin(Order.Flatten, {
|
|
24
|
+
changeData: {
|
|
25
|
+
functions: 0,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const isDebug = false;
|
|
29
|
+
|
|
30
|
+
function flattenFunction(fnPath: NodePath<t.Function>) {
|
|
31
|
+
// Skip if already processed
|
|
32
|
+
if (me.isSkipped(fnPath)) return;
|
|
33
|
+
|
|
34
|
+
// Don't apply to generator functions
|
|
35
|
+
if (fnPath.node.generator) return;
|
|
36
|
+
|
|
37
|
+
// Skip getter/setter methods
|
|
38
|
+
if (fnPath.isObjectMethod() || fnPath.isClassMethod()) {
|
|
39
|
+
if (fnPath.node.kind !== "method") return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Do not apply to arrow functions
|
|
43
|
+
if (t.isArrowFunctionExpression(fnPath.node)) return;
|
|
44
|
+
if (!t.isBlockStatement(fnPath.node.body)) return;
|
|
45
|
+
|
|
46
|
+
// Skip if marked as unsafe
|
|
47
|
+
if ((fnPath.node as NodeSymbol)[UNSAFE]) return;
|
|
48
|
+
|
|
49
|
+
var program = fnPath.findParent((p) =>
|
|
50
|
+
p.isProgram()
|
|
51
|
+
) as NodePath<t.Program>;
|
|
52
|
+
|
|
53
|
+
let functionName = getFunctionName(fnPath);
|
|
54
|
+
if (!t.isValidIdentifier(functionName, true)) {
|
|
55
|
+
functionName = "anonymous";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!me.computeProbabilityMap(me.options.flatten, functionName)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const strictMode = fnPath.find((path) => isStrictMode(path));
|
|
63
|
+
if (strictMode === fnPath) return;
|
|
64
|
+
|
|
65
|
+
me.log("Transforming", functionName);
|
|
66
|
+
|
|
67
|
+
const flatObjectName = `${me.getPlaceholder()}_flat_object`;
|
|
68
|
+
const newFnName = `${me.getPlaceholder()}_flat_${functionName}`;
|
|
69
|
+
|
|
70
|
+
const nameGen = new NameGen(me.options.identifierGenerator);
|
|
71
|
+
|
|
72
|
+
function generateProp(originalName: string, type: string) {
|
|
73
|
+
var newPropertyName: string;
|
|
74
|
+
do {
|
|
75
|
+
newPropertyName = isDebug
|
|
76
|
+
? type + "_" + originalName
|
|
77
|
+
: nameGen.generate();
|
|
78
|
+
} while (allPropertyNames.has(newPropertyName));
|
|
79
|
+
|
|
80
|
+
allPropertyNames.add(newPropertyName);
|
|
81
|
+
|
|
82
|
+
return newPropertyName;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const standardProps = new Map<string, string>();
|
|
86
|
+
const setterPropsNeeded = new Set<string>();
|
|
87
|
+
const typeofProps = new Map<string, string>();
|
|
88
|
+
const functionCallProps = new Map<string, string>();
|
|
89
|
+
const allPropertyNames = new Set();
|
|
90
|
+
|
|
91
|
+
const identifierPaths: NodePath<t.Identifier>[] = [];
|
|
92
|
+
|
|
93
|
+
// Traverse function to identify variables to be replaced with flat object properties
|
|
94
|
+
fnPath.traverse({
|
|
95
|
+
Identifier: {
|
|
96
|
+
exit(identifierPath) {
|
|
97
|
+
if (!isVariableIdentifier(identifierPath)) return;
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
identifierPath.isBindingIdentifier() &&
|
|
101
|
+
isDefiningIdentifier(identifierPath)
|
|
102
|
+
)
|
|
103
|
+
return;
|
|
104
|
+
|
|
105
|
+
if (isVariableFunctionIdentifier(identifierPath)) return;
|
|
106
|
+
|
|
107
|
+
if ((identifierPath.node as NodeSymbol)[UNSAFE]) return;
|
|
108
|
+
const identifierName = identifierPath.node.name;
|
|
109
|
+
|
|
110
|
+
if (identifierName === "arguments") return;
|
|
111
|
+
|
|
112
|
+
var binding = identifierPath.scope.getBinding(identifierName);
|
|
113
|
+
if (!binding) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
var isOutsideVariable =
|
|
118
|
+
fnPath.scope.parent.getBinding(identifierName) === binding;
|
|
119
|
+
|
|
120
|
+
if (!isOutsideVariable) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
identifierPaths.push(identifierPath);
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
me.log(
|
|
130
|
+
`Function ${functionName}`,
|
|
131
|
+
"requires",
|
|
132
|
+
Array.from(new Set(identifierPaths.map((x) => x.node.name)))
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
for (var identifierPath of identifierPaths) {
|
|
136
|
+
const identifierName = identifierPath.node.name;
|
|
137
|
+
if (typeof identifierName !== "string") continue;
|
|
138
|
+
|
|
139
|
+
const isTypeof = identifierPath.parentPath.isUnaryExpression({
|
|
140
|
+
operator: "typeof",
|
|
141
|
+
});
|
|
142
|
+
const isFunctionCall =
|
|
143
|
+
identifierPath.parentPath.isCallExpression() &&
|
|
144
|
+
identifierPath.parentPath.node.callee === identifierPath.node;
|
|
145
|
+
|
|
146
|
+
if (isTypeof) {
|
|
147
|
+
var typeofProp = typeofProps.get(identifierName);
|
|
148
|
+
if (!typeofProp) {
|
|
149
|
+
typeofProp = generateProp(identifierName, "typeof");
|
|
150
|
+
typeofProps.set(identifierName, typeofProp);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
ensureComputedExpression(identifierPath.parentPath);
|
|
154
|
+
|
|
155
|
+
identifierPath.parentPath
|
|
156
|
+
.replaceWith(
|
|
157
|
+
t.memberExpression(
|
|
158
|
+
t.identifier(flatObjectName),
|
|
159
|
+
t.stringLiteral(typeofProp),
|
|
160
|
+
true
|
|
161
|
+
)
|
|
162
|
+
)[0]
|
|
163
|
+
.skip();
|
|
164
|
+
} else if (isFunctionCall) {
|
|
165
|
+
let functionCallProp = functionCallProps.get(identifierName);
|
|
166
|
+
if (!functionCallProp) {
|
|
167
|
+
functionCallProp = generateProp(identifierName, "call");
|
|
168
|
+
functionCallProps.set(identifierName, functionCallProp);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
ensureComputedExpression(identifierPath);
|
|
172
|
+
|
|
173
|
+
// Replace identifier with a reference to the flat object property
|
|
174
|
+
identifierPath
|
|
175
|
+
.replaceWith(
|
|
176
|
+
t.memberExpression(
|
|
177
|
+
t.identifier(flatObjectName),
|
|
178
|
+
t.stringLiteral(functionCallProp),
|
|
179
|
+
true
|
|
180
|
+
)
|
|
181
|
+
)[0]
|
|
182
|
+
.skip();
|
|
183
|
+
} else {
|
|
184
|
+
let standardProp = standardProps.get(identifierName);
|
|
185
|
+
if (!standardProp) {
|
|
186
|
+
standardProp = generateProp(identifierName, "standard");
|
|
187
|
+
standardProps.set(identifierName, standardProp);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!setterPropsNeeded.has(identifierName)) {
|
|
191
|
+
// Only provide 'set' method if the variable is modified
|
|
192
|
+
var isModification = isModifiedIdentifier(identifierPath);
|
|
193
|
+
|
|
194
|
+
if (isModification) {
|
|
195
|
+
setterPropsNeeded.add(identifierName);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
ensureComputedExpression(identifierPath);
|
|
200
|
+
|
|
201
|
+
// Replace identifier with a reference to the flat object property
|
|
202
|
+
identifierPath
|
|
203
|
+
.replaceWith(
|
|
204
|
+
t.memberExpression(
|
|
205
|
+
t.identifier(flatObjectName),
|
|
206
|
+
t.stringLiteral(standardProp),
|
|
207
|
+
true
|
|
208
|
+
)
|
|
209
|
+
)[0]
|
|
210
|
+
.skip();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// for (const prop of [...typeofProps.keys(), ...functionCallProps.keys()]) {
|
|
215
|
+
// if (!standardProps.has(prop)) {
|
|
216
|
+
// standardProps.set(prop, generateProp());
|
|
217
|
+
// }
|
|
218
|
+
// }
|
|
219
|
+
|
|
220
|
+
const flatObjectProperties: t.ObjectMember[] = [];
|
|
221
|
+
|
|
222
|
+
for (var entry of standardProps) {
|
|
223
|
+
const [identifierName, objectProp] = entry;
|
|
224
|
+
|
|
225
|
+
flatObjectProperties.push(
|
|
226
|
+
me.skip(
|
|
227
|
+
t.objectMethod(
|
|
228
|
+
"get",
|
|
229
|
+
t.stringLiteral(objectProp),
|
|
230
|
+
[],
|
|
231
|
+
t.blockStatement([t.returnStatement(t.identifier(identifierName))]),
|
|
232
|
+
false,
|
|
233
|
+
false,
|
|
234
|
+
false
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
// Not all properties need a setter
|
|
240
|
+
if (setterPropsNeeded.has(identifierName)) {
|
|
241
|
+
var valueArgName = me.getPlaceholder() + "_value";
|
|
242
|
+
flatObjectProperties.push(
|
|
243
|
+
me.skip(
|
|
244
|
+
t.objectMethod(
|
|
245
|
+
"set",
|
|
246
|
+
t.stringLiteral(objectProp),
|
|
247
|
+
[t.identifier(valueArgName)],
|
|
248
|
+
t.blockStatement([
|
|
249
|
+
t.expressionStatement(
|
|
250
|
+
t.assignmentExpression(
|
|
251
|
+
"=",
|
|
252
|
+
t.identifier(identifierName),
|
|
253
|
+
t.identifier(valueArgName)
|
|
254
|
+
)
|
|
255
|
+
),
|
|
256
|
+
]),
|
|
257
|
+
false,
|
|
258
|
+
false,
|
|
259
|
+
false
|
|
260
|
+
)
|
|
261
|
+
)
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
for (const entry of typeofProps) {
|
|
267
|
+
const [identifierName, objectProp] = entry;
|
|
268
|
+
|
|
269
|
+
flatObjectProperties.push(
|
|
270
|
+
me.skip(
|
|
271
|
+
t.objectMethod(
|
|
272
|
+
"get",
|
|
273
|
+
t.stringLiteral(objectProp),
|
|
274
|
+
[],
|
|
275
|
+
t.blockStatement([
|
|
276
|
+
t.returnStatement(
|
|
277
|
+
t.unaryExpression("typeof", t.identifier(identifierName))
|
|
278
|
+
),
|
|
279
|
+
]),
|
|
280
|
+
false,
|
|
281
|
+
false,
|
|
282
|
+
false
|
|
283
|
+
)
|
|
284
|
+
)
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
for (const entry of functionCallProps) {
|
|
289
|
+
const [identifierName, objectProp] = entry;
|
|
290
|
+
|
|
291
|
+
flatObjectProperties.push(
|
|
292
|
+
me.skip(
|
|
293
|
+
t.objectMethod(
|
|
294
|
+
"method",
|
|
295
|
+
t.stringLiteral(objectProp),
|
|
296
|
+
[t.restElement(t.identifier("args"))],
|
|
297
|
+
t.blockStatement([
|
|
298
|
+
t.returnStatement(
|
|
299
|
+
t.callExpression(t.identifier(identifierName), [
|
|
300
|
+
t.spreadElement(t.identifier("args")),
|
|
301
|
+
])
|
|
302
|
+
),
|
|
303
|
+
]),
|
|
304
|
+
false,
|
|
305
|
+
false,
|
|
306
|
+
false
|
|
307
|
+
)
|
|
308
|
+
)
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Create the new flattened function
|
|
313
|
+
const flattenedFunctionDeclaration = t.functionDeclaration(
|
|
314
|
+
t.identifier(newFnName),
|
|
315
|
+
[
|
|
316
|
+
t.arrayPattern([...(fnPath.node.params as t.FunctionParameter[])]),
|
|
317
|
+
t.identifier(flatObjectName),
|
|
318
|
+
],
|
|
319
|
+
t.blockStatement([...[...fnPath.node.body.body]]),
|
|
320
|
+
false,
|
|
321
|
+
fnPath.node.async
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
// Create the flat object variable declaration
|
|
325
|
+
const flatObjectDeclaration = t.variableDeclaration("var", [
|
|
326
|
+
t.variableDeclarator(
|
|
327
|
+
t.identifier(flatObjectName),
|
|
328
|
+
t.objectExpression(flatObjectProperties)
|
|
329
|
+
),
|
|
330
|
+
]);
|
|
331
|
+
|
|
332
|
+
var argName = me.getPlaceholder() + "_args";
|
|
333
|
+
|
|
334
|
+
// Replace original function body with a call to the flattened function
|
|
335
|
+
fnPath.node.body = t.blockStatement([
|
|
336
|
+
flatObjectDeclaration,
|
|
337
|
+
t.returnStatement(
|
|
338
|
+
t.callExpression(t.identifier(newFnName), [
|
|
339
|
+
t.identifier(argName),
|
|
340
|
+
t.identifier(flatObjectName),
|
|
341
|
+
])
|
|
342
|
+
),
|
|
343
|
+
]);
|
|
344
|
+
|
|
345
|
+
const originalLength = computeFunctionLength(fnPath);
|
|
346
|
+
fnPath.node.params = [t.restElement(t.identifier(argName))];
|
|
347
|
+
|
|
348
|
+
// Ensure updated parameter gets registered in the function scope
|
|
349
|
+
fnPath.scope.crawl();
|
|
350
|
+
fnPath.skip();
|
|
351
|
+
|
|
352
|
+
// Add the new flattened function at the top level
|
|
353
|
+
var newPath = prependProgram(
|
|
354
|
+
program,
|
|
355
|
+
flattenedFunctionDeclaration
|
|
356
|
+
)[0] as NodePath<t.FunctionDeclaration>;
|
|
357
|
+
|
|
358
|
+
me.skip(newPath);
|
|
359
|
+
|
|
360
|
+
// Copy over all properties except the predictable flag
|
|
361
|
+
for (var symbol of Object.getOwnPropertySymbols(fnPath.node)) {
|
|
362
|
+
if (symbol !== PREDICTABLE) {
|
|
363
|
+
newPath.node[symbol] = fnPath.node[symbol];
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Old function is no longer predictable (rest element parameter)
|
|
368
|
+
(fnPath.node as NodeSymbol)[PREDICTABLE] = false;
|
|
369
|
+
// Old function is unsafe (uses arguments, this)
|
|
370
|
+
(fnPath.node as NodeSymbol)[UNSAFE] = true;
|
|
371
|
+
|
|
372
|
+
newPath.node[PREDICTABLE] = true;
|
|
373
|
+
|
|
374
|
+
// Carry over 'use strict' directive if not already present
|
|
375
|
+
if (strictMode) {
|
|
376
|
+
newPath.node.body.directives.push(
|
|
377
|
+
t.directive(t.directiveLiteral("use strict"))
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
// Non-simple parameter list conversion
|
|
381
|
+
prepend(
|
|
382
|
+
newPath,
|
|
383
|
+
t.variableDeclaration("var", [
|
|
384
|
+
t.variableDeclarator(
|
|
385
|
+
t.arrayPattern(newPath.node.params),
|
|
386
|
+
t.identifier("arguments")
|
|
387
|
+
),
|
|
388
|
+
])
|
|
389
|
+
);
|
|
390
|
+
newPath.node.params = [];
|
|
391
|
+
// Using 'arguments' is unsafe
|
|
392
|
+
(newPath.node as NodeSymbol)[UNSAFE] = true;
|
|
393
|
+
// Params changed and using 'arguments'
|
|
394
|
+
(newPath.node as NodeSymbol)[PREDICTABLE] = false;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Ensure parameters are registered in the new function scope
|
|
398
|
+
newPath.scope.crawl();
|
|
399
|
+
|
|
400
|
+
newPath.skip();
|
|
401
|
+
me.skip(newPath);
|
|
402
|
+
|
|
403
|
+
// Set function length
|
|
404
|
+
me.setFunctionLength(fnPath, originalLength);
|
|
405
|
+
|
|
406
|
+
me.changeData.functions++;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return {
|
|
410
|
+
visitor: {
|
|
411
|
+
Function: {
|
|
412
|
+
exit(path: NodePath<t.Function>) {
|
|
413
|
+
flattenFunction(path);
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
Program(path) {
|
|
417
|
+
path.scope.crawl();
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
};
|