fix corner case in `unused` (#5225)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 19 Dec 2021 18:25:38 +0000 (18:25 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Dec 2021 18:25:38 +0000 (02:25 +0800)
fixes #5224

lib/compress.js
test/compress/drop-unused.js

index cf8c7eb..949abac 100644 (file)
@@ -8076,7 +8076,11 @@ Compressor.prototype.compress = function(node) {
                         exprs = trim(exprs, compressor, first_in_statement, array_spread);
                         return exprs && make_sequence(self, exprs.map(convert_spread));
                     }
-                    if (!fn.contains_this()) self = make_node(AST_Call, self, self);
+                    if (!fn.contains_this()) {
+                        self = make_node(AST_Call, self, self);
+                        self.expression = self.expression.clone();
+                        self.args = self.args.slice();
+                    }
                 }
             }
             self.call_only = true;
@@ -9565,7 +9569,7 @@ Compressor.prototype.compress = function(node) {
                             expression: exp.expression,
                             property: "call",
                         }),
-                        args: args
+                        args: args,
                     }).optimize(compressor);
                 }
                 break;
@@ -9579,11 +9583,11 @@ Compressor.prototype.compress = function(node) {
                         self.args[0],
                         make_node(AST_Call, self, {
                             expression: exp.expression,
-                            args: self.args.slice(1)
-                        })
+                            args: self.args.slice(1),
+                        }),
                     ]) : make_node(AST_Call, self, {
                         expression: exp.expression,
-                        args: []
+                        args: [],
                     })).optimize(compressor);
                 }
                 break;
index be28ebe..2c4b735 100644 (file)
@@ -3516,3 +3516,47 @@ issue_5079: {
     }
     expect_stdout: "PASS"
 }
+
+issue_5224: {
+    options = {
+        evaluate: true,
+        keep_fargs: false,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            try {
+                var b = function() {
+                    var a = "FAIL 1";
+                    null && a;
+                    a = console.log(a);
+                }(new function(c, d) {
+                    console.log(d);
+                    a;
+                }("FAIL 2", Infinity));
+            } finally {
+                return f;
+            }
+        }
+        f();
+    }
+    expect: {
+        (function f() {
+            try {
+                (function() {
+                    var a = "FAIL 1";
+                    null;
+                    a = console.log(a);
+                })(function() {
+                    console.log(1 / 0);
+                    a;
+                }());
+            } finally {
+                return f;
+            }
+        })();
+    }
+    expect_stdout: "Infinity"
+}