fix corner case in awaits (#5828)

fixes #5791
This commit is contained in:
Alex Lam S.L
2024-06-06 04:51:02 +03:00
committed by GitHub
parent c21a8eee7b
commit 09910771a1
2 changed files with 81 additions and 2 deletions

View File

@@ -3964,12 +3964,19 @@ Compressor.prototype.compress = function(node) {
for (var index = statements.length; --index >= 0;) { for (var index = statements.length; --index >= 0;) {
var stat = statements[index]; var stat = statements[index];
if (!(stat instanceof AST_SimpleStatement)) break; if (!(stat instanceof AST_SimpleStatement)) break;
var node = stat.body; var node = stat.body.tail_node();
if (!(node instanceof AST_Await)) break; if (!(node instanceof AST_Await)) break;
var exp = node.expression; var exp = node.expression;
if (!needs_enqueuing(compressor, exp)) break; if (!needs_enqueuing(compressor, exp)) break;
changed = true; changed = true;
exp = exp.drop_side_effect_free(compressor, 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) { if (exp) {
stat.body = exp; stat.body = exp;
break; break;
@@ -11752,7 +11759,16 @@ Compressor.prototype.compress = function(node) {
return !lazy_op[node.operator] return !lazy_op[node.operator]
|| needs_enqueuing(compressor, node.left) && needs_enqueuing(compressor, node.right); || 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) { if (node instanceof AST_Conditional) {
return needs_enqueuing(compressor, node.consequent) && needs_enqueuing(compressor, node.alternative); return needs_enqueuing(compressor, node.consequent) && needs_enqueuing(compressor, node.alternative);
} }

View File

@@ -3642,3 +3642,66 @@ issue_5692_2: {
] ]
node_version: ">=8" 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"
}