fix corner case in `collapse_vars` (#4445)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 24 Dec 2020 02:56:22 +0000 (02:56 +0000)
committerGitHub <noreply@github.com>
Thu, 24 Dec 2020 02:56:22 +0000 (10:56 +0800)
fixes #4444

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

index 4be02b5..59aec09 100644 (file)
--- a/README.md
+++ b/README.md
@@ -638,6 +638,8 @@ to be `false` and all symbol names will be omitted.
 
 - `dead_code` (default: `true`) -- remove unreachable code
 
+- `default_values` (default: `true`) -- drop overshadowed default values
+
 - `directives` (default: `true`) -- remove redundant or non-standard directives
 
 - `drop_console` (default: `false`) -- Pass `true` to discard calls to
index 4351768..a611bf4 100644 (file)
@@ -1814,16 +1814,22 @@ merge(Compressor.prototype, {
                     }
                     if (!(fn instanceof AST_Lambda)) return true;
                     if (def && recursive_ref(compressor, def)) return true;
-                    if (!all(fn.argnames, function(argname) {
-                        return !(argname instanceof AST_Destructured);
-                    })) return true;
                     if (fn.collapse_scanning) return false;
                     fn.collapse_scanning = true;
                     var replace = can_replace;
                     can_replace = false;
                     var after = stop_after;
                     var if_hit = stop_if_hit;
-                    if (fn instanceof AST_Arrow && fn.value) {
+                    if (!all(fn.argnames, function(argname) {
+                        if (argname instanceof AST_DefaultValue) {
+                            argname.value.transform(scanner);
+                            if (abort) return false;
+                            argname = argname.name;
+                        }
+                        return !(argname instanceof AST_Destructured);
+                    })) {
+                        abort = true;
+                    } else if (fn instanceof AST_Arrow && fn.value) {
                         fn.value.transform(scanner);
                     } else for (var i = 0; !abort && i < fn.body.length; i++) {
                         var stat = fn.body[i];
index 2e04644..64dad3c 100644 (file)
@@ -950,3 +950,27 @@ mangle_arrow_2_toplevel: {
     expect_stdout: "PASS"
     node_version: ">=6"
 }
+
+issue_4444: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var a = "PASS";
+        console.log(function(b) {
+            b = a;
+            (function(c = b.p) {})();
+            return a;
+        }());
+    }
+    expect: {
+        var a = "PASS";
+        console.log(function(b) {
+            b = a;
+            (function(c = b.p) {})();
+            return a;
+        }());
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}