enhance `side_effects` (#4675)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 22 Feb 2021 07:44:16 +0000 (07:44 +0000)
committerGitHub <noreply@github.com>
Mon, 22 Feb 2021 07:44:16 +0000 (15:44 +0800)
lib/compress.js
test/compress/side_effects.js

index 0a89de7..c645101 100644 (file)
@@ -8516,6 +8516,8 @@ merge(Compressor.prototype, {
                             }
                         });
                         return node;
+                    } else if (!node.has_side_effects(compressor)) {
+                        self.drop_side_effect_free = return_null;
                     }
                 }
                 var arg_used, insert, in_loop, scope;
index 994e884..fd6cabc 100644 (file)
@@ -348,8 +348,6 @@ issue_3983_1: {
     }
     expect: {
         var a = "PASS";
-        g();
-        function g() {}
         console.log(a);
     }
     expect_stdout: "PASS"
@@ -537,3 +535,26 @@ issue_4668: {
     }
     expect_stdout: "undefined"
 }
+
+drop_side_effect_free_call: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+    }
+    input: {
+        function f(a) {
+            return "PA" + a;
+        }
+        f(42);
+        console.log(f("SS"));
+    }
+    expect: {
+        function f(a) {
+            return "PA" + a;
+        }
+        console.log(f("SS"));
+    }
+    expect_stdout: "PASS"
+}