diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-12-01 18:10:37 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-01 18:10:37 +0800 |
commit | e915832a36730c7eb29328c84ab9c5eecf1cb1f6 (patch) | |
tree | 4f950ec2bb8c49cdcc8b6758609ad1b02b06c449 | |
parent | 0593892d6efeee15b54bc91bdd0ef1224a466541 (diff) | |
download | tracifyjs-e915832a36730c7eb29328c84ab9c5eecf1cb1f6.tar.gz tracifyjs-e915832a36730c7eb29328c84ab9c5eecf1cb1f6.zip |
enhance `unused` (#3617)
-rw-r--r-- | lib/compress.js | 30 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 40 | ||||
-rw-r--r-- | test/compress/drop-unused.js | 12 | ||||
-rw-r--r-- | test/compress/evaluate.js | 4 | ||||
-rw-r--r-- | test/compress/functions.js | 2 | ||||
-rw-r--r-- | test/compress/keep_fargs.js | 6 | ||||
-rw-r--r-- | test/compress/reduce_vars.js | 12 |
7 files changed, 62 insertions, 44 deletions
diff --git a/lib/compress.js b/lib/compress.js index 6a76fc32..09019a74 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5173,24 +5173,42 @@ merge(Compressor.prototype, { && !fn.uses_arguments && !fn.pinned()) { var pos = 0, last = 0; + var side_effects = []; for (var i = 0; i < self.args.length; i++) { var trim = i >= fn.argnames.length; if (trim || fn.argnames[i].__unused) { var node = self.args[i].drop_side_effect_free(compressor); - if (node) { - self.args[pos++] = node; + if (exp === fn) { + fn.argnames.splice(i, 1); + self.args.splice(i, 1); + if (node) side_effects.push(node); + i--; + continue; + } else if (node) { + side_effects.push(node); + self.args[pos++] = make_sequence(self, side_effects); + side_effects = []; } else if (!trim) { - self.args[pos++] = make_node(AST_Number, self.args[i], { - value: 0 - }); + if (side_effects.length) { + node = make_sequence(self, side_effects); + side_effects = []; + } else { + node = make_node(AST_Number, self.args[i], { + value: 0 + }); + } + self.args[pos++] = node; continue; } } else { - self.args[pos++] = self.args[i]; + side_effects.push(self.args[i]); + self.args[pos++] = make_sequence(self, side_effects); + side_effects = []; } last = pos; } self.args.length = last; + if (side_effects.length) self.args.push(make_sequence(self, side_effects)); } if (compressor.option("unsafe")) { if (is_undeclared_ref(exp)) switch (exp.name) { diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 75dddb02..eef63779 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1047,12 +1047,12 @@ collapse_vars_repeated: { var a = 3, a = x; return a; } - (function(x){ + (function(x) { var a = "GOOD" + x, e = "BAD", k = "!", e = a; console.log(e + k); })("!"), - (function(x){ + (function(x) { var a = "GOOD" + x, e = "BAD" + x, k = "!", e = a; console.log(e + k); })("!"); @@ -1064,10 +1064,10 @@ collapse_vars_repeated: { function f2(x) { return x; } - (function(x){ + (function() { console.log("GOOD!!"); })(), - (function(x){ + (function() { console.log("GOOD!!"); })(); } @@ -2425,7 +2425,7 @@ issue_1858: { }(1)); } expect: { - console.log(function(x) { + console.log(function() { var a = {}, b = a.b = 1; return a.b + b; }()); @@ -2569,12 +2569,12 @@ chained_3: { }(1, 2)); } expect: { - console.log(function(a, b) { + console.log(function(b) { var c = 1; c = b; b++; return c; - }(0, 2)); + }(2)); } expect_stdout: "2" } @@ -2845,7 +2845,7 @@ issue_2187_2: { } expect: { var b = 1; - console.log(function(a) { + console.log(function() { return b-- && ++b; }()); } @@ -2924,7 +2924,7 @@ issue_2203_2: { console.log({ a: "FAIL", b: function() { - return function(c) { + return function() { return (String, (Object, function() { return this; }())).a; @@ -3081,7 +3081,7 @@ issue_2319_1: { }())); } expect: { - console.log(function(a) { + console.log(function() { return !function() { return this; }(); @@ -3129,7 +3129,7 @@ issue_2319_3: { } expect: { "use strict"; - console.log(function(a) { + console.log(function() { return !function() { return this; }(); @@ -3594,7 +3594,7 @@ issue_2425_2: { } expect: { var a = 8; - (function(b, c) { + (function(b) { b.toString(); })(--a, a |= 10); console.log(a); @@ -3616,7 +3616,7 @@ issue_2425_3: { } expect: { var a = 8; - (function(b, b) { + (function() { (a |= 10).toString(); })(--a); console.log(a); @@ -4160,7 +4160,7 @@ issue_2436_13: { var a = "PASS"; (function() { (function(b) { - (function(b) { + (function() { a && (a.null = "FAIL"); })(); })(); @@ -4264,11 +4264,11 @@ issue_2506: { expect: { var c = 0; function f0(bar) { - (function(Infinity_2) { - (function(NaN) { + (function() { + (function() { if (false <= 0/0 & this >> 1 >= 0) c++; - })(0, c++); + })(c++); })(); } f0(false); @@ -4574,12 +4574,12 @@ replace_all_var_scope: { } expect: { var a = 100, b = 10; - (function(c, o) { + (function(c) { switch (~a) { case (b += a): - case o++: + case c++: } - })(--b, a); + })((--b, a)); console.log(a, b); } expect_stdout: "100 109" diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 15420d8a..bc18db9a 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -815,9 +815,9 @@ issue_1583: { } expect: { function m(t) { - (function(e) { + (function() { (function() { - return (function(a) { + return (function() { return function(a) {}; })(); })(); @@ -1329,7 +1329,7 @@ issue_2226_2: { }(1, 2)); } expect: { - console.log(function(a, b) { + console.log(function(a) { return a += 2; }(1)); } @@ -1349,7 +1349,7 @@ issue_2226_3: { }(1, 2)); } expect: { - console.log(function(a, b) { + console.log(function(a) { return a += 2; }(1)); } @@ -1702,11 +1702,11 @@ chained_3: { }(1, 2)); } expect: { - console.log(function(a, b) { + console.log(function(b) { var c = b; b++; return c; - }(0, 2)); + }(2)); } expect_stdout: "2" } diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index 09fdfb2a..fadf717e 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -1674,7 +1674,7 @@ if_increment: { }(0)); } expect: { - console.log(function(a) { + console.log(function() { if (console) return 1; }()); @@ -1696,7 +1696,7 @@ try_increment: { }(0)); } expect: { - console.log(function(a) { + console.log(function() { try { return 1; } catch (e) {} diff --git a/test/compress/functions.js b/test/compress/functions.js index 4fea42dc..1473f74c 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1279,7 +1279,7 @@ issue_2630_3: { expect: { var x = 2, a = 1; (function() { - (function f1(a) { + (function f1() { f2(); --x >= 0 && f1({}); })(a++); diff --git a/test/compress/keep_fargs.js b/test/compress/keep_fargs.js index 74b801d3..440abbd9 100644 --- a/test/compress/keep_fargs.js +++ b/test/compress/keep_fargs.js @@ -873,13 +873,13 @@ iife_func_side_effects: { function z() { console.log("z"); } - (function(a, b) { + (function(b) { return function() { console.log("FAIL"); } + b(); - })(x(), function() { + })((x(), function() { return y(); - }, z()); + }), z()); } expect_stdout: [ "x", diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index a3421c00..9dcf96aa 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -1599,7 +1599,7 @@ defun_label: { } expect: { !function() { - console.log(function(a) { + console.log(function() { L: { if (2) break L; return 1; @@ -1763,13 +1763,13 @@ iife_func_side_effects: { function z() { console.log("z"); } - (function(a, b, c) { + (function(b) { return function() { console.log("FAIL"); } + b(); - })(x(), function() { + })((x(), function() { return y(); - }, z()); + }), z()); } expect_stdout: [ "x", @@ -1830,7 +1830,7 @@ issue_1595_3: { })(2); } expect: { - (function(a) { + (function() { return g(3); })(); } @@ -6602,7 +6602,7 @@ issues_3267_1: { }); } expect: { - !function(i) { + !function() { if (Object()) return console.log("PASS"); throw "FAIL"; |