Skip to content

Commit

Permalink
enhance unused (#3617)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Dec 1, 2019
1 parent 0593892 commit e915832
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 44 deletions.
30 changes: 24 additions & 6 deletions lib/compress.js
Expand Up @@ -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) {
Expand Down
40 changes: 20 additions & 20 deletions test/compress/collapse_vars.js
Expand Up @@ -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);
})("!");
Expand All @@ -1064,10 +1064,10 @@ collapse_vars_repeated: {
function f2(x) {
return x;
}
(function(x){
(function() {
console.log("GOOD!!");
})(),
(function(x){
(function() {
console.log("GOOD!!");
})();
}
Expand Down Expand Up @@ -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;
}());
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -2845,7 +2845,7 @@ issue_2187_2: {
}
expect: {
var b = 1;
console.log(function(a) {
console.log(function() {
return b-- && ++b;
}());
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3081,7 +3081,7 @@ issue_2319_1: {
}()));
}
expect: {
console.log(function(a) {
console.log(function() {
return !function() {
return this;
}();
Expand Down Expand Up @@ -3129,7 +3129,7 @@ issue_2319_3: {
}
expect: {
"use strict";
console.log(function(a) {
console.log(function() {
return !function() {
return this;
}();
Expand Down Expand Up @@ -3594,7 +3594,7 @@ issue_2425_2: {
}
expect: {
var a = 8;
(function(b, c) {
(function(b) {
b.toString();
})(--a, a |= 10);
console.log(a);
Expand All @@ -3616,7 +3616,7 @@ issue_2425_3: {
}
expect: {
var a = 8;
(function(b, b) {
(function() {
(a |= 10).toString();
})(--a);
console.log(a);
Expand Down Expand Up @@ -4160,7 +4160,7 @@ issue_2436_13: {
var a = "PASS";
(function() {
(function(b) {
(function(b) {
(function() {
a && (a.null = "FAIL");
})();
})();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"
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(e) {
(function() {
(function() {
return (function(a) {
return (function() {
return function(a) {};
})();
})();
Expand Down Expand Up @@ -1329,7 +1329,7 @@ issue_2226_2: {
}(1, 2));
}
expect: {
console.log(function(a, b) {
console.log(function(a) {
return a += 2;
}(1));
}
Expand All @@ -1349,7 +1349,7 @@ issue_2226_3: {
}(1, 2));
}
expect: {
console.log(function(a, b) {
console.log(function(a) {
return a += 2;
}(1));
}
Expand Down Expand Up @@ -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"
}
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(a) {
console.log(function() {
if (console)
return 1;
}());
Expand All @@ -1696,7 +1696,7 @@ try_increment: {
}(0));
}
expect: {
console.log(function(a) {
console.log(function() {
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(a) {
(function f1() {
f2();
--x >= 0 && f1({});
})(a++);
Expand Down
6 changes: 3 additions & 3 deletions test/compress/keep_fargs.js
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions test/compress/reduce_vars.js
Expand Up @@ -1599,7 +1599,7 @@ defun_label: {
}
expect: {
!function() {
console.log(function(a) {
console.log(function() {
L: {
if (2) break L;
return 1;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1830,7 +1830,7 @@ issue_1595_3: {
})(2);
}
expect: {
(function(a) {
(function() {
return g(3);
})();
}
Expand Down Expand Up @@ -6602,7 +6602,7 @@ issues_3267_1: {
});
}
expect: {
!function(i) {
!function() {
if (Object())
return console.log("PASS");
throw "FAIL";
Expand Down

0 comments on commit e915832

Please sign in to comment.