Skip to content

Commit

Permalink
fix corner case in conditionals & if_return (#5685)
Browse files Browse the repository at this point in the history
fixes #5684
  • Loading branch information
alexlamsl authored Sep 27, 2022
1 parent 3fa2086 commit a570c00
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3529,15 +3529,15 @@ 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];
var j = next_index(i);
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;
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit a570c00

Please sign in to comment.