js-confuser 1.7.3 → 2.0.0-alpha.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/.github/ISSUE_TEMPLATE/bug_report.md +6 -4
- package/CHANGELOG.md +70 -0
- package/Migration.md +57 -0
- package/README.md +23 -929
- package/dist/constants.js +65 -14
- package/dist/index.js +108 -160
- package/dist/obfuscator.js +316 -118
- package/dist/options.js +1 -119
- package/dist/order.js +30 -30
- package/dist/presets.js +47 -45
- package/dist/probability.js +25 -32
- package/dist/templates/bufferToStringTemplate.js +9 -0
- package/dist/templates/deadCodeTemplates.js +9 -0
- package/dist/templates/getGlobalTemplate.js +19 -0
- package/dist/templates/integrityTemplate.js +30 -0
- package/dist/templates/setFunctionLengthTemplate.js +9 -0
- package/dist/templates/stringCompressionTemplate.js +10 -0
- package/dist/templates/tamperProtectionTemplates.js +21 -0
- package/dist/templates/template.js +199 -184
- package/dist/transforms/astScrambler.js +100 -0
- package/dist/transforms/calculator.js +70 -127
- package/dist/transforms/controlFlowFlattening.js +1182 -0
- package/dist/transforms/deadCode.js +62 -587
- package/dist/transforms/dispatcher.js +300 -313
- package/dist/transforms/extraction/duplicateLiteralsRemoval.js +88 -189
- package/dist/transforms/extraction/objectExtraction.js +131 -215
- package/dist/transforms/finalizer.js +56 -59
- package/dist/transforms/flatten.js +275 -276
- package/dist/transforms/functionOutlining.js +230 -0
- package/dist/transforms/identifier/globalConcealing.js +214 -135
- package/dist/transforms/identifier/movedDeclarations.js +167 -91
- package/dist/transforms/identifier/renameVariables.js +239 -193
- package/dist/transforms/lock/integrity.js +61 -184
- package/dist/transforms/lock/lock.js +261 -387
- package/dist/transforms/minify.js +431 -436
- package/dist/transforms/opaquePredicates.js +65 -118
- package/dist/transforms/pack.js +160 -0
- package/dist/transforms/plugin.js +179 -0
- package/dist/transforms/preparation.js +261 -173
- package/dist/transforms/renameLabels.js +132 -56
- package/dist/transforms/rgf.js +140 -267
- package/dist/transforms/shuffle.js +52 -145
- package/dist/transforms/string/encoding.js +44 -175
- package/dist/transforms/string/stringCompression.js +79 -155
- package/dist/transforms/string/stringConcealing.js +189 -225
- package/dist/transforms/string/stringEncoding.js +32 -40
- package/dist/transforms/string/stringSplitting.js +54 -55
- package/dist/transforms/variableMasking.js +232 -0
- package/dist/utils/ControlObject.js +125 -0
- package/dist/utils/IntGen.js +46 -0
- package/dist/utils/NameGen.js +106 -0
- package/dist/utils/ast-utils.js +560 -0
- package/dist/utils/function-utils.js +56 -0
- package/dist/utils/gen-utils.js +48 -0
- package/dist/utils/node.js +77 -0
- package/dist/utils/object-utils.js +21 -0
- package/dist/utils/random-utils.js +91 -0
- package/dist/utils/static-utils.js +64 -0
- package/dist/validateOptions.js +122 -0
- package/index.d.ts +1 -17
- package/package.json +27 -22
- package/src/constants.ts +139 -82
- package/src/index.ts +70 -165
- package/src/obfuscationResult.ts +43 -0
- package/src/obfuscator.ts +328 -135
- package/src/options.ts +149 -658
- package/src/order.ts +14 -14
- package/src/presets.ts +39 -34
- package/src/probability.ts +21 -36
- package/src/templates/bufferToStringTemplate.ts +57 -0
- package/src/templates/deadCodeTemplates.ts +1185 -0
- package/src/templates/getGlobalTemplate.ts +72 -0
- package/src/templates/integrityTemplate.ts +69 -0
- package/src/templates/setFunctionLengthTemplate.ts +11 -0
- package/src/templates/stringCompressionTemplate.ts +42 -0
- package/src/templates/tamperProtectionTemplates.ts +116 -0
- package/src/templates/template.ts +149 -157
- package/src/transforms/astScrambler.ts +99 -0
- package/src/transforms/calculator.ts +96 -226
- package/src/transforms/controlFlowFlattening.ts +1594 -0
- package/src/transforms/deadCode.ts +85 -676
- package/src/transforms/dispatcher.ts +431 -640
- package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -295
- package/src/transforms/extraction/objectExtraction.ts +160 -333
- package/src/transforms/finalizer.ts +63 -64
- package/src/transforms/flatten.ts +439 -557
- package/src/transforms/functionOutlining.ts +225 -0
- package/src/transforms/identifier/globalConcealing.ts +255 -266
- package/src/transforms/identifier/movedDeclarations.ts +228 -142
- package/src/transforms/identifier/renameVariables.ts +250 -271
- package/src/transforms/lock/integrity.ts +85 -263
- package/src/transforms/lock/lock.ts +338 -579
- package/src/transforms/minify.ts +523 -663
- package/src/transforms/opaquePredicates.ts +90 -229
- package/src/transforms/pack.ts +195 -0
- package/src/transforms/plugin.ts +185 -0
- package/src/transforms/preparation.ts +337 -231
- package/src/transforms/renameLabels.ts +176 -77
- package/src/transforms/rgf.ts +293 -424
- package/src/transforms/shuffle.ts +80 -254
- package/src/transforms/string/encoding.ts +20 -126
- package/src/transforms/string/stringCompression.ts +117 -307
- package/src/transforms/string/stringConcealing.ts +254 -342
- package/src/transforms/string/stringEncoding.ts +28 -47
- package/src/transforms/string/stringSplitting.ts +61 -75
- package/src/transforms/variableMasking.ts +257 -0
- package/src/utils/ControlObject.ts +141 -0
- package/src/utils/IntGen.ts +33 -0
- package/src/utils/NameGen.ts +106 -0
- package/src/utils/ast-utils.ts +667 -0
- package/src/utils/function-utils.ts +50 -0
- package/src/utils/gen-utils.ts +48 -0
- package/src/utils/node.ts +78 -0
- package/src/utils/object-utils.ts +21 -0
- package/src/utils/random-utils.ts +79 -0
- package/src/utils/static-utils.ts +66 -0
- package/src/validateOptions.ts +256 -0
- package/tsconfig.json +13 -8
- package/babel.config.js +0 -12
- package/dev.js +0 -8
- package/dist/compiler.js +0 -34
- package/dist/parser.js +0 -59
- package/dist/precedence.js +0 -66
- package/dist/templates/bufferToString.js +0 -129
- package/dist/templates/core.js +0 -35
- package/dist/templates/crash.js +0 -28
- package/dist/templates/es5.js +0 -137
- package/dist/templates/functionLength.js +0 -34
- package/dist/templates/globals.js +0 -9
- package/dist/transforms/antiTooling.js +0 -88
- package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +0 -1287
- package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +0 -131
- package/dist/transforms/es5/antiClass.js +0 -164
- package/dist/transforms/es5/antiDestructuring.js +0 -193
- package/dist/transforms/es5/antiES6Object.js +0 -185
- package/dist/transforms/es5/antiSpreadOperator.js +0 -35
- package/dist/transforms/es5/antiTemplate.js +0 -66
- package/dist/transforms/es5/es5.js +0 -123
- package/dist/transforms/extraction/classExtraction.js +0 -83
- package/dist/transforms/identifier/globalAnalysis.js +0 -83
- package/dist/transforms/identifier/variableAnalysis.js +0 -104
- package/dist/transforms/lock/antiDebug.js +0 -76
- package/dist/transforms/stack.js +0 -349
- package/dist/transforms/transform.js +0 -372
- package/dist/traverse.js +0 -110
- package/dist/util/compare.js +0 -145
- package/dist/util/gen.js +0 -564
- package/dist/util/guard.js +0 -14
- package/dist/util/identifiers.js +0 -355
- package/dist/util/insert.js +0 -362
- package/dist/util/math.js +0 -19
- package/dist/util/object.js +0 -40
- package/dist/util/random.js +0 -156
- package/dist/util/scope.js +0 -20
- package/docs/ControlFlowFlattening.md +0 -595
- package/docs/Countermeasures.md +0 -70
- package/docs/ES5.md +0 -197
- package/docs/Integrity.md +0 -82
- package/docs/RGF.md +0 -424
- package/docs/RenameVariables.md +0 -116
- package/docs/TamperProtection.md +0 -100
- package/docs/Template.md +0 -117
- package/samples/example.js +0 -15
- package/samples/high.js +0 -1
- package/samples/input.js +0 -3
- package/samples/javascriptobfuscator.com.js +0 -8
- package/samples/jscrambler_advanced.js +0 -1894
- package/samples/jscrambler_light.js +0 -1134
- package/samples/low.js +0 -1
- package/samples/medium.js +0 -1
- package/samples/obfuscator.io.js +0 -1686
- package/samples/preemptive.com.js +0 -16
- package/src/compiler.ts +0 -35
- package/src/parser.ts +0 -49
- package/src/precedence.ts +0 -61
- package/src/templates/bufferToString.ts +0 -136
- package/src/templates/core.ts +0 -29
- package/src/templates/crash.ts +0 -23
- package/src/templates/es5.ts +0 -131
- package/src/templates/functionLength.ts +0 -32
- package/src/templates/globals.ts +0 -3
- package/src/transforms/antiTooling.ts +0 -102
- package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +0 -2153
- package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
- package/src/transforms/es5/antiClass.ts +0 -276
- package/src/transforms/es5/antiDestructuring.ts +0 -294
- package/src/transforms/es5/antiES6Object.ts +0 -267
- package/src/transforms/es5/antiSpreadOperator.ts +0 -56
- package/src/transforms/es5/antiTemplate.ts +0 -98
- package/src/transforms/es5/es5.ts +0 -149
- package/src/transforms/extraction/classExtraction.ts +0 -168
- package/src/transforms/identifier/globalAnalysis.ts +0 -102
- package/src/transforms/identifier/variableAnalysis.ts +0 -118
- package/src/transforms/lock/antiDebug.ts +0 -112
- package/src/transforms/stack.ts +0 -557
- package/src/transforms/transform.ts +0 -441
- package/src/traverse.ts +0 -120
- package/src/types.ts +0 -133
- package/src/util/compare.ts +0 -181
- package/src/util/gen.ts +0 -651
- package/src/util/guard.ts +0 -17
- package/src/util/identifiers.ts +0 -494
- package/src/util/insert.ts +0 -419
- package/src/util/math.ts +0 -15
- package/src/util/object.ts +0 -39
- package/src/util/random.ts +0 -221
- package/src/util/scope.ts +0 -21
- package/test/code/Cash.src.js +0 -1011
- package/test/code/Cash.test.ts +0 -132
- package/test/code/Dynamic.src.js +0 -118
- package/test/code/Dynamic.test.ts +0 -49
- package/test/code/ES6.src.js +0 -235
- package/test/code/ES6.test.ts +0 -42
- package/test/code/NewFeatures.test.ts +0 -19
- package/test/code/StrictMode.src.js +0 -65
- package/test/code/StrictMode.test.js +0 -37
- package/test/compare.test.ts +0 -104
- package/test/index.test.ts +0 -249
- package/test/options.test.ts +0 -150
- package/test/presets.test.ts +0 -22
- package/test/probability.test.ts +0 -44
- package/test/templates/template.test.ts +0 -224
- package/test/transforms/antiTooling.test.ts +0 -52
- package/test/transforms/calculator.test.ts +0 -78
- package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +0 -1274
- package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +0 -192
- package/test/transforms/deadCode.test.ts +0 -85
- package/test/transforms/dispatcher.test.ts +0 -457
- package/test/transforms/es5/antiClass.test.ts +0 -427
- package/test/transforms/es5/antiDestructuring.test.ts +0 -157
- package/test/transforms/es5/antiES6Object.test.ts +0 -245
- package/test/transforms/es5/antiTemplate.test.ts +0 -116
- package/test/transforms/es5/es5.test.ts +0 -110
- package/test/transforms/extraction/classExtraction.test.ts +0 -86
- package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +0 -200
- package/test/transforms/extraction/objectExtraction.test.ts +0 -491
- package/test/transforms/flatten.test.ts +0 -721
- package/test/transforms/hexadecimalNumbers.test.ts +0 -62
- package/test/transforms/identifier/globalConcealing.test.ts +0 -142
- package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
- package/test/transforms/identifier/renameVariables.test.ts +0 -695
- package/test/transforms/lock/antiDebug.test.ts +0 -66
- package/test/transforms/lock/browserLock.test.ts +0 -129
- package/test/transforms/lock/countermeasures.test.ts +0 -100
- package/test/transforms/lock/integrity.test.ts +0 -161
- package/test/transforms/lock/lock.test.ts +0 -204
- package/test/transforms/lock/osLock.test.ts +0 -312
- package/test/transforms/lock/selfDefending.test.ts +0 -68
- package/test/transforms/lock/tamperProtection.test.ts +0 -336
- package/test/transforms/minify.test.ts +0 -575
- package/test/transforms/opaquePredicates.test.ts +0 -43
- package/test/transforms/preparation.test.ts +0 -157
- package/test/transforms/renameLabels.test.ts +0 -95
- package/test/transforms/rgf.test.ts +0 -378
- package/test/transforms/shuffle.test.ts +0 -135
- package/test/transforms/stack.test.ts +0 -573
- package/test/transforms/string/stringCompression.test.ts +0 -120
- package/test/transforms/string/stringConcealing.test.ts +0 -299
- package/test/transforms/string/stringEncoding.test.ts +0 -95
- package/test/transforms/string/stringSplitting.test.ts +0 -135
- package/test/transforms/transform.test.ts +0 -66
- package/test/traverse.test.ts +0 -139
- package/test/util/compare.test.ts +0 -34
- package/test/util/gen.test.ts +0 -121
- package/test/util/identifiers.test.ts +0 -253
- package/test/util/insert.test.ts +0 -142
- package/test/util/math.test.ts +0 -5
- package/test/util/random.test.ts +0 -71
- /package/dist/{types.js → obfuscationResult.js} +0 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.append = append;
|
|
8
|
+
exports.ensureComputedExpression = ensureComputedExpression;
|
|
9
|
+
exports.getBlock = getBlock;
|
|
10
|
+
exports.getFunctionName = getFunctionName;
|
|
11
|
+
exports.getMemberExpressionPropertyAsString = getMemberExpressionPropertyAsString;
|
|
12
|
+
exports.getObjectPropertyAsString = getObjectPropertyAsString;
|
|
13
|
+
exports.getParentFunctionOrProgram = getParentFunctionOrProgram;
|
|
14
|
+
exports.getPatternIdentifierNames = getPatternIdentifierNames;
|
|
15
|
+
exports.isDefiningIdentifier = isDefiningIdentifier;
|
|
16
|
+
exports.isExportedIdentifier = isExportedIdentifier;
|
|
17
|
+
exports.isModifiedIdentifier = isModifiedIdentifier;
|
|
18
|
+
exports.isModuleImport = isModuleImport;
|
|
19
|
+
exports.isStrictIdentifier = isStrictIdentifier;
|
|
20
|
+
exports.isStrictMode = isStrictMode;
|
|
21
|
+
exports.isUndefined = isUndefined;
|
|
22
|
+
exports.isVariableIdentifier = isVariableIdentifier;
|
|
23
|
+
exports.prepend = prepend;
|
|
24
|
+
exports.prependProgram = prependProgram;
|
|
25
|
+
exports.replaceDefiningIdentifierToMemberExpression = replaceDefiningIdentifierToMemberExpression;
|
|
26
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
|
27
|
+
var _assert = require("assert");
|
|
28
|
+
var _node = require("./node");
|
|
29
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
30
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
31
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
32
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
33
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
34
|
+
function getPatternIdentifierNames(path) {
|
|
35
|
+
if (Array.isArray(path)) {
|
|
36
|
+
var allNames = new Set();
|
|
37
|
+
var _iterator = _createForOfIteratorHelper(path),
|
|
38
|
+
_step;
|
|
39
|
+
try {
|
|
40
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
41
|
+
var p = _step.value;
|
|
42
|
+
var names = getPatternIdentifierNames(p);
|
|
43
|
+
var _iterator2 = _createForOfIteratorHelper(names),
|
|
44
|
+
_step2;
|
|
45
|
+
try {
|
|
46
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
47
|
+
var name = _step2.value;
|
|
48
|
+
allNames.add(name);
|
|
49
|
+
}
|
|
50
|
+
} catch (err) {
|
|
51
|
+
_iterator2.e(err);
|
|
52
|
+
} finally {
|
|
53
|
+
_iterator2.f();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} catch (err) {
|
|
57
|
+
_iterator.e(err);
|
|
58
|
+
} finally {
|
|
59
|
+
_iterator.f();
|
|
60
|
+
}
|
|
61
|
+
return allNames;
|
|
62
|
+
}
|
|
63
|
+
var names = new Set();
|
|
64
|
+
var functionParent = path.find(function (parent) {
|
|
65
|
+
return parent.isFunction();
|
|
66
|
+
});
|
|
67
|
+
path.traverse({
|
|
68
|
+
BindingIdentifier: function BindingIdentifier(bindingPath) {
|
|
69
|
+
var bindingFunctionParent = bindingPath.find(function (parent) {
|
|
70
|
+
return parent.isFunction();
|
|
71
|
+
});
|
|
72
|
+
if (functionParent === bindingFunctionParent) {
|
|
73
|
+
names.add(bindingPath.node.name);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Check if the path itself is a binding identifier
|
|
79
|
+
if (path.isBindingIdentifier()) {
|
|
80
|
+
names.add(path.node.name);
|
|
81
|
+
}
|
|
82
|
+
return names;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Ensures a `String Literal` is 'computed' before replacing it with a more complex expression.
|
|
87
|
+
*
|
|
88
|
+
* ```js
|
|
89
|
+
* // Input
|
|
90
|
+
* {
|
|
91
|
+
* "myToBeEncodedString": "value"
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* // Output
|
|
95
|
+
* {
|
|
96
|
+
* ["myToBeEncodedString"]: "value"
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
* @param path
|
|
100
|
+
*/
|
|
101
|
+
function ensureComputedExpression(path) {
|
|
102
|
+
if ((t.isObjectMember(path.parent) || t.isClassMethod(path.parent) || t.isClassProperty(path.parent)) && path.parent.key === path.node && !path.parent.computed) {
|
|
103
|
+
path.parent.computed = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves a function name from debugging purposes.
|
|
109
|
+
* - Function Declaration / Expression
|
|
110
|
+
* - Variable Declaration
|
|
111
|
+
* - Object property / method
|
|
112
|
+
* - Class property / method
|
|
113
|
+
* - Program returns "[Program]"
|
|
114
|
+
* - Default returns "anonymous"
|
|
115
|
+
* @param path
|
|
116
|
+
* @returns
|
|
117
|
+
*/
|
|
118
|
+
function getFunctionName(path) {
|
|
119
|
+
var _path$parentPath, _path$parentPath2, _path$parentPath3;
|
|
120
|
+
if (!path) return "null";
|
|
121
|
+
if (path.isProgram()) return "[Program]";
|
|
122
|
+
|
|
123
|
+
// Check function declaration/expression ID
|
|
124
|
+
if ((t.isFunctionDeclaration(path.node) || t.isFunctionExpression(path.node)) && path.node.id) {
|
|
125
|
+
return path.node.id.name;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Check for containing variable declaration
|
|
129
|
+
if ((_path$parentPath = path.parentPath) !== null && _path$parentPath !== void 0 && _path$parentPath.isVariableDeclarator() && t.isIdentifier(path.parentPath.node.id)) {
|
|
130
|
+
return path.parentPath.node.id.name;
|
|
131
|
+
}
|
|
132
|
+
if (path.isObjectMethod() || path.isClassMethod()) {
|
|
133
|
+
var property = getObjectPropertyAsString(path.node);
|
|
134
|
+
if (property) return property;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Check for containing property in an object
|
|
138
|
+
if ((_path$parentPath2 = path.parentPath) !== null && _path$parentPath2 !== void 0 && _path$parentPath2.isObjectProperty() || (_path$parentPath3 = path.parentPath) !== null && _path$parentPath3 !== void 0 && _path$parentPath3.isClassProperty()) {
|
|
139
|
+
var property = getObjectPropertyAsString(path.parentPath.node);
|
|
140
|
+
if (property) return property;
|
|
141
|
+
}
|
|
142
|
+
var output = "anonymous";
|
|
143
|
+
if (path.isFunction()) {
|
|
144
|
+
if (path.node.generator) {
|
|
145
|
+
output += "*";
|
|
146
|
+
} else if (path.node.async) {
|
|
147
|
+
output = "async " + output;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return output;
|
|
151
|
+
}
|
|
152
|
+
function isModuleImport(path) {
|
|
153
|
+
// Import Declaration
|
|
154
|
+
if (path.parentPath.isImportDeclaration()) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Dynamic Import / require() call
|
|
159
|
+
if (t.isCallExpression(path.parent) && (t.isIdentifier(path.parent.callee, {
|
|
160
|
+
name: "require"
|
|
161
|
+
}) || t.isImport(path.parent.callee)) && path.node === path.parent.arguments[0]) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
function getBlock(path) {
|
|
167
|
+
return path.find(function (p) {
|
|
168
|
+
return p.isBlock();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function getParentFunctionOrProgram(path) {
|
|
172
|
+
if (path.isProgram()) return path;
|
|
173
|
+
|
|
174
|
+
// Find the nearest function-like parent
|
|
175
|
+
var functionOrProgramPath = path.findParent(function (parentPath) {
|
|
176
|
+
return parentPath.isFunction() || parentPath.isProgram();
|
|
177
|
+
});
|
|
178
|
+
(0, _assert.ok)(functionOrProgramPath);
|
|
179
|
+
return functionOrProgramPath;
|
|
180
|
+
}
|
|
181
|
+
function getObjectPropertyAsString(property) {
|
|
182
|
+
(0, _assert.ok)(t.isObjectMember(property) || t.isClassProperty(property) || t.isClassMethod(property));
|
|
183
|
+
if (!property.computed && t.isIdentifier(property.key)) {
|
|
184
|
+
return property.key.name;
|
|
185
|
+
}
|
|
186
|
+
if (t.isStringLiteral(property.key)) {
|
|
187
|
+
return property.key.value;
|
|
188
|
+
}
|
|
189
|
+
if (t.isNumericLiteral(property.key)) {
|
|
190
|
+
return property.key.value.toString();
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Gets the property of a MemberExpression as a string.
|
|
197
|
+
*
|
|
198
|
+
* @param memberPath - The path of the MemberExpression node.
|
|
199
|
+
* @returns The property as a string or null if it cannot be determined.
|
|
200
|
+
*/
|
|
201
|
+
function getMemberExpressionPropertyAsString(member) {
|
|
202
|
+
t.assertMemberExpression(member);
|
|
203
|
+
var property = member.property;
|
|
204
|
+
if (!member.computed && t.isIdentifier(property)) {
|
|
205
|
+
return property.name;
|
|
206
|
+
}
|
|
207
|
+
if (t.isStringLiteral(property)) {
|
|
208
|
+
return property.value;
|
|
209
|
+
}
|
|
210
|
+
if (t.isNumericLiteral(property)) {
|
|
211
|
+
return property.value.toString();
|
|
212
|
+
}
|
|
213
|
+
return null; // If the property cannot be determined
|
|
214
|
+
}
|
|
215
|
+
function registerPaths(paths) {
|
|
216
|
+
var _iterator3 = _createForOfIteratorHelper(paths),
|
|
217
|
+
_step3;
|
|
218
|
+
try {
|
|
219
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
220
|
+
var path = _step3.value;
|
|
221
|
+
if (path.isVariableDeclaration() && path.node.kind === "var") {
|
|
222
|
+
getParentFunctionOrProgram(path).scope.registerDeclaration(path);
|
|
223
|
+
}
|
|
224
|
+
path.scope.registerDeclaration(path);
|
|
225
|
+
}
|
|
226
|
+
} catch (err) {
|
|
227
|
+
_iterator3.e(err);
|
|
228
|
+
} finally {
|
|
229
|
+
_iterator3.f();
|
|
230
|
+
}
|
|
231
|
+
return paths;
|
|
232
|
+
}
|
|
233
|
+
function nodeListToNodes(nodesIn) {
|
|
234
|
+
var nodes = [];
|
|
235
|
+
if (Array.isArray(nodesIn[0])) {
|
|
236
|
+
(0, _assert.ok)(nodesIn.length === 1);
|
|
237
|
+
nodes = nodesIn[0];
|
|
238
|
+
} else {
|
|
239
|
+
nodes = nodesIn;
|
|
240
|
+
}
|
|
241
|
+
return nodes;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Appends to the bottom of a block. Preserving last expression for the top level.
|
|
246
|
+
*/
|
|
247
|
+
function append(path) {
|
|
248
|
+
for (var _len = arguments.length, nodesIn = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
249
|
+
nodesIn[_key - 1] = arguments[_key];
|
|
250
|
+
}
|
|
251
|
+
var nodes = nodeListToNodes(nodesIn);
|
|
252
|
+
var listParent = path.find(function (p) {
|
|
253
|
+
return p.isFunction() || p.isBlock() || p.isSwitchCase();
|
|
254
|
+
});
|
|
255
|
+
if (!listParent) {
|
|
256
|
+
throw new Error("Could not find a suitable parent to prepend to");
|
|
257
|
+
}
|
|
258
|
+
if (listParent.isProgram()) {
|
|
259
|
+
var lastExpression = listParent.get("body").at(-1);
|
|
260
|
+
if (lastExpression.isExpressionStatement()) {
|
|
261
|
+
return registerPaths(lastExpression.insertBefore(nodes));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (listParent.isSwitchCase()) {
|
|
265
|
+
return registerPaths(listParent.pushContainer("consequent", nodes));
|
|
266
|
+
}
|
|
267
|
+
if (listParent.isFunction()) {
|
|
268
|
+
var body = listParent.get("body");
|
|
269
|
+
if (listParent.isArrowFunctionExpression() && listParent.node.expression) {
|
|
270
|
+
if (!body.isBlockStatement()) {
|
|
271
|
+
body.replaceWith(t.blockStatement([t.returnStatement(body.node)]));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
(0, _assert.ok)(body.isBlockStatement());
|
|
275
|
+
return registerPaths(body.pushContainer("body", nodes));
|
|
276
|
+
}
|
|
277
|
+
(0, _assert.ok)(listParent.isBlock());
|
|
278
|
+
return registerPaths(listParent.pushContainer("body", nodes));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Prepends and registers a list of nodes to the beginning of a block.
|
|
283
|
+
*
|
|
284
|
+
* - Preserves import declarations by inserting after the last import declaration.
|
|
285
|
+
* - Handles arrow functions
|
|
286
|
+
* - Handles switch cases
|
|
287
|
+
* @param path
|
|
288
|
+
* @param nodes
|
|
289
|
+
* @returns
|
|
290
|
+
*/
|
|
291
|
+
function prepend(path) {
|
|
292
|
+
for (var _len2 = arguments.length, nodesIn = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
293
|
+
nodesIn[_key2 - 1] = arguments[_key2];
|
|
294
|
+
}
|
|
295
|
+
var nodes = nodeListToNodes(nodesIn);
|
|
296
|
+
var listParent = path.find(function (p) {
|
|
297
|
+
return p.isFunction() || p.isBlock() || p.isSwitchCase();
|
|
298
|
+
});
|
|
299
|
+
if (!listParent) {
|
|
300
|
+
throw new Error("Could not find a suitable parent to prepend to");
|
|
301
|
+
}
|
|
302
|
+
if (listParent.isProgram()) {
|
|
303
|
+
// Preserve import declarations
|
|
304
|
+
// Filter out import declarations
|
|
305
|
+
var _body = listParent.get("body");
|
|
306
|
+
var lastImportIndex = _body.findIndex(function (path) {
|
|
307
|
+
return !path.isImportDeclaration();
|
|
308
|
+
});
|
|
309
|
+
if (lastImportIndex === 0 || lastImportIndex === -1) {
|
|
310
|
+
// No non-import declarations, so we can safely unshift everything
|
|
311
|
+
return registerPaths(listParent.unshiftContainer("body", nodes));
|
|
312
|
+
} else {
|
|
313
|
+
// Insert the nodes after the last import declaration
|
|
314
|
+
return registerPaths(_body[lastImportIndex - 1].insertAfter(nodes));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (listParent.isFunction()) {
|
|
318
|
+
var body = listParent.get("body");
|
|
319
|
+
if (listParent.isArrowFunctionExpression() && listParent.node.expression) {
|
|
320
|
+
if (!body.isBlockStatement()) {
|
|
321
|
+
body = body.replaceWith(t.blockStatement([t.returnStatement(body.node)]))[0];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
(0, _assert.ok)(body.isBlockStatement());
|
|
325
|
+
return registerPaths(body.unshiftContainer("body", nodes));
|
|
326
|
+
}
|
|
327
|
+
if (listParent.isBlock()) {
|
|
328
|
+
return registerPaths(listParent.unshiftContainer("body", nodes));
|
|
329
|
+
} else if (listParent.isSwitchCase()) {
|
|
330
|
+
return registerPaths(listParent.unshiftContainer("consequent", nodes));
|
|
331
|
+
}
|
|
332
|
+
(0, _assert.ok)(false);
|
|
333
|
+
}
|
|
334
|
+
function prependProgram(path) {
|
|
335
|
+
var program = path.find(function (p) {
|
|
336
|
+
return p.isProgram();
|
|
337
|
+
});
|
|
338
|
+
(0, _assert.ok)(program);
|
|
339
|
+
for (var _len3 = arguments.length, nodes = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
340
|
+
nodes[_key3 - 1] = arguments[_key3];
|
|
341
|
+
}
|
|
342
|
+
return prepend.apply(void 0, [program].concat(nodes));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* A referenced or binding identifier, only names that reflect variables.
|
|
347
|
+
*
|
|
348
|
+
* - Excludes labels
|
|
349
|
+
*
|
|
350
|
+
* @param path
|
|
351
|
+
* @returns
|
|
352
|
+
*/
|
|
353
|
+
function isVariableIdentifier(path) {
|
|
354
|
+
var _path$parentPath4;
|
|
355
|
+
if (!path.isReferencedIdentifier() && !path.isBindingIdentifier()) return false;
|
|
356
|
+
|
|
357
|
+
// abc: {} // not a variable identifier
|
|
358
|
+
if (path.key === "label" && (_path$parentPath4 = path.parentPath) !== null && _path$parentPath4 !== void 0 && _path$parentPath4.isLabeledStatement()) return false;
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Subset of BindingIdentifier, excluding non-defined assignment expressions.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* var a = 1; // true
|
|
367
|
+
* var {c} = {} // true
|
|
368
|
+
* function b() {} // true
|
|
369
|
+
* function d([e] = [], ...f) {} // true
|
|
370
|
+
*
|
|
371
|
+
* f = 0; // false
|
|
372
|
+
* f(); // false
|
|
373
|
+
* @param path
|
|
374
|
+
* @returns
|
|
375
|
+
*/
|
|
376
|
+
function isDefiningIdentifier(path) {
|
|
377
|
+
if (path.key === "id" && path.parentPath.isFunction()) return true;
|
|
378
|
+
if (path.key === "id" && path.parentPath.isClassDeclaration) return true;
|
|
379
|
+
if (path.key === "local" && (path.parentPath.isImportSpecifier() || path.parentPath.isImportDefaultSpecifier() || path.parentPath.isImportNamespaceSpecifier())) return true;
|
|
380
|
+
var maxTraversalPath = path.find(function (p) {
|
|
381
|
+
var _p$parentPath, _p$parentPath2, _p$parentPath3;
|
|
382
|
+
return p.key === "id" && ((_p$parentPath = p.parentPath) === null || _p$parentPath === void 0 ? void 0 : _p$parentPath.isVariableDeclarator()) || p.listKey === "params" && ((_p$parentPath2 = p.parentPath) === null || _p$parentPath2 === void 0 ? void 0 : _p$parentPath2.isFunction()) || p.key === "param" && ((_p$parentPath3 = p.parentPath) === null || _p$parentPath3 === void 0 ? void 0 : _p$parentPath3.isCatchClause());
|
|
383
|
+
});
|
|
384
|
+
if (!maxTraversalPath) return false;
|
|
385
|
+
var cursor = path;
|
|
386
|
+
while (cursor && cursor !== maxTraversalPath) {
|
|
387
|
+
var _cursor$parentPath$pa;
|
|
388
|
+
if (cursor.parentPath.isObjectProperty() && (_cursor$parentPath$pa = cursor.parentPath.parentPath) !== null && _cursor$parentPath$pa !== void 0 && _cursor$parentPath$pa.isObjectPattern()) {
|
|
389
|
+
if (cursor.key !== "value") {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
} else if (cursor.parentPath.isArrayPattern()) {
|
|
393
|
+
if (cursor.listKey !== "elements") {
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
} else if (cursor.parentPath.isRestElement()) {
|
|
397
|
+
if (cursor.key !== "argument") {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
} else if (cursor.parentPath.isAssignmentPattern()) {
|
|
401
|
+
if (cursor.key !== "left") {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
} else if (cursor.parentPath.isObjectPattern()) {} else return false;
|
|
405
|
+
cursor = cursor.parentPath;
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @example
|
|
412
|
+
* function id() {} // true
|
|
413
|
+
* class id {} // true
|
|
414
|
+
* var id; // false
|
|
415
|
+
* @param path
|
|
416
|
+
* @returns
|
|
417
|
+
*/
|
|
418
|
+
function isStrictIdentifier(path) {
|
|
419
|
+
if (path.key === "id" && (path.parentPath.isFunction() || path.parentPath.isClass())) return true;
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
function isExportedIdentifier(path) {
|
|
423
|
+
// Check if the identifier is directly inside an ExportNamedDeclaration
|
|
424
|
+
if (path.parentPath.isExportNamedDeclaration()) {
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Check if the identifier is in an ExportDefaultDeclaration
|
|
429
|
+
if (path.parentPath.isExportDefaultDeclaration()) {
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Check if the identifier is within an ExportSpecifier
|
|
434
|
+
if (path.parentPath.isExportSpecifier() && path.parentPath.parentPath.isExportNamedDeclaration()) {
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Check if it's part of an exported variable declaration (e.g., export const a = 1;)
|
|
439
|
+
if (path.parentPath.isVariableDeclarator() && path.parentPath.parentPath.parentPath.isExportNamedDeclaration()) {
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Check if it's part of an exported function declaration (e.g., export function abc() {})
|
|
444
|
+
if ((path.parentPath.isFunctionDeclaration() || path.parentPath.isClassDeclaration()) && path.parentPath.parentPath.isExportNamedDeclaration()) {
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* @example
|
|
452
|
+
* function abc() {
|
|
453
|
+
* "use strict";
|
|
454
|
+
* } // true
|
|
455
|
+
* @param path
|
|
456
|
+
* @returns
|
|
457
|
+
*/
|
|
458
|
+
function isStrictMode(path) {
|
|
459
|
+
// Classes are always in strict mode
|
|
460
|
+
if (path.isClass()) return true;
|
|
461
|
+
if (path.isBlock()) {
|
|
462
|
+
if (path.isTSModuleBlock()) return false;
|
|
463
|
+
return path.node.directives.some(function (directive) {
|
|
464
|
+
return directive.value.value === "use strict";
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
if (path.isFunction()) {
|
|
468
|
+
var fnBody = path.get("body");
|
|
469
|
+
if (fnBody.isBlock()) {
|
|
470
|
+
return isStrictMode(fnBody);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* A modified identifier is an identifier that is assigned to or updated.
|
|
478
|
+
*
|
|
479
|
+
* - Assignment Expression
|
|
480
|
+
* - Update Expression
|
|
481
|
+
*
|
|
482
|
+
* @param identifierPath
|
|
483
|
+
*/
|
|
484
|
+
function isModifiedIdentifier(identifierPath) {
|
|
485
|
+
var isModification = false;
|
|
486
|
+
if (identifierPath.parentPath.isUpdateExpression()) {
|
|
487
|
+
isModification = true;
|
|
488
|
+
}
|
|
489
|
+
if (identifierPath.find(function (p) {
|
|
490
|
+
var _p$parentPath4;
|
|
491
|
+
return p.key === "left" && ((_p$parentPath4 = p.parentPath) === null || _p$parentPath4 === void 0 ? void 0 : _p$parentPath4.isAssignmentExpression());
|
|
492
|
+
})) {
|
|
493
|
+
isModification = true;
|
|
494
|
+
}
|
|
495
|
+
return isModification;
|
|
496
|
+
}
|
|
497
|
+
function replaceDefiningIdentifierToMemberExpression(path, memberExpression) {
|
|
498
|
+
// function id(){} -> var id = function() {}
|
|
499
|
+
if (path.key === "id" && path.parentPath.isFunctionDeclaration()) {
|
|
500
|
+
var asFunctionExpression = (0, _node.deepClone)(path.parentPath.node);
|
|
501
|
+
asFunctionExpression.type = "FunctionExpression";
|
|
502
|
+
path.parentPath.replaceWith(t.expressionStatement(t.assignmentExpression("=", memberExpression, asFunctionExpression)));
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// class id{} -> var id = class {}
|
|
507
|
+
if (path.key === "id" && path.parentPath.isClassDeclaration()) {
|
|
508
|
+
var asClassExpression = (0, _node.deepClone)(path.parentPath.node);
|
|
509
|
+
asClassExpression.type = "ClassExpression";
|
|
510
|
+
path.parentPath.replaceWith(t.expressionStatement(t.assignmentExpression("=", memberExpression, asClassExpression)));
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// var id = 1 -> id = 1
|
|
515
|
+
var variableDeclaratorChild = path.find(function (p) {
|
|
516
|
+
var _p$parentPath5, _p$parentPath6;
|
|
517
|
+
return p.key === "id" && ((_p$parentPath5 = p.parentPath) === null || _p$parentPath5 === void 0 ? void 0 : _p$parentPath5.isVariableDeclarator()) && ((_p$parentPath6 = p.parentPath) === null || _p$parentPath6 === void 0 || (_p$parentPath6 = _p$parentPath6.parentPath) === null || _p$parentPath6 === void 0 ? void 0 : _p$parentPath6.isVariableDeclaration());
|
|
518
|
+
});
|
|
519
|
+
if (variableDeclaratorChild) {
|
|
520
|
+
var variableDeclarator = variableDeclaratorChild.parentPath;
|
|
521
|
+
var variableDeclaration = variableDeclarator.parentPath;
|
|
522
|
+
if (variableDeclaration.type === "VariableDeclaration") {
|
|
523
|
+
(0, _assert.ok)(variableDeclaration.node.declarations.length === 1, "Multiple declarations not supported");
|
|
524
|
+
}
|
|
525
|
+
var id = variableDeclarator.get("id");
|
|
526
|
+
var init = variableDeclarator.get("init");
|
|
527
|
+
var newExpression = id.node;
|
|
528
|
+
var isForInitializer = (variableDeclaration.key === "init" || variableDeclaration.key === "left") && variableDeclaration.parentPath.isFor();
|
|
529
|
+
if (init.node || !isForInitializer) {
|
|
530
|
+
newExpression = t.assignmentExpression("=", id.node, init.node || t.identifier("undefined"));
|
|
531
|
+
}
|
|
532
|
+
if (!isForInitializer) {
|
|
533
|
+
newExpression = t.expressionStatement(newExpression);
|
|
534
|
+
}
|
|
535
|
+
path.replaceWith(memberExpression);
|
|
536
|
+
if (variableDeclaration.isVariableDeclaration()) {
|
|
537
|
+
variableDeclaration.replaceWith(newExpression);
|
|
538
|
+
}
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Safely replace the identifier with the member expression
|
|
543
|
+
// ensureComputedExpression(path);
|
|
544
|
+
// path.replaceWith(memberExpression);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* @example
|
|
549
|
+
* undefined // true
|
|
550
|
+
* void 0 // true
|
|
551
|
+
*/
|
|
552
|
+
function isUndefined(path) {
|
|
553
|
+
if (path.isIdentifier() && path.node.name === "undefined") {
|
|
554
|
+
return true;
|
|
555
|
+
}
|
|
556
|
+
if (path.isUnaryExpression() && path.node.operator === "void" && path.node.argument.type === "NumericLiteral" && path.node.argument.value === 0) {
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.computeFunctionLength = computeFunctionLength;
|
|
7
|
+
exports.isVariableFunctionIdentifier = isVariableFunctionIdentifier;
|
|
8
|
+
var _constants = require("../constants");
|
|
9
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
10
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
11
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
12
|
+
/**
|
|
13
|
+
* @example __JS_CONFUSER_VAR__(identifier) // true
|
|
14
|
+
* @param path
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
function isVariableFunctionIdentifier(path) {
|
|
18
|
+
var _path$parentPath;
|
|
19
|
+
if (path.isIdentifier() && path.listKey === "arguments" && path.key === 0 && (_path$parentPath = path.parentPath) !== null && _path$parentPath !== void 0 && _path$parentPath.isCallExpression()) {
|
|
20
|
+
var callee = path.parentPath.get("callee");
|
|
21
|
+
return callee.isIdentifier({
|
|
22
|
+
name: _constants.variableFunctionName
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Computes the `function.length` property given the parameter nodes.
|
|
30
|
+
*
|
|
31
|
+
* @example function abc(a, b, c = 1, ...d) {} // abc.length = 2
|
|
32
|
+
*/
|
|
33
|
+
function computeFunctionLength(fnPath) {
|
|
34
|
+
var savedLength = fnPath.node[_constants.FN_LENGTH];
|
|
35
|
+
if (typeof savedLength === "number") {
|
|
36
|
+
return savedLength;
|
|
37
|
+
}
|
|
38
|
+
var count = 0;
|
|
39
|
+
var _iterator = _createForOfIteratorHelper(fnPath.node.params),
|
|
40
|
+
_step;
|
|
41
|
+
try {
|
|
42
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
43
|
+
var parameterNode = _step.value;
|
|
44
|
+
if (parameterNode.type === "Identifier" || parameterNode.type === "ObjectPattern" || parameterNode.type === "ArrayPattern") {
|
|
45
|
+
count++;
|
|
46
|
+
} else {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (err) {
|
|
51
|
+
_iterator.e(err);
|
|
52
|
+
} finally {
|
|
53
|
+
_iterator.f();
|
|
54
|
+
}
|
|
55
|
+
return count;
|
|
56
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.alphabeticalGenerator = alphabeticalGenerator;
|
|
7
|
+
exports.createZeroWidthGenerator = createZeroWidthGenerator;
|
|
8
|
+
var _constants = require("../constants");
|
|
9
|
+
var _randomUtils = require("./random-utils");
|
|
10
|
+
function alphabeticalGenerator(index) {
|
|
11
|
+
var name = "";
|
|
12
|
+
while (index > 0) {
|
|
13
|
+
var t = (index - 1) % 52;
|
|
14
|
+
var thisChar = t >= 26 ? String.fromCharCode(65 + t - 26) : String.fromCharCode(97 + t);
|
|
15
|
+
name = thisChar + name;
|
|
16
|
+
index = (index - t) / 52 | 0;
|
|
17
|
+
}
|
|
18
|
+
if (!name) {
|
|
19
|
+
name = "_";
|
|
20
|
+
}
|
|
21
|
+
return name;
|
|
22
|
+
}
|
|
23
|
+
function createZeroWidthGenerator() {
|
|
24
|
+
var maxSize = 0;
|
|
25
|
+
var currentKeyWordsArray = [];
|
|
26
|
+
function generateArray() {
|
|
27
|
+
var result = _constants.reservedKeywords.map(function (keyWord) {
|
|
28
|
+
return keyWord + "\u200C".repeat(Math.max(maxSize - keyWord.length, 1));
|
|
29
|
+
}).filter(function (craftedVariableName) {
|
|
30
|
+
return craftedVariableName.length == maxSize;
|
|
31
|
+
});
|
|
32
|
+
if (!result.length) {
|
|
33
|
+
++maxSize;
|
|
34
|
+
return generateArray();
|
|
35
|
+
}
|
|
36
|
+
return (0, _randomUtils.shuffle)(result);
|
|
37
|
+
}
|
|
38
|
+
function getNextVariable() {
|
|
39
|
+
if (!currentKeyWordsArray.length) {
|
|
40
|
+
++maxSize;
|
|
41
|
+
currentKeyWordsArray = generateArray();
|
|
42
|
+
}
|
|
43
|
+
return currentKeyWordsArray.pop();
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
generate: getNextVariable
|
|
47
|
+
};
|
|
48
|
+
}
|