fix corner case in `collapse_vars` (#4431)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 20 Dec 2020 14:54:27 +0000 (14:54 +0000)
committerGitHub <noreply@github.com>
Sun, 20 Dec 2020 14:54:27 +0000 (22:54 +0800)
fixes #4430

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

index 7c1c3b4..d532300 100644 (file)
@@ -1743,7 +1743,11 @@ merge(Compressor.prototype, {
                 if (node instanceof AST_LoopControl) return true;
                 if (node instanceof AST_SymbolRef) {
                     if (node.is_declared(compressor)) {
-                        if (node.fixed_value() || can_drop_symbol(node)) return false;
+                        if (node.fixed_value()) return false;
+                        if (can_drop_symbol(node)) {
+                            return !(parent instanceof AST_PropAccess && parent.expression === node)
+                                && is_arguments(node.definition());
+                        }
                     } else if (parent instanceof AST_Assign && parent.operator == "=" && parent.left === node) {
                         return false;
                     }
@@ -1812,7 +1816,9 @@ merge(Compressor.prototype, {
                     return compressor.option("ie8") && node.name && lvalues.has(node.name.name);
                 }
                 if (node instanceof AST_PropAccess) {
-                    return side_effects || !value_def && node.expression.may_throw_on_access(compressor);
+                    var exp = node.expression;
+                    return side_effects || !value_def && exp.may_throw_on_access(compressor)
+                        || exp instanceof AST_SymbolRef && is_arguments(exp.definition());
                 }
                 if (node instanceof AST_Spread) return true;
                 if (node instanceof AST_SymbolRef) {
index edf4795..17ee8d1 100644 (file)
@@ -8602,3 +8602,63 @@ issue_4248: {
     }
     expect_stdout: "1"
 }
+
+issue_4430_1: {
+    options = {
+        collapse_vars: true,
+        pure_getters: "strict",
+    }
+    input: {
+        function f(a) {
+            switch (a = 1, arguments[0]) {
+              case 1:
+                return "PASS";
+              case 2:
+                return "FAIL";
+            }
+        }
+        console.log(f(2));
+    }
+    expect: {
+        function f(a) {
+            switch (a = 1, arguments[0]) {
+              case 1:
+                return "PASS";
+              case 2:
+                return "FAIL";
+            }
+        }
+        console.log(f(2));
+    }
+    expect_stdout: "PASS"
+}
+
+issue_4430_2: {
+    options = {
+        collapse_vars: true,
+        pure_getters: "strict",
+    }
+    input: {
+        function f(a) {
+            switch (a = 0, arguments[0]) {
+              case 0:
+                return "PASS";
+              case 1:
+                return "FAIL";
+            }
+        }
+        console.log(f(1));
+    }
+    expect: {
+        function f(a) {
+            switch (arguments[a = 0]) {
+              case 0:
+                return "PASS";
+              case 1:
+                return "FAIL";
+            }
+        }
+        console.log(f(1));
+    }
+    expect_stdout: "PASS"
+}