js-confuser 1.7.2 → 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/.github/workflows/node.js.yml +1 -1
- package/CHANGELOG.md +105 -0
- package/Migration.md +57 -0
- package/README.md +23 -913
- package/dist/constants.js +69 -13
- package/dist/index.js +108 -152
- package/dist/obfuscator.js +316 -118
- package/dist/options.js +1 -109
- 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 +213 -93
- 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 -577
- package/dist/transforms/dispatcher.js +300 -309
- 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 +217 -103
- package/dist/transforms/identifier/movedDeclarations.js +167 -91
- package/dist/transforms/identifier/renameVariables.js +240 -187
- package/dist/transforms/lock/integrity.js +61 -184
- package/dist/transforms/lock/lock.js +263 -303
- 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 +263 -163
- package/dist/transforms/renameLabels.js +132 -56
- package/dist/transforms/rgf.js +142 -240
- package/dist/transforms/shuffle.js +52 -145
- package/dist/transforms/string/encoding.js +45 -173
- package/dist/transforms/string/stringCompression.js +81 -126
- package/dist/transforms/string/stringConcealing.js +189 -224
- 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 -77
- package/src/index.ts +70 -163
- package/src/obfuscationResult.ts +43 -0
- package/src/obfuscator.ts +328 -135
- package/src/options.ts +154 -623
- package/src/order.ts +14 -14
- package/src/presets.ts +39 -34
- package/src/probability.ts +21 -36
- package/src/templates/{bufferToString.ts → bufferToStringTemplate.ts} +5 -54
- 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 +183 -92
- package/src/transforms/astScrambler.ts +99 -0
- package/src/transforms/calculator.ts +96 -224
- package/src/transforms/controlFlowFlattening.ts +1594 -0
- package/src/transforms/deadCode.ts +85 -628
- package/src/transforms/dispatcher.ts +431 -636
- package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -299
- 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 +261 -189
- package/src/transforms/identifier/movedDeclarations.ts +228 -142
- package/src/transforms/identifier/renameVariables.ts +252 -258
- package/src/transforms/lock/integrity.ts +84 -260
- package/src/transforms/lock/lock.ts +342 -491
- 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 -215
- package/src/transforms/renameLabels.ts +176 -77
- package/src/transforms/rgf.ts +293 -386
- package/src/transforms/shuffle.ts +80 -254
- package/src/transforms/string/encoding.ts +26 -129
- package/src/transforms/string/stringCompression.ts +118 -236
- package/src/transforms/string/stringConcealing.ts +255 -339
- 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 -108
- package/dist/templates/crash.js +0 -59
- 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 -1281
- 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 -70
- package/dist/transforms/identifier/variableAnalysis.js +0 -104
- package/dist/transforms/lock/antiDebug.js +0 -76
- package/dist/transforms/stack.js +0 -343
- package/dist/transforms/transform.js +0 -350
- 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 -9
- 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 -130
- package/dist/util/scope.js +0 -20
- package/docs/ControlFlowFlattening.md +0 -595
- package/docs/Countermeasures.md +0 -63
- package/docs/ES5.md +0 -197
- package/docs/Integrity.md +0 -75
- package/docs/RGF.md +0 -419
- 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/crash.ts +0 -55
- 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 -2146
- package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
- package/src/transforms/es5/antiClass.ts +0 -272
- 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 -85
- package/src/transforms/identifier/variableAnalysis.ts +0 -118
- package/src/transforms/lock/antiDebug.ts +0 -112
- package/src/transforms/stack.ts +0 -551
- package/src/transforms/transform.ts +0 -453
- package/src/traverse.ts +0 -120
- package/src/types.ts +0 -131
- package/src/util/compare.ts +0 -181
- package/src/util/gen.ts +0 -651
- package/src/util/guard.ts +0 -7
- 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 -141
- package/src/util/scope.ts +0 -21
- package/test/code/Cash.src.js +0 -1011
- package/test/code/Cash.test.ts +0 -49
- 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 -132
- package/test/presets.test.ts +0 -22
- package/test/probability.test.ts +0 -44
- package/test/templates/template.test.ts +0 -14
- 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 -72
- package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
- package/test/transforms/identifier/renameVariables.test.ts +0 -621
- 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/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
|
@@ -1,491 +1,486 @@
|
|
|
1
1
|
"use strict";
|
|
2
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); }
|
|
3
4
|
Object.defineProperty(exports, "__esModule", {
|
|
4
5
|
value: true
|
|
5
6
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
|
8
9
|
var _order = require("../order");
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var
|
|
18
|
-
function
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
var _astUtils = require("../utils/ast-utils");
|
|
11
|
+
var _constants = require("../constants");
|
|
12
|
+
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); }
|
|
13
|
+
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; }
|
|
14
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
15
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
16
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
17
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
18
|
+
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; } } }; }
|
|
19
|
+
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; } }
|
|
20
|
+
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; }
|
|
21
|
+
var identifierMap = new Map();
|
|
22
|
+
identifierMap.set("undefined", function () {
|
|
23
|
+
return t.unaryExpression("void", t.numericLiteral(0));
|
|
24
|
+
});
|
|
25
|
+
identifierMap.set("Infinity", function () {
|
|
26
|
+
return t.binaryExpression("/", t.numericLiteral(1), t.numericLiteral(0));
|
|
27
|
+
});
|
|
28
|
+
|
|
22
29
|
/**
|
|
23
|
-
*
|
|
30
|
+
* Minify removes unnecessary code and shortens the length for file size.
|
|
24
31
|
*
|
|
25
|
-
*
|
|
26
|
-
* -
|
|
27
|
-
* -
|
|
28
|
-
* -
|
|
32
|
+
* - Dead code elimination
|
|
33
|
+
* - Variable grouping
|
|
34
|
+
* - Constant folding
|
|
35
|
+
* - Shorten literals: True to !0, False to !1, Infinity to 1/0, Undefined to void 0
|
|
36
|
+
* - Remove unused variables, functions
|
|
29
37
|
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return () => {
|
|
44
|
-
var body = object.type == "SwitchCase" ? object.consequent : (0, _insert.getBlockBody)(object);
|
|
45
|
-
var earlyReturn = body.length;
|
|
46
|
-
var fnDecs = [];
|
|
47
|
-
body.forEach((stmt, i) => {
|
|
48
|
-
if (stmt.type === "ReturnStatement" || stmt.type === "BreakStatement" || stmt.type === "ContinueStatement" || stmt.type === "ThrowStatement") {
|
|
49
|
-
if (earlyReturn > i + 1) {
|
|
50
|
-
earlyReturn = i + 1;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (stmt.type == "FunctionDeclaration") {
|
|
54
|
-
fnDecs.push([stmt, i]);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
if (earlyReturn < body.length) {
|
|
58
|
-
body.length = earlyReturn;
|
|
59
|
-
body.push(...fnDecs.filter(x => x[1] >= earlyReturn).map(x => x[0]));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Now combine ExpressionStatements
|
|
38
|
+
var _default = exports["default"] = function _default(_ref) {
|
|
39
|
+
var Plugin = _ref.Plugin;
|
|
40
|
+
var me = Plugin(_order.Order.Minify);
|
|
41
|
+
return {
|
|
42
|
+
visitor: {
|
|
43
|
+
Program: function Program(path) {
|
|
44
|
+
path.scope.crawl();
|
|
45
|
+
},
|
|
46
|
+
// var a; var b; -> var a,b;
|
|
47
|
+
VariableDeclaration: {
|
|
48
|
+
exit: function exit(path) {
|
|
49
|
+
if (typeof path.key !== "number") return;
|
|
50
|
+
var kind = path.node.kind;
|
|
63
51
|
|
|
64
|
-
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
52
|
+
// get declaration after this
|
|
53
|
+
var nextDeclaration = path.getSibling(path.key + 1);
|
|
54
|
+
if (nextDeclaration.isVariableDeclaration({
|
|
55
|
+
kind: kind
|
|
56
|
+
})) {
|
|
57
|
+
var _nextDeclaration$node;
|
|
58
|
+
// Add bindings back
|
|
59
|
+
var addBindingsToScope = function addBindingsToScope(scope) {
|
|
60
|
+
for (var name in bindings) {
|
|
61
|
+
var binding = bindings[name];
|
|
62
|
+
if (binding) {
|
|
63
|
+
binding.path = newBindingIdentifierPaths[name];
|
|
64
|
+
scope.bindings[name] = binding;
|
|
65
|
+
}
|
|
73
66
|
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
};
|
|
68
|
+
var declarations = path.get("declarations");
|
|
69
|
+
|
|
70
|
+
// Preserve bindings!
|
|
71
|
+
// This is important for dead code elimination
|
|
72
|
+
var bindings = Object.create(null);
|
|
73
|
+
var _iterator = _createForOfIteratorHelper(declarations),
|
|
74
|
+
_step;
|
|
75
|
+
try {
|
|
76
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
77
|
+
var declaration = _step.value;
|
|
78
|
+
for (var _i = 0, _Object$values = Object.values(declaration.getBindingIdentifierPaths()); _i < _Object$values.length; _i++) {
|
|
79
|
+
var idPath = _Object$values[_i];
|
|
80
|
+
bindings[idPath.node.name] = idPath.scope.getBinding(idPath.node.name);
|
|
81
|
+
}
|
|
80
82
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
} catch (err) {
|
|
84
|
+
_iterator.e(err);
|
|
85
|
+
} finally {
|
|
86
|
+
_iterator.f();
|
|
83
87
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
index: startIndex
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
sequences.reverse().forEach(seq => {
|
|
92
|
-
(0, _assert.ok)(seq.index != -1);
|
|
93
|
-
body.splice(seq.index, seq.exprs.length, (0, _gen.ExpressionStatement)(seq.exprs.length == 1 ? seq.exprs[0] : (0, _gen.SequenceExpression)(seq.exprs)));
|
|
94
|
-
});
|
|
95
|
-
}
|
|
88
|
+
(_nextDeclaration$node = nextDeclaration.node.declarations).unshift.apply(_nextDeclaration$node, _toConsumableArray(declarations.map(function (x) {
|
|
89
|
+
return x.node;
|
|
90
|
+
})));
|
|
91
|
+
var newBindingIdentifierPaths = nextDeclaration.getBindingIdentifierPaths();
|
|
96
92
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
body.pop();
|
|
93
|
+
// path.remove() unfortunately removes the bindings
|
|
94
|
+
// We must perverse the entire binding object (referencePaths, constantViolations, etc)
|
|
95
|
+
// and re-add them to the new scope
|
|
96
|
+
path.remove();
|
|
97
|
+
if (kind === "var") {
|
|
98
|
+
addBindingsToScope((0, _astUtils.getParentFunctionOrProgram)(path).scope);
|
|
104
99
|
}
|
|
100
|
+
addBindingsToScope(path.scope);
|
|
105
101
|
}
|
|
106
102
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// var a=1,b=1,c=1;
|
|
114
|
-
var lastDec = null;
|
|
115
|
-
var remove = [];
|
|
116
|
-
body.forEach((x, i) => {
|
|
117
|
-
if (x.type === "VariableDeclaration") {
|
|
118
|
-
if (!lastDec || lastDec.kind !== x.kind || !lastDec.declarations.length) {
|
|
119
|
-
lastDec = x;
|
|
120
|
-
} else {
|
|
121
|
-
lastDec.declarations.push(...(0, _insert.clone)(x.declarations));
|
|
122
|
-
remove.unshift(i);
|
|
123
|
-
}
|
|
103
|
+
},
|
|
104
|
+
// true -> !0, false -> !1
|
|
105
|
+
BooleanLiteral: {
|
|
106
|
+
exit: function exit(path) {
|
|
107
|
+
if (path.node.value) {
|
|
108
|
+
path.replaceWith(t.unaryExpression("!", t.numericLiteral(0)));
|
|
124
109
|
} else {
|
|
125
|
-
|
|
110
|
+
path.replaceWith(t.unaryExpression("!", t.numericLiteral(1)));
|
|
126
111
|
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return () => {
|
|
141
|
-
// Don't touch `{get key(){...}}`
|
|
142
|
-
var propIndex = parents.findIndex(x => x.type == "Property" || x.type == "MethodDefinition");
|
|
143
|
-
if (propIndex !== -1) {
|
|
144
|
-
if (parents[propIndex].value === (parents[propIndex - 1] || object)) {
|
|
145
|
-
if (parents[propIndex].kind !== "init" || parents[propIndex].method) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
// !"" -> !1
|
|
115
|
+
UnaryExpression: {
|
|
116
|
+
exit: function exit(path) {
|
|
117
|
+
if (path.node.operator === "!") {
|
|
118
|
+
var argument = path.get("argument");
|
|
119
|
+
if (argument.isNumericLiteral()) return;
|
|
120
|
+
var value = argument.evaluateTruthy();
|
|
121
|
+
var parent = (0, _astUtils.getParentFunctionOrProgram)(path);
|
|
122
|
+
if (parent && parent.node[_constants.UNSAFE]) return;
|
|
123
|
+
if (value === undefined) return;
|
|
124
|
+
path.replaceWith(t.unaryExpression("!", t.numericLiteral(value ? 1 : 0)));
|
|
148
125
|
}
|
|
149
126
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
127
|
+
},
|
|
128
|
+
// a["key"] -> a.key
|
|
129
|
+
MemberExpression: {
|
|
130
|
+
exit: function exit(path) {
|
|
131
|
+
if (!path.node.computed) return;
|
|
132
|
+
var property = path.get("property");
|
|
133
|
+
if (!property.isStringLiteral()) return;
|
|
134
|
+
var key = property.node.value;
|
|
135
|
+
if (!t.isValidIdentifier(key)) return;
|
|
136
|
+
path.node.computed = false;
|
|
137
|
+
path.node.property = t.identifier(key);
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
// {["key"]: 1} -> {key: 1}
|
|
141
|
+
// {"key": 1} -> {key: 1}
|
|
142
|
+
ObjectProperty: {
|
|
143
|
+
exit: function exit(path) {
|
|
144
|
+
var key = path.get("key");
|
|
145
|
+
if (path.node.computed && key.isStringLiteral()) {
|
|
146
|
+
path.node.computed = false;
|
|
154
147
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
148
|
+
if (!path.node.computed && key.isStringLiteral() && t.isValidIdentifier(key.node.value)) {
|
|
149
|
+
if (identifierMap.has(key.node.value)) {
|
|
150
|
+
path.node.computed = true;
|
|
151
|
+
key.replaceWith(identifierMap.get(key.node.value)());
|
|
152
|
+
} else {
|
|
153
|
+
key.replaceWith(t.identifier(key.node.value));
|
|
154
|
+
}
|
|
158
155
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
// (a); -> a;
|
|
159
|
+
SequenceExpression: {
|
|
160
|
+
exit: function exit(path) {
|
|
161
|
+
if (path.node.expressions.length === 1) {
|
|
162
|
+
path.replaceWith(path.node.expressions[0]);
|
|
165
163
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
// ; -> ()
|
|
167
|
+
EmptyStatement: {
|
|
168
|
+
exit: function exit(path) {
|
|
169
|
+
path.remove();
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
// console; -> ();
|
|
173
|
+
ExpressionStatement: {
|
|
174
|
+
exit: function exit(path) {
|
|
175
|
+
if (path.get("expression").isIdentifier()) {
|
|
176
|
+
var _path$parentPath, _path$parentPath2;
|
|
177
|
+
// Preserve last expression of program for RGF
|
|
178
|
+
if ((_path$parentPath = path.parentPath) !== null && _path$parentPath !== void 0 && _path$parentPath.isProgram() && ((_path$parentPath2 = path.parentPath) === null || _path$parentPath2 === void 0 ? void 0 : _path$parentPath2.get("body").at(-1)) === path) return;
|
|
179
|
+
path.remove();
|
|
170
180
|
}
|
|
171
181
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
182
|
+
},
|
|
183
|
+
// undefined -> void 0
|
|
184
|
+
// Infinity -> 1/0
|
|
185
|
+
Identifier: {
|
|
186
|
+
exit: function exit(path) {
|
|
187
|
+
if (path.isReferencedIdentifier()) {
|
|
188
|
+
if (identifierMap.has(path.node.name)) {
|
|
189
|
+
(0, _astUtils.ensureComputedExpression)(path);
|
|
190
|
+
path.replaceWith(identifierMap.get(path.node.name)());
|
|
179
191
|
}
|
|
180
|
-
|
|
181
|
-
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
// true ? a : b -> a
|
|
196
|
+
ConditionalExpression: {
|
|
197
|
+
exit: function exit(path) {
|
|
198
|
+
var testValue = path.get("test").evaluateTruthy();
|
|
199
|
+
if (testValue === undefined) return;
|
|
200
|
+
path.replaceWith(testValue ? path.node.consequent : path.node.alternate);
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
// Remove unused functions
|
|
204
|
+
FunctionDeclaration: {
|
|
205
|
+
exit: function exit(path) {
|
|
206
|
+
var id = path.get("id");
|
|
207
|
+
if (id.isIdentifier() && !id.node.name.startsWith(_constants.placeholderVariablePrefix)) {
|
|
208
|
+
var binding = path.scope.getBinding(id.node.name);
|
|
209
|
+
if (binding && binding.constantViolations.length === 0 && binding.referencePaths.length === 0 && !binding.referenced) {
|
|
210
|
+
path.remove();
|
|
182
211
|
}
|
|
183
212
|
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
// var x=undefined -> var x
|
|
216
|
+
// Remove unused variables
|
|
217
|
+
// Simple destructuring
|
|
218
|
+
VariableDeclarator: {
|
|
219
|
+
exit: function exit(path) {
|
|
220
|
+
if ((0, _astUtils.isUndefined)(path.get("init"))) {
|
|
221
|
+
path.node.init = null;
|
|
222
|
+
}
|
|
223
|
+
var id = path.get("id");
|
|
224
|
+
var init = path.get("init");
|
|
191
225
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
226
|
+
// Simple array/object destructuring
|
|
227
|
+
if (id.isArrayPattern() && init.isArrayExpression()) {
|
|
228
|
+
var elements = id.get("elements");
|
|
229
|
+
var initElements = init.get("elements");
|
|
230
|
+
if (elements.length === 1 && initElements.length === 1) {
|
|
231
|
+
id.replaceWith(elements[0]);
|
|
232
|
+
init.replaceWith(initElements[0]);
|
|
197
233
|
}
|
|
198
|
-
`).single({
|
|
199
|
-
ObjectDefineProperty: this.options.preserveFunctionLength ? this.createInitVariable(_globals.ObjectDefineProperty, parents) : undefined
|
|
200
|
-
}));
|
|
201
234
|
}
|
|
202
|
-
|
|
203
|
-
var
|
|
204
|
-
var
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
type: "ArrowFunctionExpression"
|
|
217
|
-
};
|
|
218
|
-
this.replace(object, (0, _gen.VariableDeclaration)((0, _gen.VariableDeclarator)(object.id.name, wrap(arrow))));
|
|
219
|
-
var x = this.transform(arrow, []);
|
|
220
|
-
if (typeof x === "function") {
|
|
221
|
-
x();
|
|
235
|
+
if (id.isObjectPattern() && init.isObjectExpression()) {
|
|
236
|
+
var properties = id.get("properties");
|
|
237
|
+
var initProperties = init.get("properties");
|
|
238
|
+
if (properties.length === 1 && initProperties.length === 1) {
|
|
239
|
+
var firstProperty = properties[0];
|
|
240
|
+
var firstInitProperty = initProperties[0];
|
|
241
|
+
if (firstProperty.isObjectProperty() && firstInitProperty.isObjectProperty()) {
|
|
242
|
+
var firstKey = firstProperty.get("key");
|
|
243
|
+
var firstInitKey = firstInitProperty.get("key");
|
|
244
|
+
if (firstKey.isIdentifier() && firstInitKey.isIdentifier() && firstKey.node.name === firstInitKey.node.name) {
|
|
245
|
+
id.replaceWith(firstProperty.node.value);
|
|
246
|
+
init.replaceWith(firstInitProperty.node.value);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
222
249
|
}
|
|
223
250
|
}
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
251
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
} else {
|
|
242
|
-
// ()=>{exprStmt;exprStmt;} -> ()=>(expr, expr, expr, undefined)
|
|
243
|
-
var exprs = body.filter(x => x.type == "ExpressionStatement");
|
|
244
|
-
if (exprs.length == body.length) {
|
|
245
|
-
var array = [];
|
|
246
|
-
function flatten(expr) {
|
|
247
|
-
if (expr.type == "SequenceExpression") {
|
|
248
|
-
expr.expressions.forEach(flatten);
|
|
249
|
-
} else if (expr.type == "ExpressionStatement") {
|
|
250
|
-
flatten(expr.expression);
|
|
251
|
-
} else {
|
|
252
|
-
array.push(expr);
|
|
252
|
+
// Remove unused variables
|
|
253
|
+
// Can only remove if it's pure
|
|
254
|
+
if (id.isIdentifier()) {
|
|
255
|
+
// Do not remove variables in unsafe functions
|
|
256
|
+
var fn = (0, _astUtils.getParentFunctionOrProgram)(path);
|
|
257
|
+
if (fn.node[_constants.UNSAFE]) return;
|
|
258
|
+
var binding = path.scope.getBinding(id.node.name);
|
|
259
|
+
if (binding && binding.constantViolations.length === 0 && binding.referencePaths.length === 0) {
|
|
260
|
+
if (!init.node || init.isPure()) {
|
|
261
|
+
path.remove();
|
|
262
|
+
} else if (path.parentPath.isVariableDeclaration() && path.parentPath.node.declarations.length === 1) {
|
|
263
|
+
path.parentPath.replaceWith(t.expressionStatement(init.node));
|
|
253
264
|
}
|
|
254
265
|
}
|
|
255
|
-
body.forEach(flatten);
|
|
256
|
-
object.body = (0, _gen.SequenceExpression)([...(0, _insert.clone)(array), (0, _gen.UnaryExpression)("void", (0, _gen.Literal)(0))]);
|
|
257
266
|
}
|
|
258
267
|
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (object.expressions.length == 1) {
|
|
266
|
-
this.replace(object, (0, _insert.clone)(object.expressions[0]));
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// a += -1 -> a -= 1
|
|
272
|
-
if (object.type == "AssignmentExpression") {
|
|
273
|
-
return () => {
|
|
274
|
-
if (object.operator == "+=" && object.right.type == "UnaryExpression" && object.right.operator == "-") {
|
|
275
|
-
object.operator = "-=";
|
|
276
|
-
object.right = object.right.argument;
|
|
277
|
-
} else if (
|
|
278
|
-
// a -= -1 -> a += 1
|
|
279
|
-
object.operator == "-=" && object.right.type == "UnaryExpression" && object.right.operator == "-") {
|
|
280
|
-
object.operator = "+=";
|
|
281
|
-
object.right = object.right.argument;
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// a + -b -> a - b
|
|
287
|
-
if (object.type == "BinaryExpression") {
|
|
288
|
-
return () => {
|
|
289
|
-
if (object.operator == "+" && object.right.type == "UnaryExpression" && object.right.operator == "-") {
|
|
290
|
-
object.operator = "-";
|
|
291
|
-
object.right = object.right.argument;
|
|
292
|
-
} else if (
|
|
293
|
-
// a - -1 -> a + 1
|
|
294
|
-
object.operator == "-" && object.right.type == "UnaryExpression" && object.right.operator == "-") {
|
|
295
|
-
object.operator = "+";
|
|
296
|
-
object.right = object.right.argument;
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
if (object.type == "ForStatement" || object.type == "ForInStatement" || object.type == "ForOfStatement" || object.type == "WhileStatement") {
|
|
301
|
-
if (object.body.type == "BlockStatement") {
|
|
302
|
-
return () => {
|
|
303
|
-
if (object.body.body.length === 1) {
|
|
304
|
-
object.body = object.body.body[0];
|
|
268
|
+
},
|
|
269
|
+
// return undefined->return
|
|
270
|
+
ReturnStatement: {
|
|
271
|
+
exit: function exit(path) {
|
|
272
|
+
if ((0, _astUtils.isUndefined)(path.get("argument"))) {
|
|
273
|
+
path.node.argument = null;
|
|
305
274
|
}
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// Last switch case does not need break
|
|
311
|
-
if (object.type == "SwitchStatement") {
|
|
312
|
-
var last = object.cases[object.cases.length - 1];
|
|
313
|
-
if (last) {
|
|
314
|
-
var lastStatement = last.consequent[last.consequent.length - 1];
|
|
315
|
-
if (lastStatement && lastStatement.type == "BreakStatement" && lastStatement.label == null) {
|
|
316
|
-
last.consequent.pop();
|
|
317
275
|
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
276
|
+
},
|
|
277
|
+
// while(true) {a();} -> while(true) a();
|
|
278
|
+
// for(;;) {a();} -> for(;;) a();
|
|
279
|
+
// with(a) {a();} -> with(a) a();
|
|
280
|
+
"While|For|WithStatement": {
|
|
281
|
+
exit: function exit(_path) {
|
|
282
|
+
var path = _path;
|
|
283
|
+
var body = path.get("body");
|
|
284
|
+
if (body.isBlock() && body.node.body.length === 1) {
|
|
285
|
+
body.replaceWith(body.node.body[0]);
|
|
328
286
|
}
|
|
329
287
|
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
object.alternate = null;
|
|
349
|
-
} else {
|
|
350
|
-
object.consequent = (0, _gen.BlockStatement)([]);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
return () => {
|
|
354
|
-
// if ( a ) { } else {b()} -> if ( !a ) b();
|
|
355
|
-
if (body.length == 0 && object.alternate) {
|
|
356
|
-
object.test = (0, _gen.UnaryExpression)("!", (0, _insert.clone)(object.test));
|
|
357
|
-
if (object.alternate.type == "BlockStatement" && object.alternate.body.length == 1) {
|
|
358
|
-
object.alternate = (0, _insert.clone)(object.alternate.body[0]);
|
|
288
|
+
},
|
|
289
|
+
// if(a) a(); -> a && a();
|
|
290
|
+
// if(a) { return b; } -> if(a) return b;
|
|
291
|
+
// if(a) { a(); } else { b(); } -> a ? a() : b();
|
|
292
|
+
// if(a) { return b; } else { return c; } -> return a ? b : c;
|
|
293
|
+
IfStatement: {
|
|
294
|
+
exit: function exit(path) {
|
|
295
|
+
// BlockStatement to single statement
|
|
296
|
+
var consequent = path.get("consequent");
|
|
297
|
+
var alternate = path.get("alternate");
|
|
298
|
+
var isMoveable = function isMoveable(node) {
|
|
299
|
+
if (t.isDeclaration(node)) return false;
|
|
300
|
+
return true;
|
|
301
|
+
};
|
|
302
|
+
var testValue = path.get("test").evaluateTruthy();
|
|
303
|
+
var parent = (0, _astUtils.getParentFunctionOrProgram)(path);
|
|
304
|
+
if (parent && parent.node[_constants.UNSAFE]) {
|
|
305
|
+
testValue = undefined;
|
|
359
306
|
}
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
// if (a) {return b;} else {return c;} -> return a ? b : c;
|
|
368
|
-
if (stmt1.type == "ReturnStatement" && stmt2.type == "ReturnStatement") {
|
|
369
|
-
this.replace(object, (0, _gen.ReturnStatement)((0, _gen.ConditionalExpression)((0, _insert.clone)(object.test), stmt1.argument || (0, _gen.Identifier)("undefined"), stmt2.argument || (0, _gen.Identifier)("undefined"))));
|
|
307
|
+
if (typeof testValue !== "undefined") {
|
|
308
|
+
if (!alternate.node && consequent.isBlock() && consequent.node.body.length === 1 && isMoveable(consequent.node.body[0])) {
|
|
309
|
+
consequent.replaceWith(consequent.node.body[0]);
|
|
310
|
+
}
|
|
311
|
+
if (alternate.node && alternate.isBlock() && alternate.node.body.length === 1 && isMoveable(alternate.node.body[0])) {
|
|
312
|
+
alternate.replaceWith(alternate.node.body[0]);
|
|
313
|
+
}
|
|
370
314
|
}
|
|
315
|
+
if (testValue === false) {
|
|
316
|
+
// if(false){} -> ()
|
|
317
|
+
if (!alternate.node) {
|
|
318
|
+
path.remove();
|
|
319
|
+
return;
|
|
371
320
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
321
|
+
// if(false){a()}else{b()} -> b()
|
|
322
|
+
} else {
|
|
323
|
+
path.replaceWith(alternate.node);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// if(true){a()} -> {a()}
|
|
328
|
+
} else if (testValue === true) {
|
|
329
|
+
path.replaceWith(consequent.node);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
function getResult(path) {
|
|
333
|
+
if (!path.node) return null;
|
|
334
|
+
if (path.isReturnStatement()) {
|
|
335
|
+
return {
|
|
336
|
+
returnPath: path,
|
|
337
|
+
expressions: []
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
if (path.isExpressionStatement()) {
|
|
341
|
+
return {
|
|
342
|
+
returnPath: null,
|
|
343
|
+
expressions: [path.get("expression").node]
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
if (path.isBlockStatement()) {
|
|
347
|
+
var expressions = [];
|
|
348
|
+
var _iterator2 = _createForOfIteratorHelper(path.get("body")),
|
|
349
|
+
_step2;
|
|
350
|
+
try {
|
|
351
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
352
|
+
var statement = _step2.value;
|
|
353
|
+
if (statement.isReturnStatement()) {
|
|
354
|
+
return {
|
|
355
|
+
returnPath: statement,
|
|
356
|
+
expressions: expressions
|
|
357
|
+
};
|
|
358
|
+
} else if (statement.isExpressionStatement()) {
|
|
359
|
+
expressions.push(statement.get("expression").node);
|
|
360
|
+
} else {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
} catch (err) {
|
|
365
|
+
_iterator2.e(err);
|
|
366
|
+
} finally {
|
|
367
|
+
_iterator2.f();
|
|
379
368
|
}
|
|
369
|
+
return {
|
|
370
|
+
returnPath: null,
|
|
371
|
+
expressions: expressions
|
|
372
|
+
};
|
|
380
373
|
}
|
|
374
|
+
return null;
|
|
381
375
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
376
|
+
var consequentReturn = getResult(consequent);
|
|
377
|
+
var alternateReturn = getResult(alternate);
|
|
378
|
+
if (consequentReturn && alternateReturn) {
|
|
379
|
+
if (consequentReturn.returnPath && alternateReturn.returnPath) {
|
|
380
|
+
var createReturnArgument = function createReturnArgument(resultInfo) {
|
|
381
|
+
return t.sequenceExpression([].concat(_toConsumableArray(resultInfo.expressions), [resultInfo.returnPath.node.argument || t.identifier("undefined")]));
|
|
382
|
+
};
|
|
383
|
+
path.replaceWith(t.returnStatement(t.conditionalExpression(path.node.test, createReturnArgument(consequentReturn), createReturnArgument(alternateReturn))));
|
|
384
|
+
} else if (!consequentReturn.returnPath && !alternateReturn.returnPath) {
|
|
385
|
+
var joinExpressions = function joinExpressions(expressions) {
|
|
386
|
+
// condition?():() is invalid syntax
|
|
387
|
+
// Just use 0 as a placeholder
|
|
388
|
+
if (expressions.length === 0) return t.numericLiteral(0);
|
|
385
389
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
if (property.type == "Literal" && (0, _compare.isValidIdentifier)(property.value)) {
|
|
393
|
-
object.computed = false;
|
|
394
|
-
object.property.type = "Identifier";
|
|
395
|
-
object.property.name = (0, _insert.clone)(object.property.value);
|
|
396
|
-
|
|
397
|
-
// obj.name &&
|
|
398
|
-
// this.log(
|
|
399
|
-
// obj.name +
|
|
400
|
-
// "['" +
|
|
401
|
-
// object.property.name +
|
|
402
|
-
// "'] -> " +
|
|
403
|
-
// obj.name +
|
|
404
|
-
// "." +
|
|
405
|
-
// object.property.name
|
|
406
|
-
// );
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
if (object.type == "CallExpression") {
|
|
410
|
-
if (object.callee.type == "MemberExpression") {
|
|
411
|
-
var key = object.callee.computed ? object.callee.property.value : object.callee.property.name;
|
|
412
|
-
if (key == "toString" && object.arguments.length == 0) {
|
|
413
|
-
this.replace(object, (0, _gen.BinaryExpression)("+", (0, _gen.Literal)(""), (0, _insert.clone)(object.callee.object)));
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// { "x": 1 } -> {x: 1}
|
|
419
|
-
if (object.type === "Property" || object.type === "MethodDefinition") {
|
|
420
|
-
if (object.key.type == "SequenceExpression" && object.key.expressions.length == 1) {
|
|
421
|
-
object.key = object.key.expressions[0];
|
|
422
|
-
object.computed = true;
|
|
423
|
-
}
|
|
424
|
-
if (object.key.type == "Literal" && typeof object.key.value === "string" && (0, _compare.isValidIdentifier)(object.key.value)) {
|
|
425
|
-
object.key.type = "Identifier";
|
|
426
|
-
object.key.name = object.key.value;
|
|
427
|
-
object.computed = false;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
if (object.type == "VariableDeclarator") {
|
|
431
|
-
// undefined is not necessary
|
|
432
|
-
if (object.init && object.init.type == "Identifier") {
|
|
433
|
-
if (object.init.name == "undefined") {
|
|
434
|
-
object.init = null;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
if (object.id.type == "ObjectPattern" && object.init && object.init.type == "ObjectExpression") {
|
|
438
|
-
if (object.id.properties.length === 1 && object.init.properties.length === 1) {
|
|
439
|
-
var key1 = object.id.properties[0].computed ? object.id.properties[0].key.value : object.id.properties[0].key.name;
|
|
440
|
-
var key2 = object.init.properties[0].computed ? object.init.properties[0].key.value : object.init.properties[0].key.name;
|
|
441
|
-
|
|
442
|
-
// console.log(key1, key2);
|
|
443
|
-
|
|
444
|
-
if (key1 && key2 && key1 === key2) {
|
|
445
|
-
object.id = object.id.properties[0].value;
|
|
446
|
-
object.init = object.init.properties[0].value;
|
|
390
|
+
// No need for sequence expression if there's only one expression
|
|
391
|
+
if (expressions.length === 1) return expressions[0];
|
|
392
|
+
return t.sequenceExpression(expressions);
|
|
393
|
+
};
|
|
394
|
+
path.replaceWith(t.conditionalExpression(path.node.test, joinExpressions(consequentReturn.expressions), joinExpressions(alternateReturn.expressions)));
|
|
395
|
+
}
|
|
447
396
|
}
|
|
448
397
|
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
//
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
398
|
+
},
|
|
399
|
+
// Remove unreachable code
|
|
400
|
+
// Code after a return/throw/break/continue is unreachable
|
|
401
|
+
// Remove implied returns
|
|
402
|
+
// Remove code after if all branches are unreachable
|
|
403
|
+
"Block|SwitchCase": {
|
|
404
|
+
enter: function enter(path) {
|
|
405
|
+
if (path.isProgram()) {
|
|
406
|
+
path.scope.crawl();
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
exit: function exit(path) {
|
|
410
|
+
var statementList = path.isBlock() ? path.get("body") : path.get("consequent");
|
|
411
|
+
var impliedReturn;
|
|
412
|
+
function isUnreachable(statementList) {
|
|
413
|
+
var topLevel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
414
|
+
var unreachableState = false;
|
|
415
|
+
var _iterator3 = _createForOfIteratorHelper(statementList),
|
|
416
|
+
_step3;
|
|
417
|
+
try {
|
|
418
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
419
|
+
var statement = _step3.value;
|
|
420
|
+
if (unreachableState) {
|
|
421
|
+
statement.remove();
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
if (statement.isIfStatement()) {
|
|
425
|
+
var consequent = statement.get("consequent");
|
|
426
|
+
var alternate = statement.get("alternate");
|
|
427
|
+
if ([consequent, alternate].every(function (x) {
|
|
428
|
+
return x.node && x.isBlockStatement() && isUnreachable(x.get("body"));
|
|
429
|
+
})) {
|
|
430
|
+
unreachableState = true;
|
|
431
|
+
if (!topLevel) {
|
|
432
|
+
return true;
|
|
433
|
+
} else {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (statement.isSwitchStatement()) {
|
|
439
|
+
// Can only remove switch statements if all cases are unreachable
|
|
440
|
+
// And all paths are exhausted
|
|
441
|
+
var cases = statement.get("cases");
|
|
442
|
+
var hasDefaultCase = cases.some(function (x) {
|
|
443
|
+
return !x.node.test;
|
|
444
|
+
});
|
|
445
|
+
if (hasDefaultCase && cases.every(function (x) {
|
|
446
|
+
return isUnreachable(x.get("consequent"));
|
|
447
|
+
})) {
|
|
448
|
+
unreachableState = true;
|
|
449
|
+
if (!topLevel) {
|
|
450
|
+
return true;
|
|
451
|
+
} else {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (statement.isReturnStatement() || statement.isThrowStatement() || statement.isBreakStatement() || statement.isContinueStatement()) {
|
|
457
|
+
unreachableState = true;
|
|
458
|
+
if (!topLevel) {
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (topLevel) {
|
|
463
|
+
if (statement == statementList.at(-1) && statement.isReturnStatement() && !statement.node.argument) {
|
|
464
|
+
impliedReturn = statement;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
} catch (err) {
|
|
469
|
+
_iterator3.e(err);
|
|
470
|
+
} finally {
|
|
471
|
+
_iterator3.f();
|
|
472
|
+
}
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
isUnreachable(statementList, true);
|
|
476
|
+
if (impliedReturn) {
|
|
477
|
+
var functionParent = path.getFunctionParent();
|
|
478
|
+
if (functionParent && t.isBlockStatement(functionParent.node.body) && functionParent.node.body === path.node) {
|
|
479
|
+
impliedReturn.remove();
|
|
480
|
+
}
|
|
481
|
+
}
|
|
476
482
|
}
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
if (object.type == "UnaryExpression" && object.operator == "!") {
|
|
480
|
-
if (object.argument.type == "Literal" && !object.argument.regex) {
|
|
481
|
-
this.replace(object, (0, _gen.Literal)(!object.argument.value));
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
if (object.type == "ConditionalExpression") {
|
|
485
|
-
if (object.test.type == "Literal" && !object.test.regex) {
|
|
486
|
-
this.replace(object, object.test.value ? object.consequent : object.alternate);
|
|
487
483
|
}
|
|
488
484
|
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
exports.default = Minify;
|
|
485
|
+
};
|
|
486
|
+
};
|