fix corner cases in `collapse_vars` & `dead_code` (#4285)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 17 Nov 2020 08:23:50 +0000 (08:23 +0000)
committerGitHub <noreply@github.com>
Tue, 17 Nov 2020 08:23:50 +0000 (16:23 +0800)
fixes #4284

lib/compress.js
test/compress/destructured.js

index b8725b3..e4426fa 100644 (file)
@@ -845,7 +845,7 @@ merge(Compressor.prototype, {
             if (init instanceof AST_Definitions) {
                 init.definitions[0].name.match_symbol(function(node) {
                     if (node instanceof AST_SymbolDeclaration) node.definition().fixed = false;
-                });
+                }, true);
             } else if (init instanceof AST_SymbolRef) {
                 init.definition().fixed = false;
             }
@@ -1188,11 +1188,12 @@ merge(Compressor.prototype, {
     AST_Node.DEFMETHOD("match_symbol", function(predicate) {
         return predicate(this);
     });
-    AST_Destructured.DEFMETHOD("match_symbol", function(predicate) {
+    AST_Destructured.DEFMETHOD("match_symbol", function(predicate, allow_computed_keys) {
         var found = false;
         var tw = new TreeWalker(function(node) {
             if (found) return true;
             if (node instanceof AST_DestructuredKeyVal) {
+                if (!allow_computed_keys && node.key instanceof AST_Node) return found = true;
                 node.value.walk(tw);
                 return true;
             }
@@ -7045,7 +7046,7 @@ merge(Compressor.prototype, {
                         name: node,
                         value: make_value(compressor, node)
                     }));
-                });
+                }, true);
             });
             return dropped;
         };
@@ -7107,7 +7108,7 @@ merge(Compressor.prototype, {
                 if (node instanceof AST_SymbolDeclaration) {
                     return !node.fixed_value() || may_overlap(compressor, node.definition());
                 }
-            });
+            }, true);
         }) ? to_var(self) : self;
     }
 
@@ -9061,6 +9062,7 @@ merge(Compressor.prototype, {
                         def.fixed = false;
                         return strip_assignment();
                     } else if (parent instanceof AST_VarDef) {
+                        if (!(parent.name instanceof AST_SymbolDeclaration)) continue;
                         if (parent.name.definition() !== def) continue;
                         if (in_try(level, parent)) break;
                         def.fixed = false;
index 65429b9..1555e3e 100644 (file)
@@ -1341,3 +1341,65 @@ issue_4282: {
     expect_stdout: true
     node_version: ">=6"
 }
+
+issue_4284_1: {
+    options = {
+        dead_code: true,
+    }
+    input: {
+        var a, {
+            0: b,
+        } = a = "foo";
+        console.log(a, b);
+    }
+    expect: {
+        var a, {
+            0: b,
+        } = a = "foo";
+        console.log(a, b);
+    }
+    expect_stdout: "foo f"
+    node_version: ">=6"
+}
+
+issue_4284_2: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var a, {
+            [console.log(a)]: b,
+        } = (a = "PASS", 0);
+        var c = a;
+    }
+    expect: {
+        var a, {
+            [console.log(a)]: b,
+        } = (a = "PASS", 0);
+        var c = a;
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}
+
+issue_4284_3: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var a, b;
+        ({
+            [console.log(a)]: b,
+        } = (a = "PASS", 0));
+        var c = a;
+    }
+    expect: {
+        var a, b;
+        ({
+            [console.log(a)]: b,
+        } = (a = "PASS", 0));
+        var c = a;
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}