fix corner cases in keep_fargs & unused (#3618)

This commit is contained in:
Alex Lam S.L 2019-12-02 06:43:54 +08:00 committed by GitHub
parent e915832a36
commit 9a6faf365b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 320 additions and 39 deletions

View File

@ -5173,12 +5173,14 @@ merge(Compressor.prototype, {
&& !fn.uses_arguments && !fn.uses_arguments
&& !fn.pinned()) { && !fn.pinned()) {
var pos = 0, last = 0; var pos = 0, last = 0;
var drop_fargs = exp === fn && compressor.drop_fargs(fn, self)
&& (!fn.name || !fn.name.definition().recursive_refs);
var side_effects = []; 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 (exp === fn) { if (drop_fargs) {
fn.argnames.splice(i, 1); fn.argnames.splice(i, 1);
self.args.splice(i, 1); self.args.splice(i, 1);
if (node) side_effects.push(node); if (node) side_effects.push(node);
@ -5207,8 +5209,17 @@ merge(Compressor.prototype, {
} }
last = pos; last = pos;
} }
if (drop_fargs) for (; i < fn.argnames.length; i++) {
if (fn.argnames[i].__unused) fn.argnames.splice(i--, 1);
}
self.args.length = last; self.args.length = last;
if (side_effects.length) self.args.push(make_sequence(self, side_effects)); if (side_effects.length) {
var arg = make_sequence(self, side_effects);
self.args.push(self.args.length < fn.argnames.length ? make_node(AST_UnaryPrefix, self, {
operator: "void",
expression: arg
}) : arg);
}
} }
if (compressor.option("unsafe")) { if (compressor.option("unsafe")) {
if (is_undeclared_ref(exp)) switch (exp.name) { if (is_undeclared_ref(exp)) switch (exp.name) {
@ -6568,6 +6579,7 @@ merge(Compressor.prototype, {
name.scope = value; name.scope = value;
value.name = name; value.name = name;
lambda_def = value.def_function(name); lambda_def = value.def_function(name);
lambda_def.recursive_refs = def.recursive_refs;
} }
value.walk(new TreeWalker(function(node) { value.walk(new TreeWalker(function(node) {
if (!(node instanceof AST_SymbolRef)) return; if (!(node instanceof AST_SymbolRef)) return;

View File

@ -1051,7 +1051,6 @@ collapse_vars_repeated: {
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 +1063,10 @@ collapse_vars_repeated: {
function f2(x) { function f2(x) {
return x; return x;
} }
(function() { (function(x) {
console.log("GOOD!!"); console.log("GOOD!!");
})(), })(),
(function() { (function(x) {
console.log("GOOD!!"); console.log("GOOD!!");
})(); })();
} }
@ -2425,7 +2424,7 @@ issue_1858: {
}(1)); }(1));
} }
expect: { expect: {
console.log(function() { console.log(function(x) {
var a = {}, b = a.b = 1; var a = {}, b = a.b = 1;
return a.b + b; return a.b + b;
}()); }());
@ -2569,12 +2568,12 @@ chained_3: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(b) { console.log(function(a, b) {
var c = 1; var c = 1;
c = b; c = b;
b++; b++;
return c; return c;
}(2)); }(0, 2));
} }
expect_stdout: "2" expect_stdout: "2"
} }
@ -2845,7 +2844,7 @@ issue_2187_2: {
} }
expect: { expect: {
var b = 1; var b = 1;
console.log(function() { console.log(function(a) {
return b-- && ++b; return b-- && ++b;
}()); }());
} }
@ -2924,7 +2923,7 @@ issue_2203_2: {
console.log({ console.log({
a: "FAIL", a: "FAIL",
b: function() { b: function() {
return function() { return function(c) {
return (String, (Object, function() { return (String, (Object, function() {
return this; return this;
}())).a; }())).a;
@ -3081,7 +3080,7 @@ issue_2319_1: {
}())); }()));
} }
expect: { expect: {
console.log(function() { console.log(function(a) {
return !function() { return !function() {
return this; return this;
}(); }();
@ -3129,7 +3128,7 @@ issue_2319_3: {
} }
expect: { expect: {
"use strict"; "use strict";
console.log(function() { console.log(function(a) {
return !function() { return !function() {
return this; return this;
}(); }();
@ -3594,7 +3593,7 @@ issue_2425_2: {
} }
expect: { expect: {
var a = 8; var a = 8;
(function(b) { (function(b, c) {
b.toString(); b.toString();
})(--a, a |= 10); })(--a, a |= 10);
console.log(a); console.log(a);
@ -3616,7 +3615,7 @@ issue_2425_3: {
} }
expect: { expect: {
var a = 8; var a = 8;
(function() { (function(b, b) {
(a |= 10).toString(); (a |= 10).toString();
})(--a); })(--a);
console.log(a); console.log(a);
@ -4160,7 +4159,7 @@ issue_2436_13: {
var a = "PASS"; var a = "PASS";
(function() { (function() {
(function(b) { (function(b) {
(function() { (function(b) {
a && (a.null = "FAIL"); a && (a.null = "FAIL");
})(); })();
})(); })();
@ -4264,11 +4263,11 @@ issue_2506: {
expect: { expect: {
var c = 0; var c = 0;
function f0(bar) { function f0(bar) {
(function() { (function(Infinity_2) {
(function() { (function(NaN) {
if (false <= 0/0 & this >> 1 >= 0) if (false <= 0/0 & this >> 1 >= 0)
c++; c++;
})(c++); })(0, c++);
})(); })();
} }
f0(false); f0(false);
@ -4566,20 +4565,20 @@ replace_all_var_scope: {
var a = 100, b = 10; var a = 100, b = 10;
(function(r, a) { (function(r, a) {
switch (~a) { switch (~a) {
case (b += a): case (b += a):
case a++: case a++:
} }
})(--b, a); })(--b, a);
console.log(a, b); console.log(a, b);
} }
expect: { expect: {
var a = 100, b = 10; var a = 100, b = 10;
(function(c) { (function(c, o) {
switch (~a) { switch (~a) {
case (b += a): case (b += a):
case c++: case o++:
} }
})((--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() { (function(e) {
(function() { (function() {
return (function() { return (function(a) {
return function(a) {}; return function(a) {};
})(); })();
})(); })();
@ -1329,7 +1329,7 @@ issue_2226_2: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(a) { console.log(function(a, b) {
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) { console.log(function(a, b) {
return a += 2; return a += 2;
}(1)); }(1));
} }
@ -1702,11 +1702,11 @@ chained_3: {
}(1, 2)); }(1, 2));
} }
expect: { expect: {
console.log(function(b) { console.log(function(a, b) {
var c = b; var c = b;
b++; b++;
return c; return c;
}(2)); }(0, 2));
} }
expect_stdout: "2" expect_stdout: "2"
} }

View File

@ -1674,7 +1674,7 @@ if_increment: {
}(0)); }(0));
} }
expect: { expect: {
console.log(function() { console.log(function(a) {
if (console) if (console)
return 1; return 1;
}()); }());
@ -1696,7 +1696,7 @@ try_increment: {
}(0)); }(0));
} }
expect: { expect: {
console.log(function() { console.log(function(a) {
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() { (function f1(a) {
f2(); f2();
--x >= 0 && f1({}); --x >= 0 && f1({});
})(a++); })(a++);

View File

@ -1155,3 +1155,273 @@ issue_3423_2: {
} }
expect_stdout: "1" expect_stdout: "1"
} }
collapse_vars_repeated: {
options = {
booleans: true,
collapse_vars: true,
comparisons: true,
conditionals: true,
dead_code: true,
evaluate: true,
hoist_funs: true,
if_return: true,
join_vars: true,
keep_fargs: "strict",
loops: true,
properties: true,
reduce_funcs: true,
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
function f1() {
var dummy = 3, a = 5, unused = 2, a = 1, a = 3;
return -a;
}
function f2(x) {
var a = 3, a = x;
return a;
}
(function(x) {
var a = "GOOD" + x, e = "BAD", k = "!", e = a;
console.log(e + k);
})("!"),
(function(x) {
var a = "GOOD" + x, e = "BAD" + x, k = "!", e = a;
console.log(e + k);
})("!");
}
expect: {
function f1() {
return -3;
}
function f2(x) {
return x;
}
(function() {
console.log("GOOD!!");
})(),
(function() {
console.log("GOOD!!");
})();
}
expect_stdout: true
}
chained_3: {
options = {
collapse_vars: true,
keep_fargs: "strict",
unused: true,
}
input: {
console.log(function(a, b) {
var c = a, c = b;
b++;
return c;
}(1, 2));
}
expect: {
console.log(function(b) {
var c = 1;
c = b;
b++;
return c;
}(2));
}
expect_stdout: "2"
}
replace_all_var_scope: {
rename = true
options = {
collapse_vars: true,
keep_fargs: "strict",
unused: true,
}
mangle = {}
input: {
var a = 100, b = 10;
(function(r, a) {
switch (~a) {
case (b += a):
case a++:
}
})(--b, a);
console.log(a, b);
}
expect: {
var a = 100, b = 10;
(function(c) {
switch (~a) {
case (b += a):
case c++:
}
})((--b, a));
console.log(a, b);
}
expect_stdout: "100 109"
}
issue_1583: {
options = {
keep_fargs: "strict",
passes: 2,
reduce_funcs: true,
reduce_vars: true,
unused: true,
}
input: {
function m(t) {
(function(e) {
t = e();
})(function() {
return (function(a) {
return a;
})(function(a) {});
});
}
}
expect: {
function m(t) {
(function() {
(function() {
return (function() {
return function(a) {};
})();
})();
})();
}
}
}
issues_3267_1: {
options = {
collapse_vars: true,
conditionals: true,
dead_code: true,
evaluate: true,
inline: true,
keep_fargs: "strict",
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
(function(x) {
x();
})(function() {
(function(i) {
if (i)
return console.log("PASS");
throw "FAIL";
})(Object());
});
}
expect: {
!function() {
if (Object())
return console.log("PASS");
throw "FAIL";
}();
}
expect_stdout: "PASS"
}
trailing_argument_side_effects: {
options = {
keep_fargs: "strict",
unused: true,
}
input: {
function f() {
return "FAIL";
}
console.log(function(a, b) {
return b || "PASS";
}(f()));
}
expect: {
function f() {
return "FAIL";
}
console.log(function(b) {
return b || "PASS";
}(void f()));
}
expect_stdout: "PASS"
}
recursive_iife_1: {
options = {
keep_fargs: "strict",
reduce_vars: true,
unused: true,
}
input: {
console.log(function f(a, b) {
return b || f("FAIL", "PASS");
}());
}
expect: {
console.log(function f(a, b) {
return b || f("FAIL", "PASS");
}());
}
expect_stdout: "PASS"
}
recursive_iife_2: {
options = {
keep_fargs: "strict",
reduce_vars: true,
unused: true,
}
input: {
console.log(function f(a, b) {
return b || f("FAIL", "PASS");
}(null, 0));
}
expect: {
console.log(function f(a, b) {
return b || f("FAIL", "PASS");
}(0, 0));
}
expect_stdout: "PASS"
}
recursive_iife_3: {
options = {
keep_fargs: false,
reduce_vars: true,
unused: true,
}
input: {
var a = 1, c = "PASS";
(function() {
function f(b, d, e) {
a-- && f(null, 42, 0);
e && (c = "FAIL");
d && d.p;
}
var a_1 = f();
})();
console.log(c);
}
expect: {
var a = 1, c = "PASS";
(function() {
(function f(b, d, e) {
a-- && f(null, 42, 0);
e && (c = "FAIL");
d && d.p;
})();
})();
console.log(c);
}
expect_stdout: "PASS"
}

View File

@ -1599,7 +1599,7 @@ defun_label: {
} }
expect: { expect: {
!function() { !function() {
console.log(function() { console.log(function(a) {
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(b) { (function(a, b, c) {
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() { (function(a) {
return g(3); return g(3);
})(); })();
} }
@ -6602,7 +6602,7 @@ issues_3267_1: {
}); });
} }
expect: { expect: {
!function() { !function(i) {
if (Object()) if (Object())
return console.log("PASS"); return console.log("PASS");
throw "FAIL"; throw "FAIL";