enhance unused (#3617)

This commit is contained in:
Alex Lam S.L
2019-12-01 18:10:37 +08:00
committed by GitHub
parent 0593892d6e
commit e915832a36
7 changed files with 62 additions and 44 deletions

View File

@@ -5173,24 +5173,42 @@ merge(Compressor.prototype, {
&& !fn.uses_arguments && !fn.uses_arguments
&& !fn.pinned()) { && !fn.pinned()) {
var pos = 0, last = 0; var pos = 0, last = 0;
var side_effects = [];
for (var i = 0; i < self.args.length; i++) { for (var i = 0; i < self.args.length; i++) {
var trim = i >= fn.argnames.length; var trim = i >= fn.argnames.length;
if (trim || fn.argnames[i].__unused) { if (trim || fn.argnames[i].__unused) {
var node = self.args[i].drop_side_effect_free(compressor); var node = self.args[i].drop_side_effect_free(compressor);
if (node) { if (exp === fn) {
self.args[pos++] = node; 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) { } else if (!trim) {
self.args[pos++] = make_node(AST_Number, self.args[i], { if (side_effects.length) {
node = make_sequence(self, side_effects);
side_effects = [];
} else {
node = make_node(AST_Number, self.args[i], {
value: 0 value: 0
}); });
}
self.args[pos++] = node;
continue; continue;
} }
} else { } 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; last = pos;
} }
self.args.length = last; self.args.length = last;
if (side_effects.length) self.args.push(make_sequence(self, side_effects));
} }
if (compressor.option("unsafe")) { if (compressor.option("unsafe")) {
if (is_undeclared_ref(exp)) switch (exp.name) { if (is_undeclared_ref(exp)) switch (exp.name) {

View File

@@ -1047,12 +1047,12 @@ collapse_vars_repeated: {
var a = 3, a = x; var a = 3, a = x;
return a; return a;
} }
(function(x){ (function(x) {
var a = "GOOD" + x, e = "BAD", k = "!", e = a; var a = "GOOD" + x, e = "BAD", k = "!", e = a;
console.log(e + k); console.log(e + k);
})("!"), })("!"),
(function(x){ (function(x) {
var a = "GOOD" + x, e = "BAD" + x, k = "!", e = a; var a = "GOOD" + x, e = "BAD" + x, k = "!", e = a;
console.log(e + k); console.log(e + k);
})("!"); })("!");
@@ -1064,10 +1064,10 @@ collapse_vars_repeated: {
function f2(x) { function f2(x) {
return x; return x;
} }
(function(x){ (function() {
console.log("GOOD!!"); console.log("GOOD!!");
})(), })(),
(function(x){ (function() {
console.log("GOOD!!"); console.log("GOOD!!");
})(); })();
} }
@@ -2425,7 +2425,7 @@ issue_1858: {
}(1)); }(1));
} }
expect: { expect: {
console.log(function(x) { console.log(function() {
var a = {}, b = a.b = 1; var a = {}, b = a.b = 1;
return a.b + b; return a.b + b;
}()); }());
@@ -2569,12 +2569,12 @@ chained_3: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(a, b) { console.log(function(b) {
var c = 1; var c = 1;
c = b; c = b;
b++; b++;
return c; return c;
}(0, 2)); }(2));
} }
expect_stdout: "2" expect_stdout: "2"
} }
@@ -2845,7 +2845,7 @@ issue_2187_2: {
} }
expect: { expect: {
var b = 1; var b = 1;
console.log(function(a) { console.log(function() {
return b-- && ++b; return b-- && ++b;
}()); }());
} }
@@ -2924,7 +2924,7 @@ issue_2203_2: {
console.log({ console.log({
a: "FAIL", a: "FAIL",
b: function() { b: function() {
return function(c) { return function() {
return (String, (Object, function() { return (String, (Object, function() {
return this; return this;
}())).a; }())).a;
@@ -3081,7 +3081,7 @@ issue_2319_1: {
}())); }()));
} }
expect: { expect: {
console.log(function(a) { console.log(function() {
return !function() { return !function() {
return this; return this;
}(); }();
@@ -3129,7 +3129,7 @@ issue_2319_3: {
} }
expect: { expect: {
"use strict"; "use strict";
console.log(function(a) { console.log(function() {
return !function() { return !function() {
return this; return this;
}(); }();
@@ -3594,7 +3594,7 @@ issue_2425_2: {
} }
expect: { expect: {
var a = 8; var a = 8;
(function(b, c) { (function(b) {
b.toString(); b.toString();
})(--a, a |= 10); })(--a, a |= 10);
console.log(a); console.log(a);
@@ -3616,7 +3616,7 @@ issue_2425_3: {
} }
expect: { expect: {
var a = 8; var a = 8;
(function(b, b) { (function() {
(a |= 10).toString(); (a |= 10).toString();
})(--a); })(--a);
console.log(a); console.log(a);
@@ -4160,7 +4160,7 @@ issue_2436_13: {
var a = "PASS"; var a = "PASS";
(function() { (function() {
(function(b) { (function(b) {
(function(b) { (function() {
a && (a.null = "FAIL"); a && (a.null = "FAIL");
})(); })();
})(); })();
@@ -4264,11 +4264,11 @@ issue_2506: {
expect: { expect: {
var c = 0; var c = 0;
function f0(bar) { function f0(bar) {
(function(Infinity_2) { (function() {
(function(NaN) { (function() {
if (false <= 0/0 & this >> 1 >= 0) if (false <= 0/0 & this >> 1 >= 0)
c++; c++;
})(0, c++); })(c++);
})(); })();
} }
f0(false); f0(false);
@@ -4574,12 +4574,12 @@ replace_all_var_scope: {
} }
expect: { expect: {
var a = 100, b = 10; var a = 100, b = 10;
(function(c, o) { (function(c) {
switch (~a) { switch (~a) {
case (b += a): case (b += a):
case o++: case c++:
} }
})(--b, a); })((--b, a));
console.log(a, b); console.log(a, b);
} }
expect_stdout: "100 109" expect_stdout: "100 109"

View File

@@ -815,9 +815,9 @@ issue_1583: {
} }
expect: { expect: {
function m(t) { function m(t) {
(function(e) {
(function() { (function() {
return (function(a) { (function() {
return (function() {
return function(a) {}; return function(a) {};
})(); })();
})(); })();
@@ -1329,7 +1329,7 @@ issue_2226_2: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(a, b) { console.log(function(a) {
return a += 2; return a += 2;
}(1)); }(1));
} }
@@ -1349,7 +1349,7 @@ issue_2226_3: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(a, b) { console.log(function(a) {
return a += 2; return a += 2;
}(1)); }(1));
} }
@@ -1702,11 +1702,11 @@ chained_3: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(a, b) { console.log(function(b) {
var c = b; var c = b;
b++; b++;
return c; return c;
}(0, 2)); }(2));
} }
expect_stdout: "2" expect_stdout: "2"
} }

View File

@@ -1674,7 +1674,7 @@ if_increment: {
}(0)); }(0));
} }
expect: { expect: {
console.log(function(a) { console.log(function() {
if (console) if (console)
return 1; return 1;
}()); }());
@@ -1696,7 +1696,7 @@ try_increment: {
}(0)); }(0));
} }
expect: { expect: {
console.log(function(a) { console.log(function() {
try { try {
return 1; return 1;
} catch (e) {} } catch (e) {}

View File

@@ -1279,7 +1279,7 @@ issue_2630_3: {
expect: { expect: {
var x = 2, a = 1; var x = 2, a = 1;
(function() { (function() {
(function f1(a) { (function f1() {
f2(); f2();
--x >= 0 && f1({}); --x >= 0 && f1({});
})(a++); })(a++);

View File

@@ -873,13 +873,13 @@ iife_func_side_effects: {
function z() { function z() {
console.log("z"); console.log("z");
} }
(function(a, b) { (function(b) {
return function() { return function() {
console.log("FAIL"); console.log("FAIL");
} + b(); } + b();
})(x(), function() { })((x(), function() {
return y(); return y();
}, z()); }), z());
} }
expect_stdout: [ expect_stdout: [
"x", "x",

View File

@@ -1599,7 +1599,7 @@ defun_label: {
} }
expect: { expect: {
!function() { !function() {
console.log(function(a) { console.log(function() {
L: { L: {
if (2) break L; if (2) break L;
return 1; return 1;
@@ -1763,13 +1763,13 @@ iife_func_side_effects: {
function z() { function z() {
console.log("z"); console.log("z");
} }
(function(a, b, c) { (function(b) {
return function() { return function() {
console.log("FAIL"); console.log("FAIL");
} + b(); } + b();
})(x(), function() { })((x(), function() {
return y(); return y();
}, z()); }), z());
} }
expect_stdout: [ expect_stdout: [
"x", "x",
@@ -1830,7 +1830,7 @@ issue_1595_3: {
})(2); })(2);
} }
expect: { expect: {
(function(a) { (function() {
return g(3); return g(3);
})(); })();
} }
@@ -6602,7 +6602,7 @@ issues_3267_1: {
}); });
} }
expect: { expect: {
!function(i) { !function() {
if (Object()) if (Object())
return console.log("PASS"); return console.log("PASS");
throw "FAIL"; throw "FAIL";