fix corner case in `reduce_vars` (#3895)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 13 May 2020 15:44:54 +0000 (16:44 +0100)
committerGitHub <noreply@github.com>
Wed, 13 May 2020 15:44:54 +0000 (23:44 +0800)
fixes #3894

lib/compress.js
test/compress/reduce_vars.js

index f7c0af3..8cf7262 100644 (file)
@@ -741,6 +741,11 @@ merge(Compressor.prototype, {
                             if (j < 0) return value;
                             return iife.args[j] || make_node(AST_Undefined, iife);
                         };
+                        d.fixed.reduce_arg = function() {
+                            var j = fn.argnames.indexOf(arg);
+                            if (j < 0 || j >= iife.args.length) return;
+                            iife.args[j] = make_node(AST_Number, iife.args[j], { value: 0 });
+                        };
                         tw.loop_ids[d.id] = tw.in_loop;
                         mark(tw, d, true);
                     } else {
@@ -7566,6 +7571,7 @@ merge(Compressor.prototype, {
                     }));
                 } else {
                     value = fixed.optimize(compressor);
+                    if (def.fixed.reduce_arg) def.fixed.reduce_arg();
                 }
                 def.replaced++;
                 return value;
index 262a77f..08accc4 100644 (file)
@@ -7068,3 +7068,30 @@ issue_3880: {
     }
     expect_stdout: "PASS"
 }
+
+issue_3894: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        function log(msg) {
+            console.log(msg ? "FAIL" : "PASS");
+        }
+        var a;
+        (function(b) {
+            a = 0,
+            log(b);
+        })(-0);
+    }
+    expect: {
+        function log(msg) {
+            console.log(msg ? "FAIL" : "PASS");
+        }
+        var a;
+        void log(-(a = 0));
+    }
+    expect_stdout: "PASS"
+}