fix corner case in `awaits` (#5158)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 29 Oct 2021 15:33:10 +0000 (23:33 +0800)
committerGitHub <noreply@github.com>
Fri, 29 Oct 2021 15:33:10 +0000 (23:33 +0800)
fixes #5157

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

index 7d1e6fd..225fc74 100644 (file)
@@ -10334,7 +10334,7 @@ merge(Compressor.prototype, {
                     do {
                         node = parent;
                         parent = compressor.parent(level++);
-                        if (parent instanceof AST_Try && parent.bfinally && parent.bfinally !== node) {
+                        if (parent instanceof AST_Try && member(node, parent.body)) {
                             drop = false;
                             break;
                         }
index 0a7ae64..a3b55b1 100644 (file)
@@ -2046,3 +2046,100 @@ issue_5070: {
     expect_stdout: "PASS"
     node_version: ">=10"
 }
+
+issue_5157_async_function: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
+    input: {
+        async function f() {
+            throw "FAIL";
+        }
+        (async function() {
+            try {
+                return await f();
+            } catch (e) {
+                return "PASS";
+            }
+        })().then(console.log);
+    }
+    expect: {
+        async function f() {
+            throw "FAIL";
+        }
+        (async function() {
+            try {
+                return await f();
+            } catch (e) {
+                return "PASS";
+            }
+        })().then(console.log);
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}
+
+issue_5157_async_iife: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
+    input: {
+        (async function() {
+            try {
+                return await async function() {
+                    throw "FAIL";
+                }();
+            } catch (e) {
+                return "PASS";
+            }
+        })().then(console.log);
+    }
+    expect: {
+        (async function() {
+            try {
+                return await async function() {
+                    throw "FAIL";
+                }();
+            } catch (e) {
+                return "PASS";
+            }
+        })().then(console.log);
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}
+
+issue_5157_promise: {
+    options = {
+        awaits: true,
+        side_effects: true,
+    }
+    input: {
+        var p = new Promise(function(resolve, reject) {
+            reject("FAIL");
+        });
+        (async function() {
+            try {
+                return await p;
+            } catch (e) {
+                return "PASS";
+            }
+        })().then(console.log);
+    }
+    expect: {
+        var p = new Promise(function(resolve, reject) {
+            reject("FAIL");
+        });
+        (async function() {
+            try {
+                return await p;
+            } catch (e) {
+                return "PASS";
+            }
+        })().then(console.log);
+    }
+    expect_stdout: "PASS"
+    node_version: ">=8"
+}