webfuscator 0.9.4
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/DISCLOSURE +9 -0
- package/LICENSE +21 -0
- package/README.md +83 -0
- package/dist/analysis/constant.d.ts +11 -0
- package/dist/analysis/constant.d.ts.map +1 -0
- package/dist/analysis/constant.js +261 -0
- package/dist/analysis/constant.js.map +1 -0
- package/dist/analysis/document-order.d.ts +5 -0
- package/dist/analysis/document-order.d.ts.map +1 -0
- package/dist/analysis/document-order.js +124 -0
- package/dist/analysis/document-order.js.map +1 -0
- package/dist/analysis/inlineability.d.ts +13 -0
- package/dist/analysis/inlineability.d.ts.map +1 -0
- package/dist/analysis/inlineability.js +270 -0
- package/dist/analysis/inlineability.js.map +1 -0
- package/dist/analysis/purity.d.ts +5 -0
- package/dist/analysis/purity.d.ts.map +1 -0
- package/dist/analysis/purity.js +281 -0
- package/dist/analysis/purity.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/obfuscator.d.ts +4 -0
- package/dist/obfuscator.d.ts.map +1 -0
- package/dist/obfuscator.js +222 -0
- package/dist/obfuscator.js.map +1 -0
- package/dist/options.d.ts +73 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +2 -0
- package/dist/options.js.map +1 -0
- package/dist/preparation/dot-to-bracket.d.ts +16 -0
- package/dist/preparation/dot-to-bracket.d.ts.map +1 -0
- package/dist/preparation/dot-to-bracket.js +36 -0
- package/dist/preparation/dot-to-bracket.js.map +1 -0
- package/dist/preparation/expand-destructuring.d.ts +26 -0
- package/dist/preparation/expand-destructuring.d.ts.map +1 -0
- package/dist/preparation/expand-destructuring.js +154 -0
- package/dist/preparation/expand-destructuring.js.map +1 -0
- package/dist/preparation/expand-template-literals.d.ts +23 -0
- package/dist/preparation/expand-template-literals.d.ts.map +1 -0
- package/dist/preparation/expand-template-literals.js +78 -0
- package/dist/preparation/expand-template-literals.js.map +1 -0
- package/dist/preparation/rewrite-class-fields.d.ts +43 -0
- package/dist/preparation/rewrite-class-fields.d.ts.map +1 -0
- package/dist/preparation/rewrite-class-fields.js +270 -0
- package/dist/preparation/rewrite-class-fields.js.map +1 -0
- package/dist/preparation/split-sequence-expressions.d.ts +15 -0
- package/dist/preparation/split-sequence-expressions.d.ts.map +1 -0
- package/dist/preparation/split-sequence-expressions.js +36 -0
- package/dist/preparation/split-sequence-expressions.js.map +1 -0
- package/dist/preparation/split-variable-declarations.d.ts +20 -0
- package/dist/preparation/split-variable-declarations.d.ts.map +1 -0
- package/dist/preparation/split-variable-declarations.js +36 -0
- package/dist/preparation/split-variable-declarations.js.map +1 -0
- package/dist/preparation/stringify-object-keys.d.ts +21 -0
- package/dist/preparation/stringify-object-keys.d.ts.map +1 -0
- package/dist/preparation/stringify-object-keys.js +96 -0
- package/dist/preparation/stringify-object-keys.js.map +1 -0
- package/dist/preparation/wrap-single-statements.d.ts +24 -0
- package/dist/preparation/wrap-single-statements.d.ts.map +1 -0
- package/dist/preparation/wrap-single-statements.js +53 -0
- package/dist/preparation/wrap-single-statements.js.map +1 -0
- package/dist/transforms/arguments-to-parameters.d.ts +20 -0
- package/dist/transforms/arguments-to-parameters.d.ts.map +1 -0
- package/dist/transforms/arguments-to-parameters.js +162 -0
- package/dist/transforms/arguments-to-parameters.js.map +1 -0
- package/dist/transforms/arrow-to-function.d.ts +46 -0
- package/dist/transforms/arrow-to-function.d.ts.map +1 -0
- package/dist/transforms/arrow-to-function.js +203 -0
- package/dist/transforms/arrow-to-function.js.map +1 -0
- package/dist/transforms/collapse-single-use-temps.d.ts +16 -0
- package/dist/transforms/collapse-single-use-temps.d.ts.map +1 -0
- package/dist/transforms/collapse-single-use-temps.js +805 -0
- package/dist/transforms/collapse-single-use-temps.js.map +1 -0
- package/dist/transforms/const-let-to-var.d.ts +21 -0
- package/dist/transforms/const-let-to-var.d.ts.map +1 -0
- package/dist/transforms/const-let-to-var.js +217 -0
- package/dist/transforms/const-let-to-var.js.map +1 -0
- package/dist/transforms/const-to-let.d.ts +17 -0
- package/dist/transforms/const-to-let.d.ts.map +1 -0
- package/dist/transforms/const-to-let.js +33 -0
- package/dist/transforms/const-to-let.js.map +1 -0
- package/dist/transforms/do-while-to-while.d.ts +26 -0
- package/dist/transforms/do-while-to-while.d.ts.map +1 -0
- package/dist/transforms/do-while-to-while.js +54 -0
- package/dist/transforms/do-while-to-while.js.map +1 -0
- package/dist/transforms/drop-console.d.ts +27 -0
- package/dist/transforms/drop-console.d.ts.map +1 -0
- package/dist/transforms/drop-console.js +107 -0
- package/dist/transforms/drop-console.js.map +1 -0
- package/dist/transforms/drop-debugger.d.ts +18 -0
- package/dist/transforms/drop-debugger.d.ts.map +1 -0
- package/dist/transforms/drop-debugger.js +34 -0
- package/dist/transforms/drop-debugger.js.map +1 -0
- package/dist/transforms/expand-binary-assignment.d.ts +26 -0
- package/dist/transforms/expand-binary-assignment.d.ts.map +1 -0
- package/dist/transforms/expand-binary-assignment.js +78 -0
- package/dist/transforms/expand-binary-assignment.js.map +1 -0
- package/dist/transforms/expand-logical-assignment.d.ts +22 -0
- package/dist/transforms/expand-logical-assignment.d.ts.map +1 -0
- package/dist/transforms/expand-logical-assignment.js +73 -0
- package/dist/transforms/expand-logical-assignment.js.map +1 -0
- package/dist/transforms/extract-object-properties.d.ts +21 -0
- package/dist/transforms/extract-object-properties.d.ts.map +1 -0
- package/dist/transforms/extract-object-properties.js +340 -0
- package/dist/transforms/extract-object-properties.js.map +1 -0
- package/dist/transforms/fold-builtin-methods.d.ts +23 -0
- package/dist/transforms/fold-builtin-methods.d.ts.map +1 -0
- package/dist/transforms/fold-builtin-methods.js +839 -0
- package/dist/transforms/fold-builtin-methods.js.map +1 -0
- package/dist/transforms/fold-constants.d.ts +16 -0
- package/dist/transforms/fold-constants.d.ts.map +1 -0
- package/dist/transforms/fold-constants.js +421 -0
- package/dist/transforms/fold-constants.js.map +1 -0
- package/dist/transforms/for-to-while.d.ts +29 -0
- package/dist/transforms/for-to-while.d.ts.map +1 -0
- package/dist/transforms/for-to-while.js +91 -0
- package/dist/transforms/for-to-while.js.map +1 -0
- package/dist/transforms/function-declaration-to-expression.d.ts +28 -0
- package/dist/transforms/function-declaration-to-expression.d.ts.map +1 -0
- package/dist/transforms/function-declaration-to-expression.js +95 -0
- package/dist/transforms/function-declaration-to-expression.js.map +1 -0
- package/dist/transforms/inline-functions.d.ts +16 -0
- package/dist/transforms/inline-functions.d.ts.map +1 -0
- package/dist/transforms/inline-functions.js +896 -0
- package/dist/transforms/inline-functions.js.map +1 -0
- package/dist/transforms/logical-to-ternary.d.ts +20 -0
- package/dist/transforms/logical-to-ternary.d.ts.map +1 -0
- package/dist/transforms/logical-to-ternary.js +78 -0
- package/dist/transforms/logical-to-ternary.js.map +1 -0
- package/dist/transforms/mangle-properties.d.ts +27 -0
- package/dist/transforms/mangle-properties.d.ts.map +1 -0
- package/dist/transforms/mangle-properties.js +703 -0
- package/dist/transforms/mangle-properties.js.map +1 -0
- package/dist/transforms/nullish-coalescing-to-ternary.d.ts +18 -0
- package/dist/transforms/nullish-coalescing-to-ternary.d.ts.map +1 -0
- package/dist/transforms/nullish-coalescing-to-ternary.js +61 -0
- package/dist/transforms/nullish-coalescing-to-ternary.js.map +1 -0
- package/dist/transforms/numbers-to-strings.d.ts +19 -0
- package/dist/transforms/numbers-to-strings.d.ts.map +1 -0
- package/dist/transforms/numbers-to-strings.js +51 -0
- package/dist/transforms/numbers-to-strings.js.map +1 -0
- package/dist/transforms/object-method-to-property.d.ts +34 -0
- package/dist/transforms/object-method-to-property.d.ts.map +1 -0
- package/dist/transforms/object-method-to-property.js +78 -0
- package/dist/transforms/object-method-to-property.js.map +1 -0
- package/dist/transforms/optional-chaining-to-ternary.d.ts +19 -0
- package/dist/transforms/optional-chaining-to-ternary.d.ts.map +1 -0
- package/dist/transforms/optional-chaining-to-ternary.js +206 -0
- package/dist/transforms/optional-chaining-to-ternary.js.map +1 -0
- package/dist/transforms/pack-declarations-into-parameters.d.ts +22 -0
- package/dist/transforms/pack-declarations-into-parameters.d.ts.map +1 -0
- package/dist/transforms/pack-declarations-into-parameters.js +208 -0
- package/dist/transforms/pack-declarations-into-parameters.js.map +1 -0
- package/dist/transforms/remove-anonymous-function-names.d.ts +17 -0
- package/dist/transforms/remove-anonymous-function-names.d.ts.map +1 -0
- package/dist/transforms/remove-anonymous-function-names.js +72 -0
- package/dist/transforms/remove-anonymous-function-names.js.map +1 -0
- package/dist/transforms/remove-unreachable-code.d.ts +21 -0
- package/dist/transforms/remove-unreachable-code.d.ts.map +1 -0
- package/dist/transforms/remove-unreachable-code.js +103 -0
- package/dist/transforms/remove-unreachable-code.js.map +1 -0
- package/dist/transforms/remove-unused-code.d.ts +18 -0
- package/dist/transforms/remove-unused-code.d.ts.map +1 -0
- package/dist/transforms/remove-unused-code.js +249 -0
- package/dist/transforms/remove-unused-code.js.map +1 -0
- package/dist/transforms/rename-identifiers.d.ts +24 -0
- package/dist/transforms/rename-identifiers.d.ts.map +1 -0
- package/dist/transforms/rename-identifiers.js +264 -0
- package/dist/transforms/rename-identifiers.js.map +1 -0
- package/dist/transforms/specials-to-strings.d.ts +30 -0
- package/dist/transforms/specials-to-strings.d.ts.map +1 -0
- package/dist/transforms/specials-to-strings.js +101 -0
- package/dist/transforms/specials-to-strings.js.map +1 -0
- package/dist/transforms/switch-to-if.d.ts +28 -0
- package/dist/transforms/switch-to-if.d.ts.map +1 -0
- package/dist/transforms/switch-to-if.js +279 -0
- package/dist/transforms/switch-to-if.js.map +1 -0
- package/dist/transforms/ternary-to-if.d.ts +20 -0
- package/dist/transforms/ternary-to-if.d.ts.map +1 -0
- package/dist/transforms/ternary-to-if.js +315 -0
- package/dist/transforms/ternary-to-if.js.map +1 -0
- package/dist/transforms/update-to-assignment.d.ts +26 -0
- package/dist/transforms/update-to-assignment.d.ts.map +1 -0
- package/dist/transforms/update-to-assignment.js +120 -0
- package/dist/transforms/update-to-assignment.js.map +1 -0
- package/dist/transforms/yodify-conditions.d.ts +22 -0
- package/dist/transforms/yodify-conditions.d.ts.map +1 -0
- package/dist/transforms/yodify-conditions.js +91 -0
- package/dist/transforms/yodify-conditions.js.map +1 -0
- package/dist/utils/ast.d.ts +10 -0
- package/dist/utils/ast.d.ts.map +1 -0
- package/dist/utils/ast.js +112 -0
- package/dist/utils/ast.js.map +1 -0
- package/dist/utils/change-tracking.d.ts +8 -0
- package/dist/utils/change-tracking.d.ts.map +1 -0
- package/dist/utils/change-tracking.js +8 -0
- package/dist/utils/change-tracking.js.map +1 -0
- package/dist/utils/dom-properties.d.ts +2 -0
- package/dist/utils/dom-properties.d.ts.map +1 -0
- package/dist/utils/dom-properties.js +9252 -0
- package/dist/utils/dom-properties.js.map +1 -0
- package/dist/utils/evaluation-order.d.ts +4 -0
- package/dist/utils/evaluation-order.d.ts.map +1 -0
- package/dist/utils/evaluation-order.js +152 -0
- package/dist/utils/evaluation-order.js.map +1 -0
- package/dist/utils/literal.d.ts +5 -0
- package/dist/utils/literal.d.ts.map +1 -0
- package/dist/utils/literal.js +90 -0
- package/dist/utils/literal.js.map +1 -0
- package/dist/utils/logical-lowering.d.ts +4 -0
- package/dist/utils/logical-lowering.d.ts.map +1 -0
- package/dist/utils/logical-lowering.js +104 -0
- package/dist/utils/logical-lowering.js.map +1 -0
- package/dist/utils/loop-lowering.d.ts +23 -0
- package/dist/utils/loop-lowering.d.ts.map +1 -0
- package/dist/utils/loop-lowering.js +88 -0
- package/dist/utils/loop-lowering.js.map +1 -0
- package/dist/utils/paths.d.ts +9 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +90 -0
- package/dist/utils/paths.js.map +1 -0
- package/dist/utils/random.d.ts +3 -0
- package/dist/utils/random.d.ts.map +1 -0
- package/dist/utils/random.js +22 -0
- package/dist/utils/random.js.map +1 -0
- package/dist/utils/string-generator.d.ts +37 -0
- package/dist/utils/string-generator.d.ts.map +1 -0
- package/dist/utils/string-generator.js +175 -0
- package/dist/utils/string-generator.js.map +1 -0
- package/package.json +76 -0
- package/src/analysis/constant.ts +310 -0
- package/src/analysis/document-order.ts +141 -0
- package/src/analysis/inlineability.ts +336 -0
- package/src/analysis/purity.ts +315 -0
- package/src/index.ts +2 -0
- package/src/obfuscator.ts +271 -0
- package/src/options.ts +114 -0
- package/src/preparation/dot-to-bracket.ts +41 -0
- package/src/preparation/expand-destructuring.ts +211 -0
- package/src/preparation/expand-template-literals.ts +92 -0
- package/src/preparation/rewrite-class-fields.ts +320 -0
- package/src/preparation/split-sequence-expressions.ts +40 -0
- package/src/preparation/split-variable-declarations.ts +39 -0
- package/src/preparation/stringify-object-keys.ts +107 -0
- package/src/preparation/wrap-single-statements.ts +63 -0
- package/src/transforms/arguments-to-parameters.ts +187 -0
- package/src/transforms/arrow-to-function.ts +225 -0
- package/src/transforms/collapse-single-use-temps.ts +957 -0
- package/src/transforms/const-let-to-var.ts +246 -0
- package/src/transforms/const-to-let.ts +38 -0
- package/src/transforms/do-while-to-while.ts +69 -0
- package/src/transforms/drop-console.ts +119 -0
- package/src/transforms/drop-debugger.ts +38 -0
- package/src/transforms/expand-binary-assignment.ts +94 -0
- package/src/transforms/expand-logical-assignment.ts +84 -0
- package/src/transforms/extract-object-properties.ts +404 -0
- package/src/transforms/fold-builtin-methods.ts +949 -0
- package/src/transforms/fold-constants.ts +462 -0
- package/src/transforms/for-to-while.ts +109 -0
- package/src/transforms/function-declaration-to-expression.ts +108 -0
- package/src/transforms/inline-functions.ts +1074 -0
- package/src/transforms/logical-to-ternary.ts +92 -0
- package/src/transforms/mangle-properties.ts +819 -0
- package/src/transforms/nullish-coalescing-to-ternary.ts +77 -0
- package/src/transforms/numbers-to-strings.ts +61 -0
- package/src/transforms/object-method-to-property.ts +86 -0
- package/src/transforms/optional-chaining-to-ternary.ts +225 -0
- package/src/transforms/pack-declarations-into-parameters.ts +233 -0
- package/src/transforms/remove-anonymous-function-names.ts +84 -0
- package/src/transforms/remove-unreachable-code.ts +115 -0
- package/src/transforms/remove-unused-code.ts +296 -0
- package/src/transforms/rename-identifiers.ts +289 -0
- package/src/transforms/specials-to-strings.ts +116 -0
- package/src/transforms/switch-to-if.ts +339 -0
- package/src/transforms/ternary-to-if.ts +387 -0
- package/src/transforms/update-to-assignment.ts +132 -0
- package/src/transforms/yodify-conditions.ts +103 -0
- package/src/utils/ast.ts +129 -0
- package/src/utils/change-tracking.ts +15 -0
- package/src/utils/dom-properties.ts +9252 -0
- package/src/utils/evaluation-order.ts +178 -0
- package/src/utils/literal.ts +96 -0
- package/src/utils/logical-lowering.ts +132 -0
- package/src/utils/loop-lowering.ts +109 -0
- package/src/utils/paths.ts +103 -0
- package/src/utils/random.ts +22 -0
- package/src/utils/string-generator.ts +206 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import generate from '@babel/generator';
|
|
2
|
+
import { parse } from '@babel/parser';
|
|
3
|
+
import { dotToBracket } from './preparation/dot-to-bracket.js';
|
|
4
|
+
import { expandDestructuring } from './preparation/expand-destructuring.js';
|
|
5
|
+
import { expandTemplateLiterals } from './preparation/expand-template-literals.js';
|
|
6
|
+
import { rewriteClassFields } from './preparation/rewrite-class-fields.js';
|
|
7
|
+
import { splitSequenceExpressions } from './preparation/split-sequence-expressions.js';
|
|
8
|
+
import { splitVariableDeclarations } from './preparation/split-variable-declarations.js';
|
|
9
|
+
import { stringifyObjectKeys } from './preparation/stringify-object-keys.js';
|
|
10
|
+
import { wrapSingleStatements } from './preparation/wrap-single-statements.js';
|
|
11
|
+
import { argumentsToParameters } from './transforms/arguments-to-parameters.js';
|
|
12
|
+
import { arrowToFunction } from './transforms/arrow-to-function.js';
|
|
13
|
+
import { collapseSingleUseTemps } from './transforms/collapse-single-use-temps.js';
|
|
14
|
+
import { constLetToVar } from './transforms/const-let-to-var.js';
|
|
15
|
+
import { constToLet } from './transforms/const-to-let.js';
|
|
16
|
+
import { doWhileToWhile } from './transforms/do-while-to-while.js';
|
|
17
|
+
import { dropConsole } from './transforms/drop-console.js';
|
|
18
|
+
import { dropDebugger } from './transforms/drop-debugger.js';
|
|
19
|
+
import { expandBinaryAssignment } from './transforms/expand-binary-assignment.js';
|
|
20
|
+
import { expandLogicalAssignment } from './transforms/expand-logical-assignment.js';
|
|
21
|
+
import { extractObjectProperties } from './transforms/extract-object-properties.js';
|
|
22
|
+
import { foldBuiltinMethods } from './transforms/fold-builtin-methods.js';
|
|
23
|
+
import { foldConstants } from './transforms/fold-constants.js';
|
|
24
|
+
import { forToWhile } from './transforms/for-to-while.js';
|
|
25
|
+
import { functionDeclarationToExpression } from './transforms/function-declaration-to-expression.js';
|
|
26
|
+
import { inlineFunctions } from './transforms/inline-functions.js';
|
|
27
|
+
import { logicalToTernary } from './transforms/logical-to-ternary.js';
|
|
28
|
+
import { mangleProperties } from './transforms/mangle-properties.js';
|
|
29
|
+
import { nullishCoalescingToTernary } from './transforms/nullish-coalescing-to-ternary.js';
|
|
30
|
+
import { numbersToStrings } from './transforms/numbers-to-strings.js';
|
|
31
|
+
import { objectMethodToProperty } from './transforms/object-method-to-property.js';
|
|
32
|
+
import { optionalChainingToTernary } from './transforms/optional-chaining-to-ternary.js';
|
|
33
|
+
import { packDeclarationsIntoParameters } from './transforms/pack-declarations-into-parameters.js';
|
|
34
|
+
import { removeAnonymousFunctionNames } from './transforms/remove-anonymous-function-names.js';
|
|
35
|
+
import { removeUnreachableCode } from './transforms/remove-unreachable-code.js';
|
|
36
|
+
import { removeUnusedCode } from './transforms/remove-unused-code.js';
|
|
37
|
+
import { renameIdentifiers } from './transforms/rename-identifiers.js';
|
|
38
|
+
import { specialsToStrings } from './transforms/specials-to-strings.js';
|
|
39
|
+
import { switchToIf } from './transforms/switch-to-if.js';
|
|
40
|
+
import { ternaryToIf } from './transforms/ternary-to-if.js';
|
|
41
|
+
import { updateToAssignment } from './transforms/update-to-assignment.js';
|
|
42
|
+
import { yodifyConditions } from './transforms/yodify-conditions.js';
|
|
43
|
+
const parserOptions = { sourceType: 'unambiguous' };
|
|
44
|
+
const PRESERVED_COMMENT_RE = /^!|@(?:license|preserve)\b|^#\s*source(?:Mapping)?URL=/iu;
|
|
45
|
+
const DEFAULT_STRING_GENERATOR_MODE = 'mangled';
|
|
46
|
+
const DEFAULT_SEED = 0;
|
|
47
|
+
// Real inputs settle in a handful of iterations. Hitting this cap signals an
|
|
48
|
+
// algorithmic bug; never raise it as a workaround.
|
|
49
|
+
const FIXED_POINT_ITERATION_CAP = 10;
|
|
50
|
+
// Mangling must see quoted access and class fields before preparation erases
|
|
51
|
+
// those shapes. It is opt-in because outside code may use the same properties.
|
|
52
|
+
const BEFORE_PREPARATION = [['mangleProperties', mangleProperties]];
|
|
53
|
+
// Shape-normalizing passes. Always run; never exposed in `options.transforms`.
|
|
54
|
+
const PREPARATION = [
|
|
55
|
+
['rewriteClassFields', asBoolean(rewriteClassFields)],
|
|
56
|
+
['expandDestructuring', asBoolean(expandDestructuring)],
|
|
57
|
+
['wrapSingleStatements', asBoolean(wrapSingleStatements)],
|
|
58
|
+
['splitSequenceExpressions', asBoolean(splitSequenceExpressions)],
|
|
59
|
+
['splitVariableDeclarations', asBoolean(splitVariableDeclarations)],
|
|
60
|
+
['expandTemplateLiterals', asBoolean(expandTemplateLiterals)],
|
|
61
|
+
['stringifyObjectKeys', asBoolean(stringifyObjectKeys)],
|
|
62
|
+
['dotToBracket', asBoolean(dotToBracket)],
|
|
63
|
+
];
|
|
64
|
+
// These one-shot passes prepare the fixed point. Lexical declarations become
|
|
65
|
+
// foldable function-scoped bindings, except at script top level where `var`
|
|
66
|
+
// would create a global property. `argumentsToParameters` precedes parameter
|
|
67
|
+
// renaming. Method demotion precedes extraction because any surviving
|
|
68
|
+
// `ObjectMethod` makes extraction refuse the whole literal.
|
|
69
|
+
const PRE_LOOP = [
|
|
70
|
+
['dropDebugger', dropDebugger],
|
|
71
|
+
['dropConsole', dropConsole],
|
|
72
|
+
['constToLet', constToLet],
|
|
73
|
+
['constLetToVar', constLetToVar],
|
|
74
|
+
['argumentsToParameters', argumentsToParameters],
|
|
75
|
+
['objectMethodToProperty', objectMethodToProperty],
|
|
76
|
+
];
|
|
77
|
+
// Folds, removals, extraction, and inlining expose work for one another. Stop
|
|
78
|
+
// when a full round makes no change.
|
|
79
|
+
const FIXED_POINT = [
|
|
80
|
+
['foldConstants', foldConstants],
|
|
81
|
+
['foldBuiltinMethods', foldBuiltinMethods],
|
|
82
|
+
['extractObjectProperties', extractObjectProperties],
|
|
83
|
+
['inlineFunctions', inlineFunctions],
|
|
84
|
+
['removeUnreachableCode', removeUnreachableCode],
|
|
85
|
+
['removeUnusedCode', removeUnusedCode],
|
|
86
|
+
['removeAnonymousFunctionNames', removeAnonymousFunctionNames],
|
|
87
|
+
['collapseSingleUseTemps', collapseSingleUseTemps],
|
|
88
|
+
];
|
|
89
|
+
// Lowerings run from producer to consumer. Arrow capture lands before code can
|
|
90
|
+
// move out of an arrow. Function declarations remain available to the inliner
|
|
91
|
+
// until the fixed point ends. Loop passes converge on `while`. Updates become
|
|
92
|
+
// compound assignments before binary assignment expansion. Logical assignment,
|
|
93
|
+
// optional chaining, nullish coalescing, logical expressions, and ternaries then
|
|
94
|
+
// lower in that order.
|
|
95
|
+
const LOWERING = [
|
|
96
|
+
['arrowToFunction', arrowToFunction],
|
|
97
|
+
['functionDeclarationToExpression', functionDeclarationToExpression],
|
|
98
|
+
['switchToIf', switchToIf],
|
|
99
|
+
['forToWhile', forToWhile],
|
|
100
|
+
['doWhileToWhile', doWhileToWhile],
|
|
101
|
+
['updateToAssignment', updateToAssignment],
|
|
102
|
+
['expandBinaryAssignment', expandBinaryAssignment],
|
|
103
|
+
['expandLogicalAssignment', expandLogicalAssignment],
|
|
104
|
+
['optionalChainingToTernary', optionalChainingToTernary],
|
|
105
|
+
['nullishCoalescingToTernary', nullishCoalescingToTernary],
|
|
106
|
+
['logicalToTernary', logicalToTernary],
|
|
107
|
+
['ternaryToIf', ternaryToIf],
|
|
108
|
+
];
|
|
109
|
+
// Yodification and folding need raw numeric literals. Special-value lowering
|
|
110
|
+
// comes next, then number lowering also handles the `1` created for `Infinity`.
|
|
111
|
+
const POST_PROCESS = [
|
|
112
|
+
['packDeclarationsIntoParameters', packDeclarationsIntoParameters],
|
|
113
|
+
['yodifyConditions', yodifyConditions],
|
|
114
|
+
['specialsToStrings', specialsToStrings],
|
|
115
|
+
['numbersToStrings', numbersToStrings],
|
|
116
|
+
];
|
|
117
|
+
// Renaming runs last because later transforms depend on recognizable bindings.
|
|
118
|
+
const RENAME = [['renameIdentifiers', renameIdentifiers]];
|
|
119
|
+
/** Parses, transforms, and prints JavaScript without mutating external state. */
|
|
120
|
+
export function obfuscate(code, options = {}) {
|
|
121
|
+
const log = options.verbose ? makeLogger() : null;
|
|
122
|
+
const baseCtx = {
|
|
123
|
+
seed: options.seed ?? DEFAULT_SEED,
|
|
124
|
+
stringGeneratorMode: options.stringGeneratorMode ?? DEFAULT_STRING_GENERATOR_MODE,
|
|
125
|
+
};
|
|
126
|
+
const ast = run(log, 'parse', () => parse(code, parserOptions));
|
|
127
|
+
for (const [name, fn] of BEFORE_PREPARATION) {
|
|
128
|
+
if (!isEnabled(name, options)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
run(log, name, () => fn(ast, ctxFor(name, options, baseCtx)));
|
|
132
|
+
}
|
|
133
|
+
for (const [name, fn] of PREPARATION) {
|
|
134
|
+
run(log, name, () => fn(ast, baseCtx));
|
|
135
|
+
}
|
|
136
|
+
for (const [name, fn] of PRE_LOOP) {
|
|
137
|
+
if (!isEnabled(name, options)) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
run(log, name, () => fn(ast, ctxFor(name, options, baseCtx)));
|
|
141
|
+
}
|
|
142
|
+
runFixedPoint(ast, options, baseCtx, log);
|
|
143
|
+
for (const [name, fn] of LOWERING) {
|
|
144
|
+
if (!isEnabled(name, options)) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
run(log, name, () => fn(ast, ctxFor(name, options, baseCtx)));
|
|
148
|
+
}
|
|
149
|
+
for (const [name, fn] of POST_PROCESS) {
|
|
150
|
+
if (!isEnabled(name, options)) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
run(log, name, () => fn(ast, ctxFor(name, options, baseCtx)));
|
|
154
|
+
}
|
|
155
|
+
for (const [name, fn] of RENAME) {
|
|
156
|
+
if (!isEnabled(name, options)) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
run(log, name, () => fn(ast, ctxFor(name, options, baseCtx)));
|
|
160
|
+
}
|
|
161
|
+
return run(log, 'generate', () => generate(ast, { shouldPrintComment }).code);
|
|
162
|
+
}
|
|
163
|
+
function isEnabled(name, options) {
|
|
164
|
+
if (name === 'mangleProperties') {
|
|
165
|
+
return (options.transforms?.mangleProperties !== undefined &&
|
|
166
|
+
options.transforms.mangleProperties !== false);
|
|
167
|
+
}
|
|
168
|
+
return options.transforms?.[name] !== false;
|
|
169
|
+
}
|
|
170
|
+
// Boolean entries inherit the base. An override replaces its string mode.
|
|
171
|
+
function ctxFor(name, options, base) {
|
|
172
|
+
const entry = options.transforms?.[name];
|
|
173
|
+
let context = base;
|
|
174
|
+
if (entry && typeof entry === 'object' && entry.stringGeneratorMode !== undefined) {
|
|
175
|
+
context = { ...base, stringGeneratorMode: entry.stringGeneratorMode };
|
|
176
|
+
}
|
|
177
|
+
if (name === 'mangleProperties' && entry && typeof entry === 'object') {
|
|
178
|
+
return { ...context, mangleProperties: entry };
|
|
179
|
+
}
|
|
180
|
+
return context;
|
|
181
|
+
}
|
|
182
|
+
function runFixedPoint(ast, options, ctx, log) {
|
|
183
|
+
const enabled = FIXED_POINT.filter(([name]) => isEnabled(name, options));
|
|
184
|
+
if (enabled.length === 0) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
for (let iter = 0; iter < FIXED_POINT_ITERATION_CAP; iter++) {
|
|
188
|
+
let any = false;
|
|
189
|
+
for (const [name, fn] of enabled) {
|
|
190
|
+
if (run(log, name, () => fn(ast, ctx))) {
|
|
191
|
+
any = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (!any) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function asBoolean(fn) {
|
|
200
|
+
return (ast) => {
|
|
201
|
+
fn(ast);
|
|
202
|
+
return false;
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function shouldPrintComment(value) {
|
|
206
|
+
return PRESERVED_COMMENT_RE.test(value);
|
|
207
|
+
}
|
|
208
|
+
function run(log, name, fn) {
|
|
209
|
+
return log ? log(name, fn) : fn();
|
|
210
|
+
}
|
|
211
|
+
function makeLogger() {
|
|
212
|
+
const t0 = performance.now();
|
|
213
|
+
const stamp = () => `[${(performance.now() - t0).toFixed(0).padStart(6)}ms]`;
|
|
214
|
+
return (name, fn) => {
|
|
215
|
+
console.error(`${stamp()} > ${name}`);
|
|
216
|
+
const start = performance.now();
|
|
217
|
+
const result = fn();
|
|
218
|
+
console.error(`${stamp()} < ${name} (${(performance.now() - start).toFixed(1)}ms)`);
|
|
219
|
+
return result;
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=obfuscator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"obfuscator.js","sourceRoot":"","sources":["../src/obfuscator.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AAKrC,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAA;AAC7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAA;AAC1E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAA;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAA;AACzE,OAAO,EA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,EAAE,CAAC,GAAG,CAAC,CAAA;QACP,OAAO,KAAK,CAAA;IACd,CAAC,CAAA;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAa;IACvC,OAAO,oBAAoB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACzC,CAAC;AAID,SAAS,GAAG,CAAI,GAAkB,EAAE,IAAY,EAAE,EAAW;IAC3D,OAAO,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAA;AACnC,CAAC;AAED,SAAS,UAAU;IACjB,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,EAAE,CAAA;IAC5B,MAAM,KAAK,GAAG,GAAW,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAA;IACpF,OAAO,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE;QAClB,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,EAAE,MAAM,IAAI,EAAE,CAAC,CAAA;QACrC,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAA;QAC/B,MAAM,MAAM,GAAG,EAAE,EAAE,CAAA;QACnB,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,EAAE,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;QACnF,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { StringGeneratorModeOption } from './utils/string-generator.js';
|
|
2
|
+
/**
|
|
3
|
+
* Exported transform entry points. Preparation passes are always enabled and
|
|
4
|
+
* do not appear here.
|
|
5
|
+
*/
|
|
6
|
+
export type TransformName = 'dropConsole' | 'dropDebugger' | 'constToLet' | 'constLetToVar' | 'argumentsToParameters' | 'objectMethodToProperty' | 'foldConstants' | 'foldBuiltinMethods' | 'extractObjectProperties' | 'inlineFunctions' | 'removeUnreachableCode' | 'removeUnusedCode' | 'removeAnonymousFunctionNames' | 'collapseSingleUseTemps' | 'arrowToFunction' | 'functionDeclarationToExpression' | 'switchToIf' | 'forToWhile' | 'doWhileToWhile' | 'updateToAssignment' | 'expandBinaryAssignment' | 'expandLogicalAssignment' | 'optionalChainingToTernary' | 'nullishCoalescingToTernary' | 'logicalToTernary' | 'ternaryToIf' | 'packDeclarationsIntoParameters' | 'yodifyConditions' | 'specialsToStrings' | 'numbersToStrings' | 'mangleProperties' | 'renameIdentifiers';
|
|
7
|
+
type TransformEntry = boolean | {
|
|
8
|
+
stringGeneratorMode?: StringGeneratorModeOption;
|
|
9
|
+
};
|
|
10
|
+
/** Maps a zero-based ordinal to a stable property name. */
|
|
11
|
+
export type PropertyNameGenerator = (index: number) => string;
|
|
12
|
+
/** Terser-compatible property-mangling options using camel-case names. */
|
|
13
|
+
export interface ManglePropertiesOptions {
|
|
14
|
+
/** Allow names of JavaScript and DOM builtins to be mangled. Default `false`. */
|
|
15
|
+
builtins?: boolean;
|
|
16
|
+
/** Reuse and extend mappings across calls or separately transformed files. */
|
|
17
|
+
cache?: Map<string, string>;
|
|
18
|
+
/** Keep the original name visible as `_$name$suffix_`. Default `false`. */
|
|
19
|
+
debug?: boolean | string;
|
|
20
|
+
/**
|
|
21
|
+
* Preserve quoted occurrences. `true` also reserves their names globally;
|
|
22
|
+
* `'strict'` lets unquoted occurrences of the same name be mangled.
|
|
23
|
+
*/
|
|
24
|
+
keepQuoted?: boolean | 'strict';
|
|
25
|
+
/** Custom property-name generator. It takes precedence over `stringGeneratorMode`. */
|
|
26
|
+
nameGenerator?: PropertyNameGenerator;
|
|
27
|
+
/**
|
|
28
|
+
* Only names carrying `/*@__MANGLE_PROP__*\/` or `/*#__MANGLE_PROP__*\/` may
|
|
29
|
+
* be mangled. Default `false`.
|
|
30
|
+
*/
|
|
31
|
+
onlyAnnotated?: boolean;
|
|
32
|
+
/** Only names already present in `cache` may be mangled. Default `false`. */
|
|
33
|
+
onlyCache?: boolean;
|
|
34
|
+
/** Only property names matching this expression may be mangled. */
|
|
35
|
+
regex?: RegExp | string;
|
|
36
|
+
/** Property names that must not be mangled. */
|
|
37
|
+
reserved?: readonly string[];
|
|
38
|
+
/** Override the default string-generator mode for property names. */
|
|
39
|
+
stringGeneratorMode?: StringGeneratorModeOption;
|
|
40
|
+
/** Mangle accesses rooted at undeclared identifiers. Default `false`. */
|
|
41
|
+
undeclared?: boolean;
|
|
42
|
+
}
|
|
43
|
+
type SimpleTransform = Exclude<TransformName, 'mangleProperties' | 'renameIdentifiers' | 'specialsToStrings'>;
|
|
44
|
+
type TransformsMap = Partial<Record<SimpleTransform, boolean>> & {
|
|
45
|
+
mangleProperties?: boolean | ManglePropertiesOptions;
|
|
46
|
+
renameIdentifiers?: TransformEntry;
|
|
47
|
+
specialsToStrings?: TransformEntry;
|
|
48
|
+
};
|
|
49
|
+
export interface ObfuscatorOptions {
|
|
50
|
+
/** Logs each pipeline step and its elapsed milliseconds to stderr. */
|
|
51
|
+
verbose?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Per-transform toggles and string-generator overrides. `mangleProperties` is
|
|
54
|
+
* opt-in because property names can cross the program boundary. Other omitted
|
|
55
|
+
* transforms default to enabled.
|
|
56
|
+
*/
|
|
57
|
+
transforms?: TransformsMap;
|
|
58
|
+
/**
|
|
59
|
+
* Default mode for generated identifiers, labels, and string contents. An
|
|
60
|
+
* array mixes modes uniformly. Individual transforms may override it through
|
|
61
|
+
* `transforms.<name>.stringGeneratorMode`. Default `'mangled'`.
|
|
62
|
+
*/
|
|
63
|
+
stringGeneratorMode?: StringGeneratorModeOption;
|
|
64
|
+
/** Seed for any nondeterministic transform. Same seed, same output. Default `0`. */
|
|
65
|
+
seed?: number;
|
|
66
|
+
}
|
|
67
|
+
export interface TransformContext {
|
|
68
|
+
mangleProperties?: ManglePropertiesOptions;
|
|
69
|
+
seed: number;
|
|
70
|
+
stringGeneratorMode: StringGeneratorModeOption;
|
|
71
|
+
}
|
|
72
|
+
export {};
|
|
73
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAA;AAE3E;;;GAGG;AACH,MAAM,MAAM,aAAa,GACrB,aAAa,GACb,cAAc,GACd,YAAY,GACZ,eAAe,GACf,uBAAuB,GACvB,wBAAwB,GACxB,eAAe,GACf,oBAAoB,GACpB,yBAAyB,GACzB,iBAAiB,GACjB,uBAAuB,GACvB,kBAAkB,GAClB,8BAA8B,GAC9B,wBAAwB,GACxB,iBAAiB,GACjB,iCAAiC,GACjC,YAAY,GACZ,YAAY,GACZ,gBAAgB,GAChB,oBAAoB,GACpB,wBAAwB,GACxB,yBAAyB,GACzB,2BAA2B,GAC3B,4BAA4B,GAC5B,kBAAkB,GAClB,aAAa,GACb,gCAAgC,GAChC,kBAAkB,GAClB,mBAAmB,GACnB,kBAAkB,GAClB,kBAAkB,GAClB,mBAAmB,CAAA;AAGvB,KAAK,cAAc,GAAG,OAAO,GAAG;IAAE,mBAAmB,CAAC,EAAE,yBAAyB,CAAA;CAAE,CAAA;AAEnF,2DAA2D;AAC3D,MAAM,MAAM,qBAAqB,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAA;AAE7D,0EAA0E;AAC1E,MAAM,WAAW,uBAAuB;IACtC,iFAAiF;IACjF,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC3B,2EAA2E;IAC3E,KAAK,CAAC,EAAE,OAAO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1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { File } from '@babel/types';
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites `obj.foo` as `obj["foo"]`. Skips already-computed access and
|
|
4
|
+
* private fields; private-name semantics aren't expressible as a string key.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // ◀️ before
|
|
8
|
+
* user.name = obj.foo.bar;
|
|
9
|
+
* var v = maybe?.value;
|
|
10
|
+
*
|
|
11
|
+
* // ▶️ after
|
|
12
|
+
* user["name"] = obj["foo"]["bar"];
|
|
13
|
+
* var v = maybe?.["value"];
|
|
14
|
+
*/
|
|
15
|
+
export declare function dotToBracket(ast: File): void;
|
|
16
|
+
//# sourceMappingURL=dot-to-bracket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dot-to-bracket.d.ts","sourceRoot":"","sources":["../../src/preparation/dot-to-bracket.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAExC;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,IAAI,GAAG,IAAI,CAE5C"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import traverse from '@babel/traverse';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
/**
|
|
4
|
+
* Rewrites `obj.foo` as `obj["foo"]`. Skips already-computed access and
|
|
5
|
+
* private fields; private-name semantics aren't expressible as a string key.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // ◀️ before
|
|
9
|
+
* user.name = obj.foo.bar;
|
|
10
|
+
* var v = maybe?.value;
|
|
11
|
+
*
|
|
12
|
+
* // ▶️ after
|
|
13
|
+
* user["name"] = obj["foo"]["bar"];
|
|
14
|
+
* var v = maybe?.["value"];
|
|
15
|
+
*/
|
|
16
|
+
export function dotToBracket(ast) {
|
|
17
|
+
traverse(ast, visitor);
|
|
18
|
+
}
|
|
19
|
+
const visitor = {
|
|
20
|
+
'MemberExpression|OptionalMemberExpression'(path) {
|
|
21
|
+
convert(path.node);
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
function convert(node) {
|
|
25
|
+
if (node.computed) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (!t.isIdentifier(node.property)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Babel 8 narrows members by `computed`, so both fields need widening.
|
|
32
|
+
const bracketed = node;
|
|
33
|
+
bracketed.property = t.stringLiteral(node.property.name);
|
|
34
|
+
bracketed.computed = true;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=dot-to-bracket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dot-to-bracket.js","sourceRoot":"","sources":["../../src/preparation/dot-to-bracket.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,iBAAiB,CAAA;AACtC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAA;AAGjC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAAC,GAAS;IACpC,QAAQ,CAAC,GAAG,EAAE,OAAyC,CAAC,CAAA;AAC1D,CAAC;AAED,MAAM,OAAO,GAAG;IACd,2CAA2C,CAAC,IAE3C;QACC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACpB,CAAC;CACO,CAAA;AAEV,SAAS,OAAO,CAAC,IAAqD;IACpE,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAClB,OAAM;IACR,CAAC;IACD,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnC,OAAM;IACR,CAAC;IACD,uEAAuE;IACvE,MAAM,SAAS,GAAG,IAAuD,CAAA;IACzE,SAAS,CAAC,QAAQ,GAAG,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;IACxD,SAAS,CAAC,QAAQ,GAAG,IAAI,CAAA;AAC3B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { File } from '@babel/types';
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites object destructuring declarations as individual property reads.
|
|
4
|
+
* Array patterns stay intact because indexed reads cannot reproduce the iterator
|
|
5
|
+
* protocol.
|
|
6
|
+
*
|
|
7
|
+
* The source, computed keys, and getters keep their original evaluation counts.
|
|
8
|
+
* Defaults compare against `void 0` because `undefined` can be shadowed.
|
|
9
|
+
*
|
|
10
|
+
* `with` and direct `eval` can expose synthesized names that Babel cannot see, so
|
|
11
|
+
* those scopes are skipped. A `RequireObjectCoercible` guard runs before any
|
|
12
|
+
* computed key, matching ObjectBindingPattern evaluation order.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // ◀️ before
|
|
16
|
+
* const { a, b: renamed, c = 3 } = obj;
|
|
17
|
+
*
|
|
18
|
+
* // ▶️ after
|
|
19
|
+
* const _ref = obj;
|
|
20
|
+
* const a = _ref.a;
|
|
21
|
+
* const renamed = _ref.b;
|
|
22
|
+
* const _c = _ref.c;
|
|
23
|
+
* const c = _c === void 0 ? 3 : _c;
|
|
24
|
+
*/
|
|
25
|
+
export declare function expandDestructuring(ast: File): void;
|
|
26
|
+
//# sourceMappingURL=expand-destructuring.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expand-destructuring.d.ts","sourceRoot":"","sources":["../../src/preparation/expand-destructuring.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AASxC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,IAAI,GAAG,IAAI,CAEnD"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import traverse from '@babel/traverse';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
import { enclosingScopeHasDirectEval, isInsideWith } from '../utils/ast.js';
|
|
4
|
+
/**
|
|
5
|
+
* Rewrites object destructuring declarations as individual property reads.
|
|
6
|
+
* Array patterns stay intact because indexed reads cannot reproduce the iterator
|
|
7
|
+
* protocol.
|
|
8
|
+
*
|
|
9
|
+
* The source, computed keys, and getters keep their original evaluation counts.
|
|
10
|
+
* Defaults compare against `void 0` because `undefined` can be shadowed.
|
|
11
|
+
*
|
|
12
|
+
* `with` and direct `eval` can expose synthesized names that Babel cannot see, so
|
|
13
|
+
* those scopes are skipped. A `RequireObjectCoercible` guard runs before any
|
|
14
|
+
* computed key, matching ObjectBindingPattern evaluation order.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // ◀️ before
|
|
18
|
+
* const { a, b: renamed, c = 3 } = obj;
|
|
19
|
+
*
|
|
20
|
+
* // ▶️ after
|
|
21
|
+
* const _ref = obj;
|
|
22
|
+
* const a = _ref.a;
|
|
23
|
+
* const renamed = _ref.b;
|
|
24
|
+
* const _c = _ref.c;
|
|
25
|
+
* const c = _c === void 0 ? 3 : _c;
|
|
26
|
+
*/
|
|
27
|
+
export function expandDestructuring(ast) {
|
|
28
|
+
traverse(ast, visitor);
|
|
29
|
+
}
|
|
30
|
+
const visitor = {
|
|
31
|
+
VariableDeclaration(path) {
|
|
32
|
+
if (typeof path.listKey !== 'string') {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Babel cannot prove a generated name hidden from `with` or direct `eval`.
|
|
36
|
+
if (isInsideWith(path) || enclosingScopeHasDirectEval(path)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const { kind } = path.node;
|
|
40
|
+
const replacement = [];
|
|
41
|
+
let changed = false;
|
|
42
|
+
for (const declarator of path.node.declarations) {
|
|
43
|
+
const { id, init } = declarator;
|
|
44
|
+
if (!init ||
|
|
45
|
+
!t.isObjectPattern(id) ||
|
|
46
|
+
containsRestElement(id) ||
|
|
47
|
+
containsArrayPattern(id) ||
|
|
48
|
+
containsEmptyObjectPattern(id)) {
|
|
49
|
+
replacement.push(t.variableDeclaration(kind, [declarator]));
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
let source = init;
|
|
53
|
+
// Snapshot a mutable identifier before reading more than one property.
|
|
54
|
+
if (t.isIdentifier(source) && topLevelReadCount(id) > 1) {
|
|
55
|
+
const temp = path.scope.generateUidIdentifier('ref');
|
|
56
|
+
replacement.push(t.variableDeclaration(kind, [t.variableDeclarator(temp, source)]));
|
|
57
|
+
source = t.cloneNode(temp);
|
|
58
|
+
}
|
|
59
|
+
replacement.push(...expandPattern(kind, id, source, path.scope));
|
|
60
|
+
changed = true;
|
|
61
|
+
}
|
|
62
|
+
if (changed) {
|
|
63
|
+
path.replaceWithMultiple(replacement);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
function expandPattern(kind, target, source, scope) {
|
|
68
|
+
if (t.isIdentifier(target)) {
|
|
69
|
+
return [t.variableDeclaration(kind, [t.variableDeclarator(target, source)])];
|
|
70
|
+
}
|
|
71
|
+
if (t.isAssignmentPattern(target)) {
|
|
72
|
+
const innerName = t.isIdentifier(target.left) && t.isValidIdentifier(target.left.name)
|
|
73
|
+
? scope.generateUid(target.left.name)
|
|
74
|
+
: scope.generateUid('default');
|
|
75
|
+
const innerId = t.identifier(innerName);
|
|
76
|
+
const decls = [
|
|
77
|
+
t.variableDeclaration(kind, [t.variableDeclarator(t.cloneNode(innerId), source)]),
|
|
78
|
+
];
|
|
79
|
+
const defaulted = t.conditionalExpression(
|
|
80
|
+
// `void 0`, not the shadowable identifier `undefined`.
|
|
81
|
+
t.binaryExpression('===', t.cloneNode(innerId), t.unaryExpression('void', t.numericLiteral(0))), target.right, t.cloneNode(innerId));
|
|
82
|
+
decls.push(...expandPattern(kind, target.left, defaulted, scope));
|
|
83
|
+
return decls;
|
|
84
|
+
}
|
|
85
|
+
if (t.isObjectPattern(target)) {
|
|
86
|
+
const decls = [];
|
|
87
|
+
// Snapshot before repeated or nested property evaluation.
|
|
88
|
+
let src = source;
|
|
89
|
+
if (!t.isIdentifier(src)) {
|
|
90
|
+
const temp = scope.generateUidIdentifier('ref');
|
|
91
|
+
decls.push(t.variableDeclaration(kind, [t.variableDeclarator(temp, src)]));
|
|
92
|
+
src = t.cloneNode(temp);
|
|
93
|
+
}
|
|
94
|
+
// `({} = src)` reproduces RequireObjectCoercible before computed keys run.
|
|
95
|
+
if (hasComputedKey(target)) {
|
|
96
|
+
decls.push(t.expressionStatement(t.assignmentExpression('=', t.objectPattern([]), t.cloneNode(src))));
|
|
97
|
+
}
|
|
98
|
+
for (const prop of target.properties) {
|
|
99
|
+
if (t.isRestElement(prop)) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const { key } = prop;
|
|
103
|
+
const useComputed = prop.computed || !t.isIdentifier(key);
|
|
104
|
+
const access = t.memberExpression(t.cloneNode(src), t.cloneNode(key), useComputed);
|
|
105
|
+
decls.push(...expandPattern(kind, prop.value, access, scope));
|
|
106
|
+
}
|
|
107
|
+
return decls;
|
|
108
|
+
}
|
|
109
|
+
return [t.variableDeclaration(kind, [t.variableDeclarator(target, source)])];
|
|
110
|
+
}
|
|
111
|
+
function topLevelReadCount(pattern) {
|
|
112
|
+
return pattern.properties.filter((prop) => !t.isRestElement(prop)).length;
|
|
113
|
+
}
|
|
114
|
+
function hasComputedKey(pattern) {
|
|
115
|
+
return pattern.properties.some((prop) => t.isObjectProperty(prop) && prop.computed);
|
|
116
|
+
}
|
|
117
|
+
// An empty pattern still throws on nullish input, with no property read to carry
|
|
118
|
+
// that check after expansion.
|
|
119
|
+
function containsEmptyObjectPattern(pattern) {
|
|
120
|
+
if (t.isObjectPattern(pattern)) {
|
|
121
|
+
return (pattern.properties.length === 0 ||
|
|
122
|
+
pattern.properties.some((prop) => t.isObjectProperty(prop) && containsEmptyObjectPattern(prop.value)));
|
|
123
|
+
}
|
|
124
|
+
if (t.isAssignmentPattern(pattern)) {
|
|
125
|
+
return containsEmptyObjectPattern(pattern.left);
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
function containsArrayPattern(pattern) {
|
|
130
|
+
if (t.isArrayPattern(pattern)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
if (t.isObjectPattern(pattern)) {
|
|
134
|
+
return pattern.properties.some((prop) => t.isObjectProperty(prop) && containsArrayPattern(prop.value));
|
|
135
|
+
}
|
|
136
|
+
if (t.isAssignmentPattern(pattern)) {
|
|
137
|
+
return containsArrayPattern(pattern.left);
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
function containsRestElement(pattern) {
|
|
142
|
+
if (t.isObjectPattern(pattern)) {
|
|
143
|
+
return pattern.properties.some((prop) => t.isRestElement(prop) ||
|
|
144
|
+
(t.isObjectProperty(prop) && containsRestElement(prop.value)));
|
|
145
|
+
}
|
|
146
|
+
if (t.isArrayPattern(pattern)) {
|
|
147
|
+
return pattern.elements.some((el) => el !== null && (t.isRestElement(el) || containsRestElement(el)));
|
|
148
|
+
}
|
|
149
|
+
if (t.isAssignmentPattern(pattern)) {
|
|
150
|
+
return containsRestElement(pattern.left);
|
|
151
|
+
}
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=expand-destructuring.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expand-destructuring.js","sourceRoot":"","sources":["../../src/preparation/expand-destructuring.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,iBAAiB,CAAA;AAEtC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAA;AAGjC,OAAO,EAAE,2BAA2B,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAOzE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,mBAAmB,CAA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AAa,CAAC,IAAI,CAAC;YACrB,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,mBAAmB,CAAC,IAAI,CAAC,KAAsB,CAAC,CAAC,CACjF,CAAA;IACH,CAAC;IACD,IAAI,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;QAC9B,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,CAC1B,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,IAAI,mBAAmB,CAAC,EAAmB,CAAC,CAAC,CACzF,CAAA;IACH,CAAC;IACD,IAAI,CAAC,CAAC,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC;QACnC,OAAO,mBAAmB,CAAC,OAAO,CAAC,IAAqB,CAAC,CAAA;IAC3D,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { File } from '@babel/types';
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites safe template literals as string concatenation. Tagged templates
|
|
4
|
+
* stay intact because the tag receives their quasi structure.
|
|
5
|
+
*
|
|
6
|
+
* Templates use the "string" ToPrimitive hint, while `x + ""` uses "default".
|
|
7
|
+
* Those hints can call object coercion methods in a different order, so only
|
|
8
|
+
* expressions known to produce primitives are expanded.
|
|
9
|
+
*
|
|
10
|
+
* An empty first quasi still matters. It makes `` `${5}` `` produce `"5"`
|
|
11
|
+
* instead of the number `5`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // ◀️ before
|
|
15
|
+
* var msg = `sum is ${a + b}`;
|
|
16
|
+
* var raw = `static text`;
|
|
17
|
+
*
|
|
18
|
+
* // ▶️ after
|
|
19
|
+
* var msg = "sum is " + (a + b);
|
|
20
|
+
* var raw = "static text";
|
|
21
|
+
*/
|
|
22
|
+
export declare function expandTemplateLiterals(ast: File): void;
|
|
23
|
+
//# sourceMappingURL=expand-template-literals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expand-template-literals.d.ts","sourceRoot":"","sources":["../../src/preparation/expand-template-literals.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAIxC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,IAAI,GAAG,IAAI,CAEtD"}
|