enhance `side_effects` (#4638)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 10 Feb 2021 10:09:54 +0000 (10:09 +0000)
committerGitHub <noreply@github.com>
Wed, 10 Feb 2021 10:09:54 +0000 (18:09 +0800)
lib/compress.js
test/compress/arrows.js
test/compress/yields.js

index a0e018e..5a2a054 100644 (file)
@@ -6922,9 +6922,29 @@ merge(Compressor.prototype, {
                 exprs = trim(exprs, compressor, first_in_statement, array_spread);
                 return exprs && make_sequence(self, exprs.map(convert_spread));
             }
-            var def;
-            if ((is_arrow(exp) && !exp.value || exp instanceof AST_AsyncFunction || exp instanceof AST_Function)
-                && !(exp.name && (def = exp.name.definition()).references.length > def.replaced)) {
+            if (compressor.option("yields") && is_generator(exp)) {
+                var call = self.clone();
+                call.expression = make_node(AST_Function, exp, exp);
+                call.expression.body = [];
+                var opt = call.transform(compressor);
+                if (opt !== call) return opt.drop_side_effect_free(compressor, first_in_statement);
+            }
+            var drop_body = false;
+            if (compressor.option("arrows") && is_arrow(exp)) {
+                if (exp.value) {
+                    exp.value = exp.value.drop_side_effect_free(compressor);
+                } else {
+                    drop_body = true;
+                }
+            } else if (exp instanceof AST_AsyncFunction || exp instanceof AST_Function) {
+                if (exp.name) {
+                    var def = exp.name.definition();
+                    drop_body = def.references.length == def.replaced;
+                } else {
+                    drop_body = true;
+                }
+            }
+            if (drop_body) {
                 exp.process_expression(false, function(node) {
                     var value = node.value && node.value.drop_side_effect_free(compressor, true);
                     return value ? make_node(AST_SimpleStatement, node, {
index 57d8d39..fbb18b9 100644 (file)
@@ -456,6 +456,7 @@ collapse_property_lambda: {
 
 drop_return: {
     options = {
+        arrows: true,
         side_effects: true,
     }
     input: {
@@ -474,6 +475,21 @@ drop_return: {
     node_version: ">=4"
 }
 
+drop_value: {
+    options = {
+        arrows: true,
+        side_effects: true,
+    }
+    input: {
+        ((a, b) => a + b)(console.log(42));
+    }
+    expect: {
+        ((a, b) => {})(console.log(42));
+    }
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
 reduce_iife_1: {
     options = {
         evaluate: true,
index cd2518d..7ba19bf 100644 (file)
@@ -667,6 +667,44 @@ inline_nested_yield: {
     node_version: ">=4"
 }
 
+drop_body: {
+    options = {
+        side_effects: true,
+        yields: true,
+    }
+    input: {
+        (function*([ , a = console.log("foo") ]) {
+            console.log("bar");
+        })([ console.log("baz") ]);
+    }
+    expect: {
+        [ [ , 0[0] = console.log("foo") ] ] = [ [ console.log("baz") ] ];
+    }
+    expect_stdout: [
+        "baz",
+        "foo",
+    ]
+    node_version: ">=6"
+}
+
+drop_unused_call: {
+    options = {
+        inline: true,
+        side_effects: true,
+        toplevel: true,
+        unused: true,
+        yields: true,
+    }
+    input: {
+        var a = function*(){}(console.log("PASS"));
+    }
+    expect: {
+        console.log("PASS");
+    }
+    expect_stdout: "PASS"
+    node_version: ">=4"
+}
+
 issue_4454_1: {
     rename = false
     options = {