fix corner case in `side_effects` (#4545)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 12 Jan 2021 17:08:16 +0000 (17:08 +0000)
committerGitHub <noreply@github.com>
Tue, 12 Jan 2021 17:08:16 +0000 (01:08 +0800)
fixes #4544

lib/compress.js
test/compress/rests.js

index c9bd182..dde50e0 100644 (file)
@@ -8243,7 +8243,7 @@ merge(Compressor.prototype, {
             if (compressor.option("side_effects")
                 && can_drop
                 && all(fn.body, is_empty)
-                && (fn === exp ? fn_name_unused(fn, compressor) : !has_default && !has_destructured)
+                && (fn === exp ? fn_name_unused(fn, compressor) : !fn.rest && !has_default && !has_destructured)
                 && !(is_arrow(fn) && fn.value)) {
                 return make_sequence(self, convert_args()).optimize(compressor);
             }
@@ -8286,7 +8286,7 @@ merge(Compressor.prototype, {
 
         function convert_args(value) {
             var args = self.args.slice();
-            var destructured = has_default > 1 || has_destructured;
+            var destructured = fn.rest || has_default > 1 || has_destructured;
             if (destructured || has_spread) args = [ make_node(AST_Array, self, { elements: args }) ];
             if (destructured) {
                 var tt = new TreeTransformer(function(node, descend) {
@@ -8332,10 +8332,15 @@ merge(Compressor.prototype, {
                     argname = argname.transform(tt);
                     if (argname) lhs[index] = argname;
                 });
+                var rest = fn.rest && fn.rest.transform(tt);
+                if (rest) lhs.length = fn.argnames.length;
                 fill_holes(fn, lhs);
                 args[0] = make_node(AST_Assign, self, {
                     operator: "=",
-                    left: make_node(AST_DestructuredArray, fn, { elements: lhs }),
+                    left: make_node(AST_DestructuredArray, fn, {
+                        elements: lhs,
+                        rest: rest,
+                    }),
                     right: args[0],
                 });
             } else fn.argnames.forEach(function(argname) {
index ed92770..6bc5b75 100644 (file)
@@ -600,3 +600,49 @@ issue_4538: {
     expect_stdout: "function"
     node_version: ">=6"
 }
+
+issue_4544_1: {
+    options = {
+        keep_fnames: true,
+        side_effects: true,
+    }
+    input: {
+        try {
+            (function f(...[ {} ]) {})();
+        } catch (e) {
+            console.log("PASS");
+        }
+    }
+    expect: {
+        try {
+            [ ...[ {} ] ] = [];
+        } catch (e) {
+            console.log("PASS");
+        }
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}
+
+issue_4544_2: {
+    options = {
+        keep_fnames: true,
+        side_effects: true,
+    }
+    input: {
+        try {
+            (function f(a, ...[ {} ]) {})([]);
+        } catch (e) {
+            console.log("PASS");
+        }
+    }
+    expect: {
+        try {
+            [ , ...[ {} ] ] = [ [] ];
+        } catch (e) {
+            console.log("PASS");
+        }
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}