fix corner case in `side_effects` (#4765)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 12 Mar 2021 18:40:28 +0000 (18:40 +0000)
committerGitHub <noreply@github.com>
Fri, 12 Mar 2021 18:40:28 +0000 (02:40 +0800)
fixes #4764

lib/compress.js
test/compress/awaits.js

index 807872f..09ae2ff 100644 (file)
@@ -7266,10 +7266,10 @@ merge(Compressor.prototype, {
             }
             var drop_body = false;
             if (compressor.option("arrows") && is_arrow(exp)) {
-                if (exp.value) {
-                    exp.value = exp.value.drop_side_effect_free(compressor);
-                } else {
+                if (!exp.value) {
                     drop_body = true;
+                } else if (!is_async(exp) || is_primitive(compressor, exp.value)) {
+                    exp.value = exp.value.drop_side_effect_free(compressor);
                 }
             } else if (exp instanceof AST_AsyncFunction || exp instanceof AST_Function) {
                 if (exp.name) {
@@ -7280,14 +7280,22 @@ merge(Compressor.prototype, {
                 }
             }
             if (drop_body) {
+                var async = is_async(exp);
                 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, {
-                        body: value
-                    }) : make_node(AST_EmptyStatement, node);
+                    var value = node.value;
+                    if (value) {
+                        if (async && !is_primitive(compressor, value)) return node;
+                        value = value.drop_side_effect_free(compressor, true);
+                    }
+                    if (!value) return make_node(AST_EmptyStatement, node);
+                    return make_node(AST_SimpleStatement, node, { body: value });
                 });
                 scan_local_returns(exp, function(node) {
-                    if (node.value) node.value = node.value.drop_side_effect_free(compressor);
+                    var value = node.value;
+                    if (value) {
+                        if (async && !is_primitive(compressor, value)) return;
+                        node.value = value.drop_side_effect_free(compressor);
+                    }
                 });
                 // always shallow clone to ensure stripping of negated IIFEs
                 self = self.clone();
index 937fcb5..be3cc5a 100644 (file)
@@ -564,7 +564,7 @@ drop_return: {
     input: {
         (async function(a) {
             while (!console);
-            return console.log(a);
+            return !console.log(a);
         })(42);
     }
     expect: {
@@ -1408,3 +1408,78 @@ issue_4747: {
     expect_stdout: "PASS"
     node_version: ">=8"
 }
+
+issue_4764_1: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        (async function() {
+            return {
+                then() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect: {
+        (async function() {
+            return {
+                then() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}
+
+issue_4764_2: {
+    options = {
+        arrows: true,
+        side_effects: true,
+    }
+    input: {
+        (async () => ({
+            get then() {
+                console.log("PASS");
+            },
+        }))();
+    }
+    expect: {
+        (async () => ({
+            get then() {
+                console.log("PASS");
+            },
+        }))();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}
+
+issue_4764_3: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        (async function(o) {
+            return o;
+        })({
+            then() {
+                console.log("PASS");
+            },
+        });
+    }
+    expect: {
+        (async function(o) {
+            return o;
+        })({
+            then() {
+                console.log("PASS");
+            },
+        });
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}