Skip to content

Commit a570c00

Browse files
authored
fix corner case in conditionals & if_return (#5685)
fixes #5684
1 parent 3fa2086 commit a570c00

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

Diff for: lib/compress.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -3529,15 +3529,15 @@ Compressor.prototype.compress = function(node) {
35293529
var declare_only, jump, merge_jump;
35303530
var in_iife = in_lambda && parent && parent.TYPE == "Call" && parent.expression === self;
35313531
var chain_if_returns = in_lambda && compressor.option("conditionals") && compressor.option("sequences");
3532+
var drop_return_void = !(in_try && in_try.bfinally && in_async_generator(in_lambda));
35323533
var multiple_if_returns = has_multiple_if_returns(statements);
35333534
for (var i = statements.length; --i >= 0;) {
35343535
var stat = statements[i];
35353536
var j = next_index(i);
35363537
var next = statements[j];
35373538

35383539
if (in_lambda && declare_only && !next && stat instanceof AST_Return
3539-
&& !(self instanceof AST_SwitchBranch)
3540-
&& !(in_try && in_try.bfinally && in_async_generator(in_lambda))) {
3540+
&& drop_return_void && !(self instanceof AST_SwitchBranch)) {
35413541
var body = stat.value;
35423542
if (!body) {
35433543
changed = true;
@@ -3633,7 +3633,7 @@ Compressor.prototype.compress = function(node) {
36333633
var in_bool = stat.body.in_bool || next instanceof AST_Return && next.in_bool;
36343634
// if (foo()) return x; return y; ---> return foo() ? x : y;
36353635
if (!stat.alternative && next instanceof AST_Return
3636-
&& (!value == !next.value || !in_async_generator(in_lambda))) {
3636+
&& (drop_return_void || !value == !next.value)) {
36373637
changed = true;
36383638
stat = stat.clone();
36393639
stat.alternative = make_node(AST_BlockStatement, next, {
@@ -9686,7 +9686,7 @@ Compressor.prototype.compress = function(node) {
96869686
self.body,
96879687
],
96889688
}).optimize(compressor);
9689-
if (cons_value && alt_value || !in_async_generator(compressor.find_parent(AST_Scope))) {
9689+
if (cons_value && alt_value || !keep_return_void()) {
96909690
var exit = make_node(self.body.CTOR, self, {
96919691
value: make_node(AST_Conditional, self, {
96929692
condition: self.condition,
@@ -9762,6 +9762,22 @@ Compressor.prototype.compress = function(node) {
97629762
return node instanceof AST_BlockStatement ? node.body : [ node ];
97639763
}
97649764

9765+
function keep_return_void() {
9766+
var has_finally = false, level = 0, node = compressor.self();
9767+
do {
9768+
if (node instanceof AST_Catch) {
9769+
if (compressor.parent(level).bfinally) has_finally = true;
9770+
level++;
9771+
} else if (node instanceof AST_Finally) {
9772+
level++;
9773+
} else if (node instanceof AST_Scope) {
9774+
return has_finally && in_async_generator(node);
9775+
} else if (node instanceof AST_Try) {
9776+
if (node.bfinally) has_finally = true;
9777+
}
9778+
} while (node = compressor.parent(level++));
9779+
}
9780+
97659781
function last_index(stats) {
97669782
for (var index = stats.length; --index >= 0;) {
97679783
if (!is_declaration(stats[index], true)) break;

Diff for: test/compress/yields.js

+29
Original file line numberDiff line numberDiff line change
@@ -1989,3 +1989,32 @@ issue_5679_6: {
19891989
expect_stdout: "PASS"
19901990
node_version: ">=10"
19911991
}
1992+
1993+
issue_5684: {
1994+
options = {
1995+
conditionals: true,
1996+
if_return: true,
1997+
}
1998+
input: {
1999+
(async function*() {
2000+
switch (42) {
2001+
default:
2002+
if (console.log("PASS"))
2003+
return;
2004+
return null;
2005+
case false:
2006+
}
2007+
})().next();
2008+
}
2009+
expect: {
2010+
(async function*() {
2011+
switch (42) {
2012+
default:
2013+
return console.log("PASS") ? void 0 : null;
2014+
case false:
2015+
}
2016+
})().next();
2017+
}
2018+
expect_stdout: "PASS"
2019+
node_version: ">=10"
2020+
}

0 commit comments

Comments
 (0)