fix corner cases in `inline` & `side_effects` (#4503)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 4 Jan 2021 02:17:32 +0000 (02:17 +0000)
committerGitHub <noreply@github.com>
Mon, 4 Jan 2021 02:17:32 +0000 (10:17 +0800)
fixes #4502

lib/compress.js
test/compress/default-values.js

index ca2880e..aedbe44 100644 (file)
@@ -8055,6 +8055,7 @@ merge(Compressor.prototype, {
         var stat = is_func && fn.first_statement();
         var has_default = false;
         var can_drop = is_func && all(fn.argnames, function(argname, index) {
+            if (has_default && self.args[index] instanceof AST_Spread) return false;
             if (argname instanceof AST_DefaultValue) {
                 has_default = true;
                 var arg = self.args[index];
@@ -8062,7 +8063,7 @@ merge(Compressor.prototype, {
                 var abort = false;
                 argname.value.walk(new TreeWalker(function(node) {
                     if (abort) return true;
-                    if (node instanceof AST_SymbolRef && fn.find_variable(node.name) === node.definition()) {
+                    if (node instanceof AST_SymbolRef && fn.variables.get(node.name) === node.definition()) {
                         return abort = true;
                     }
                 }));
@@ -8407,29 +8408,47 @@ merge(Compressor.prototype, {
             for (var i = self.args.length; --i >= len;) {
                 expressions.push(self.args[i]);
             }
+            var default_args = [];
             for (i = len; --i >= 0;) {
-                var name = fn.argnames[i];
-                var value = self.args[i];
-                if (name instanceof AST_DefaultValue) {
-                    value = value ? make_sequence(self, [ value, name.value ]) : name.value;
-                    name = name.name;
+                var argname = fn.argnames[i];
+                var name;
+                if (argname instanceof AST_DefaultValue) {
+                    default_args.push(argname);
+                    name = argname.name;
+                } else {
+                    name = argname;
                 }
+                var value = self.args[i];
                 if (name.__unused || scope.var_names()[name.name]) {
                     if (value) expressions.push(value);
                 } else {
                     var symbol = make_node(AST_SymbolVar, name, name);
                     name.definition().orig.push(symbol);
-                    if (!value && in_loop) value = make_node(AST_Undefined, self);
                     if ("__unused" in name) {
                         append_var(decls, expressions, symbol);
                         if (value) expressions.push(value);
                     } else {
+                        if (!value && in_loop && argname === name) value = make_node(AST_Undefined, self);
                         append_var(decls, expressions, symbol, value);
                     }
                 }
             }
             decls.reverse();
             expressions.reverse();
+            for (i = default_args.length; --i >= 0;) {
+                var node = default_args[i];
+                if ("__unused" in node.name) {
+                    expressions.push(node.value);
+                } else {
+                    var sym = make_node(AST_SymbolRef, node.name, node.name);
+                    node.name.definition().references.push(sym);
+                    expressions.push(make_node(AST_Assign, node, {
+                        operator: "=",
+                        left: sym,
+                        right: node.value,
+                    }));
+                }
+            }
         }
 
         function flatten_vars(decls, expressions) {
index cefffc5..7308b30 100644 (file)
@@ -1341,3 +1341,86 @@ issue_4496: {
     ]
     node_version: ">=6"
 }
+
+issue_4502_1: {
+    options = {
+        inline: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            var a = "PASS";
+            (function(b = a++) {
+                var a;
+            })(void 0, console.log(a));
+        })();
+    }
+    expect: {
+        (function() {
+            var a = "PASS";
+            console.log(a),
+            a++,
+            void 0;
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}
+
+issue_4502_2: {
+    options = {
+        inline: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            var a = "PASS";
+            (function(b = a++) {})(void 0, console.log(a));
+        })();
+    }
+    expect: {
+        (function() {
+            var a = "PASS";
+            console.log(a),
+            a++,
+            void 0;
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}
+
+issue_4502_3: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        (function() {
+            var a = "PASS";
+            (function(b = a++) {})(void 0, console.log(a));
+        })();
+    }
+    expect: {
+        (function() {
+            var a = "PASS";
+            console.log(a),
+            a++;
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}
+
+issue_4502_4: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        (function(a, b = console.log("FAIL")) {})(..."" + console.log(42));
+    }
+    expect: {
+        (function(a, b = console.log("FAIL")) {})(..."" + console.log(42));
+    }
+    expect_stdout: "42"
+    node_version: ">=6"
+}