fix corner case in `collapse_vars` (#4048)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 9 Aug 2020 21:48:56 +0000 (22:48 +0100)
committerGitHub <noreply@github.com>
Sun, 9 Aug 2020 21:48:56 +0000 (05:48 +0800)
fixes #4047

lib/compress.js
test/compress/collapse_vars.js

index 9c1fc3a..6f73936 100644 (file)
@@ -1984,9 +1984,12 @@ merge(Compressor.prototype, {
                 if (expr.name instanceof AST_SymbolFunarg) {
                     var index = compressor.self().argnames.indexOf(expr.name);
                     var args = compressor.parent().args;
-                    if (args[index]) args[index] = make_node(AST_Number, args[index], {
-                        value: 0
-                    });
+                    if (args[index]) {
+                        args[index] = make_node(AST_Number, args[index], {
+                            value: 0
+                        });
+                        expr.name.definition().fixed = false;
+                    }
                     return true;
                 }
                 var end = hit_stack.length - 1;
index ff6b9a2..7fc4b76 100644 (file)
@@ -8391,3 +8391,59 @@ issue_4040: {
     }
     expect_stdout: "PASS"
 }
+
+issue_4047_1: {
+    options = {
+        collapse_vars: true,
+        evaluate: true,
+        inline: true,
+        reduce_vars: true,
+        sequences: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var b = 1;
+        console.log(+function(a) {
+            b = a;
+            (a >>= 0) && console.log("PASS");
+        }(--b + (0 !== typeof A)));
+    }
+    expect: {
+        var b = 1;
+        var a;
+        console.log((a = --b + ((a = 0) !== typeof A), +void ((a >>= 0) && console.log("PASS"))));
+    }
+    expect_stdout: [
+        "PASS",
+        "NaN",
+    ]
+}
+
+issue_4047_2: {
+    options = {
+        collapse_vars: true,
+        evaluate: true,
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        sequences: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var b = 1;
+        console.log(+function(a) {
+            b = a;
+            (a >>= 0) && console.log("PASS");
+        }(--b + (0 !== typeof A)));
+    }
+    expect: {
+        var a;
+        console.log((a = +(0 !== typeof A), +void ((a >>= 0) && console.log("PASS"))));
+    }
+    expect_stdout: [
+        "PASS",
+        "NaN",
+    ]
+}