Skip to content

Commit

Permalink
fix corner cases in keep_fargs & unused (#3618)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Dec 1, 2019
1 parent e915832 commit 9a6faf3
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 39 deletions.
16 changes: 14 additions & 2 deletions lib/compress.js
Expand Up @@ -5173,12 +5173,14 @@ merge(Compressor.prototype, {
&& !fn.uses_arguments
&& !fn.pinned()) {
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 = [];
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 (exp === fn) {
if (drop_fargs) {
fn.argnames.splice(i, 1);
self.args.splice(i, 1);
if (node) side_effects.push(node);
Expand Down Expand Up @@ -5207,8 +5209,17 @@ merge(Compressor.prototype, {
}
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;
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 (is_undeclared_ref(exp)) switch (exp.name) {
Expand Down Expand Up @@ -6568,6 +6579,7 @@ merge(Compressor.prototype, {
name.scope = value;
value.name = name;
lambda_def = value.def_function(name);
lambda_def.recursive_refs = def.recursive_refs;
}
value.walk(new TreeWalker(function(node) {
if (!(node instanceof AST_SymbolRef)) return;
Expand Down
43 changes: 21 additions & 22 deletions test/compress/collapse_vars.js
Expand Up @@ -1051,7 +1051,6 @@ collapse_vars_repeated: {
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);
Expand All @@ -1064,10 +1063,10 @@ collapse_vars_repeated: {
function f2(x) {
return x;
}
(function() {
(function(x) {
console.log("GOOD!!");
})(),
(function() {
(function(x) {
console.log("GOOD!!");
})();
}
Expand Down Expand Up @@ -2425,7 +2424,7 @@ issue_1858: {
}(1));
}
expect: {
console.log(function() {
console.log(function(x) {
var a = {}, b = a.b = 1;
return a.b + b;
}());
Expand Down Expand Up @@ -2569,12 +2568,12 @@ chained_3: {
}(1, 2));
}
expect: {
console.log(function(b) {
console.log(function(a, b) {
var c = 1;
c = b;
b++;
return c;
}(2));
}(0, 2));
}
expect_stdout: "2"
}
Expand Down Expand Up @@ -2845,7 +2844,7 @@ issue_2187_2: {
}
expect: {
var b = 1;
console.log(function() {
console.log(function(a) {
return b-- && ++b;
}());
}
Expand Down Expand Up @@ -2924,7 +2923,7 @@ issue_2203_2: {
console.log({
a: "FAIL",
b: function() {
return function() {
return function(c) {
return (String, (Object, function() {
return this;
}())).a;
Expand Down Expand Up @@ -3081,7 +3080,7 @@ issue_2319_1: {
}()));
}
expect: {
console.log(function() {
console.log(function(a) {
return !function() {
return this;
}();
Expand Down Expand Up @@ -3129,7 +3128,7 @@ issue_2319_3: {
}
expect: {
"use strict";
console.log(function() {
console.log(function(a) {
return !function() {
return this;
}();
Expand Down Expand Up @@ -3594,7 +3593,7 @@ issue_2425_2: {
}
expect: {
var a = 8;
(function(b) {
(function(b, c) {
b.toString();
})(--a, a |= 10);
console.log(a);
Expand All @@ -3616,7 +3615,7 @@ issue_2425_3: {
}
expect: {
var a = 8;
(function() {
(function(b, b) {
(a |= 10).toString();
})(--a);
console.log(a);
Expand Down Expand Up @@ -4160,7 +4159,7 @@ issue_2436_13: {
var a = "PASS";
(function() {
(function(b) {
(function() {
(function(b) {
a && (a.null = "FAIL");
})();
})();
Expand Down Expand Up @@ -4264,11 +4263,11 @@ issue_2506: {
expect: {
var c = 0;
function f0(bar) {
(function() {
(function() {
(function(Infinity_2) {
(function(NaN) {
if (false <= 0/0 & this >> 1 >= 0)
c++;
})(c++);
})(0, c++);
})();
}
f0(false);
Expand Down Expand Up @@ -4566,20 +4565,20 @@ replace_all_var_scope: {
var a = 100, b = 10;
(function(r, a) {
switch (~a) {
case (b += a):
case a++:
case (b += a):
case a++:
}
})(--b, a);
console.log(a, b);
}
expect: {
var a = 100, b = 10;
(function(c) {
(function(c, o) {
switch (~a) {
case (b += a):
case c++:
case (b += a):
case o++:
}
})((--b, a));
})(--b, a);
console.log(a, b);
}
expect_stdout: "100 109"
Expand Down
12 changes: 6 additions & 6 deletions test/compress/drop-unused.js
Expand Up @@ -815,9 +815,9 @@ issue_1583: {
}
expect: {
function m(t) {
(function() {
(function(e) {
(function() {
return (function() {
return (function(a) {
return function(a) {};
})();
})();
Expand Down Expand Up @@ -1329,7 +1329,7 @@ issue_2226_2: {
}(1, 2));
}
expect: {
console.log(function(a) {
console.log(function(a, b) {
return a += 2;
}(1));
}
Expand All @@ -1349,7 +1349,7 @@ issue_2226_3: {
}(1, 2));
}
expect: {
console.log(function(a) {
console.log(function(a, b) {
return a += 2;
}(1));
}
Expand Down Expand Up @@ -1702,11 +1702,11 @@ chained_3: {
}(1, 2));
}
expect: {
console.log(function(b) {
console.log(function(a, b) {
var c = b;
b++;
return c;
}(2));
}(0, 2));
}
expect_stdout: "2"
}
Expand Down
4 changes: 2 additions & 2 deletions test/compress/evaluate.js
Expand Up @@ -1674,7 +1674,7 @@ if_increment: {
}(0));
}
expect: {
console.log(function() {
console.log(function(a) {
if (console)
return 1;
}());
Expand All @@ -1696,7 +1696,7 @@ try_increment: {
}(0));
}
expect: {
console.log(function() {
console.log(function(a) {
try {
return 1;
} catch (e) {}
Expand Down
2 changes: 1 addition & 1 deletion test/compress/functions.js
Expand Up @@ -1279,7 +1279,7 @@ issue_2630_3: {
expect: {
var x = 2, a = 1;
(function() {
(function f1() {
(function f1(a) {
f2();
--x >= 0 && f1({});
})(a++);
Expand Down

0 comments on commit 9a6faf3

Please sign in to comment.