fix corner case in `side_effects` (#4343)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 7 Dec 2020 09:25:04 +0000 (09:25 +0000)
committerGitHub <noreply@github.com>
Mon, 7 Dec 2020 09:25:04 +0000 (17:25 +0800)
fixes #4342

lib/compress.js
test/compress/spread.js

index be38304..21ecc5e 100644 (file)
@@ -6240,18 +6240,20 @@ merge(Compressor.prototype, {
             if (!exp.is_string(compressor)) return node;
             return exp.drop_side_effect_free(compressor, first_in_statement);
         }
+        function convert_spread(node) {
+            return node instanceof AST_Spread ? make_node(AST_Array, node, {
+                elements: [ node ]
+            }) : node;
+        }
         def(AST_Node, return_this);
         def(AST_Accessor, return_null);
         def(AST_Array, function(compressor, first_in_statement) {
             var values = trim(this.elements, compressor, first_in_statement, array_spread);
             if (!values) return null;
-            if (all(values, function(node) {
-                return !(node instanceof AST_Spread);
-            })) return make_sequence(this, values);
-            if (values === this.elements) return this;
-            var node = this.clone();
-            node.elements = values;
-            return node;
+            if (values === this.elements && all(values, function(node) {
+                return node instanceof AST_Spread;
+            })) return this;
+            return make_sequence(this, values.map(convert_spread));
         });
         def(AST_Assign, function(compressor) {
             var left = this.left;
@@ -6304,14 +6306,14 @@ merge(Compressor.prototype, {
             if (self.is_expr_pure(compressor)) {
                 if (self.pure) AST_Node.warn("Dropping __PURE__ call [{file}:{line},{col}]", self.start);
                 var args = trim(self.args, compressor, first_in_statement, array_spread);
-                return args && make_sequence(self, args);
+                return args && make_sequence(self, args.map(convert_spread));
             }
             var exp = self.expression;
             if (self.is_call_pure(compressor)) {
                 var exprs = self.args.slice();
                 exprs.unshift(exp.expression);
                 exprs = trim(exprs, compressor, first_in_statement, array_spread);
-                return exprs && make_sequence(self, exprs);
+                return exprs && make_sequence(self, exprs.map(convert_spread));
             }
             var def;
             if (exp instanceof AST_Function
@@ -6342,7 +6344,7 @@ merge(Compressor.prototype, {
                         var exprs = self.args.slice();
                         exprs.unshift(exp);
                         exprs = trim(exprs, compressor, first_in_statement, array_spread);
-                        return exprs && make_sequence(self, exprs);
+                        return exprs && make_sequence(self, exprs.map(convert_spread));
                     }
                     if (!fn.contains_this()) return make_node(AST_Call, self, self);
                 }
index 21ccbe6..6763d4f 100644 (file)
@@ -425,3 +425,25 @@ issue_4331: {
     expect_stdout: "PASS"
     node_version: ">=8"
 }
+
+issue_4342: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        try {
+            new function() {}(...42);
+        } catch (e) {
+            console.log("PASS");
+        }
+    }
+    expect: {
+        try {
+            [ ...42 ];
+        } catch (e) {
+            console.log("PASS");
+        }
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}