fix corner case in `awaits` (#4740)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 5 Mar 2021 20:25:44 +0000 (20:25 +0000)
committerGitHub <noreply@github.com>
Fri, 5 Mar 2021 20:25:44 +0000 (04:25 +0800)
fixes #4738

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

index 9d390d0..20558ba 100644 (file)
@@ -7205,10 +7205,10 @@ merge(Compressor.prototype, {
         });
         def(AST_Await, function(compressor) {
             if (!compressor.option("awaits")) return this;
-            var exp = this.expression.drop_side_effect_free(compressor);
-            if (exp === this.expression) return this;
+            var exp = this.expression;
+            if (!is_primitive(compressor, exp)) return this;
             var node = this.clone();
-            node.expression = exp || make_node(AST_Number, this, { value: 0 });
+            node.expression = exp.drop_side_effect_free(compressor) || make_node(AST_Number, this, { value: 0 });
             return node;
         });
         def(AST_Binary, function(compressor, first_in_statement) {
@@ -9555,6 +9555,25 @@ merge(Compressor.prototype, {
             || node instanceof AST_Object;
     }
 
+    function is_primitive(compressor, node) {
+        if (node.is_constant()) return true;
+        if (node instanceof AST_Assign) return node.operator != "=" || is_primitive(compressor, node.right);
+        if (node instanceof AST_Binary) {
+            return !lazy_op[node.operator]
+                || is_primitive(compressor, node.left) && is_primitive(compressor, node.right);
+        }
+        if (node instanceof AST_Conditional) {
+            return is_primitive(compressor, node.consequent) && is_primitive(compressor, node.alternative);
+        }
+        if (node instanceof AST_Sequence) return is_primitive(compressor, node.tail_node());
+        if (node instanceof AST_SymbolRef) {
+            var fixed = node.fixed_value();
+            return fixed && is_primitive(compressor, fixed);
+        }
+        if (node instanceof AST_Template) return !node.tag || is_raw_tag(compressor, node.tag);
+        if (node instanceof AST_Unary) return true;
+    }
+
     function repeatable(compressor, node) {
         if (node instanceof AST_Dot) return repeatable(compressor, node.expression);
         if (node instanceof AST_Sub) {
index 974134c..0534d18 100644 (file)
@@ -23,16 +23,34 @@ async_label: {
 }
 
 await_await: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
     input: {
         (async function() {
-            console.log("PASS");
-            await await 42;
+            await await {
+                then(resolve) {
+                    resolve({
+                        then() {
+                            console.log("PASS");
+                        },
+                    });
+                },
+            };
         })();
     }
     expect: {
         (async function() {
-            console.log("PASS");
-            await await 42;
+            await {
+                then(resolve) {
+                    resolve({
+                        then() {
+                            console.log("PASS");
+                        },
+                    });
+                },
+            };
         })();
     }
     expect_stdout: "PASS"
@@ -1280,3 +1298,84 @@ issue_4717: {
     expect_stdout: "PASS"
     node_version: ">=8"
 }
+
+issue_4738_1: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
+    input: {
+        (async function() {
+            await {
+                then() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect: {
+        (async function() {
+            await {
+                then() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}
+
+issue_4738_2: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
+    input: {
+        (async function() {
+            await {
+                get then() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect: {
+        (async function() {
+            await {
+                get then() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}
+
+issue_4738_3: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
+    input: {
+        (async function() {
+            await {
+                then: function() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect: {
+        (async function() {
+            await {
+                then: function() {
+                    console.log("PASS");
+                },
+            };
+        })();
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}