enhance `unused` (#3617)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 1 Dec 2019 10:10:37 +0000 (18:10 +0800)
committerGitHub <noreply@github.com>
Sun, 1 Dec 2019 10:10:37 +0000 (18:10 +0800)
lib/compress.js
test/compress/collapse_vars.js
test/compress/drop-unused.js
test/compress/evaluate.js
test/compress/functions.js
test/compress/keep_fargs.js
test/compress/reduce_vars.js

index 6a76fc3..09019a7 100644 (file)
@@ -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) {
index 75dddb0..eef6377 100644 (file)
@@ -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"
index 15420d8..bc18db9 100644 (file)
@@ -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"
 }
index 09fdfb2..fadf717 100644 (file)
@@ -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) {}
index 4fea42d..1473f74 100644 (file)
@@ -1279,7 +1279,7 @@ issue_2630_3: {
     expect: {
         var x = 2, a = 1;
         (function() {
-            (function f1(a) {
+            (function f1() {
                 f2();
                 --x >= 0 && f1({});
             })(a++);
index 74b801d..440abbd 100644 (file)
@@ -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",
index a3421c0..9dcf96a 100644 (file)
@@ -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";