From 09910771a1e8e2531c8dac09e274e345d3feb0e4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 6 Jun 2024 04:51:02 +0300 Subject: [PATCH] fix corner case in `awaits` (#5828) fixes #5791 --- lib/compress.js | 20 +++++++++++-- test/compress/awaits.js | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index d1b0fab9141..664be40da2b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3964,12 +3964,19 @@ Compressor.prototype.compress = function(node) { for (var index = statements.length; --index >= 0;) { var stat = statements[index]; if (!(stat instanceof AST_SimpleStatement)) break; - var node = stat.body; + var node = stat.body.tail_node(); if (!(node instanceof AST_Await)) break; var exp = node.expression; if (!needs_enqueuing(compressor, exp)) break; changed = true; exp = exp.drop_side_effect_free(compressor, true); + if (stat.body instanceof AST_Sequence) { + var expressions = stat.body.expressions.slice(); + expressions.pop(); + if (exp) expressions.push(exp); + stat.body = make_sequence(stat.body, expressions); + break; + } if (exp) { stat.body = exp; break; @@ -11752,7 +11759,16 @@ Compressor.prototype.compress = function(node) { return !lazy_op[node.operator] || needs_enqueuing(compressor, node.left) && needs_enqueuing(compressor, node.right); } - if (node instanceof AST_Call) return is_async(node.expression); + if (node instanceof AST_Call) { + if (!is_async(node.expression)) return false; + var has_await = false; + walk_body(node.expression, new TreeWalker(function(expr) { + if (has_await) return true; + if (expr instanceof AST_Await) return has_await = true; + if (expr !== node && expr instanceof AST_Scope) return true; + })); + return !has_await; + } if (node instanceof AST_Conditional) { return needs_enqueuing(compressor, node.consequent) && needs_enqueuing(compressor, node.alternative); } diff --git a/test/compress/awaits.js b/test/compress/awaits.js index 42e86c8033a..45d5dd3eca1 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -3642,3 +3642,66 @@ issue_5692_2: { ] node_version: ">=8" } + +issue_5791: { + options = { + awaits: true, + reduce_funcs: true, + reduce_vars: true, + side_effects: true, + unused: true, + } + input: { + (async function() { + async function f() { + try { + await { + then(resolve) { + setImmediate(() => { + console.log("foo"); + resolve(); + }); + }, + }; + } catch (e) { + console.log("FAIL", e); + } + } + async function g() { + try { + await f(); + } catch (e) {} + } + await g(); + console.log("bar"); + })(); + } + expect: { + (async function() { + await async function() { + try { + await async function() { + try { + await { + then(resolve) { + setImmediate(() => { + console.log("foo"); + resolve(); + }); + }, + }; + } catch (e) { + console.log("FAIL", e); + } + }(); + } catch (e) {} + }(); + console.log("bar"); + })(); + } + expect_stdout: [ + "foo", + "bar", + ] + node_version: ">=8" +}