fix corner case in conditionals & if_return (#5685)

fixes #5684
This commit is contained in:
Alex Lam S.L
2022-09-27 17:04:32 +01:00
committed by GitHub
parent 3fa2086681
commit a570c00251
2 changed files with 49 additions and 4 deletions

View File

@@ -3529,6 +3529,7 @@ Compressor.prototype.compress = function(node) {
var declare_only, jump, merge_jump;
var in_iife = in_lambda && parent && parent.TYPE == "Call" && parent.expression === self;
var chain_if_returns = in_lambda && compressor.option("conditionals") && compressor.option("sequences");
var drop_return_void = !(in_try && in_try.bfinally && in_async_generator(in_lambda));
var multiple_if_returns = has_multiple_if_returns(statements);
for (var i = statements.length; --i >= 0;) {
var stat = statements[i];
@@ -3536,8 +3537,7 @@ Compressor.prototype.compress = function(node) {
var next = statements[j];
if (in_lambda && declare_only && !next && stat instanceof AST_Return
&& !(self instanceof AST_SwitchBranch)
&& !(in_try && in_try.bfinally && in_async_generator(in_lambda))) {
&& drop_return_void && !(self instanceof AST_SwitchBranch)) {
var body = stat.value;
if (!body) {
changed = true;
@@ -3633,7 +3633,7 @@ Compressor.prototype.compress = function(node) {
var in_bool = stat.body.in_bool || next instanceof AST_Return && next.in_bool;
// if (foo()) return x; return y; ---> return foo() ? x : y;
if (!stat.alternative && next instanceof AST_Return
&& (!value == !next.value || !in_async_generator(in_lambda))) {
&& (drop_return_void || !value == !next.value)) {
changed = true;
stat = stat.clone();
stat.alternative = make_node(AST_BlockStatement, next, {
@@ -9686,7 +9686,7 @@ Compressor.prototype.compress = function(node) {
self.body,
],
}).optimize(compressor);
if (cons_value && alt_value || !in_async_generator(compressor.find_parent(AST_Scope))) {
if (cons_value && alt_value || !keep_return_void()) {
var exit = make_node(self.body.CTOR, self, {
value: make_node(AST_Conditional, self, {
condition: self.condition,
@@ -9762,6 +9762,22 @@ Compressor.prototype.compress = function(node) {
return node instanceof AST_BlockStatement ? node.body : [ node ];
}
function keep_return_void() {
var has_finally = false, level = 0, node = compressor.self();
do {
if (node instanceof AST_Catch) {
if (compressor.parent(level).bfinally) has_finally = true;
level++;
} else if (node instanceof AST_Finally) {
level++;
} else if (node instanceof AST_Scope) {
return has_finally && in_async_generator(node);
} else if (node instanceof AST_Try) {
if (node.bfinally) has_finally = true;
}
} while (node = compressor.parent(level++));
}
function last_index(stats) {
for (var index = stats.length; --index >= 0;) {
if (!is_declaration(stats[index], true)) break;

View File

@@ -1989,3 +1989,32 @@ issue_5679_6: {
expect_stdout: "PASS"
node_version: ">=10"
}
issue_5684: {
options = {
conditionals: true,
if_return: true,
}
input: {
(async function*() {
switch (42) {
default:
if (console.log("PASS"))
return;
return null;
case false:
}
})().next();
}
expect: {
(async function*() {
switch (42) {
default:
return console.log("PASS") ? void 0 : null;
case false:
}
})().next();
}
expect_stdout: "PASS"
node_version: ">=10"
}