Skip to content

Commit

Permalink
fix corner case in awaits (#5828)
Browse files Browse the repository at this point in the history
fixes #5791
  • Loading branch information
alexlamsl authored Jun 6, 2024
1 parent c21a8ee commit 0991077
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
63 changes: 63 additions & 0 deletions test/compress/awaits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 0991077

Please sign in to comment.