Skip to content

Commit 8076d66

Browse files
authored
fix corner case in if_return (#5596)
fixes #5595
1 parent 64e3cee commit 8076d66

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

lib/compress.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,15 +3452,16 @@ Compressor.prototype.compress = function(node) {
34523452
var parent = compressor.parent();
34533453
var self = compressor.self();
34543454
var declare_only, jump, merge_jump;
3455-
var in_iife = in_lambda && parent && parent.TYPE == "Call" && parent.expression === self;
3456-
var chain_if_returns = in_lambda && compressor.option("conditionals") && compressor.option("sequences");
3455+
var in_tail = in_lambda && self instanceof AST_Block && self.body === statements;
3456+
var in_iife = in_tail && parent && parent.TYPE == "Call" && parent.expression === self;
3457+
var chain_if_returns = in_tail && compressor.option("conditionals") && compressor.option("sequences");
34573458
var multiple_if_returns = has_multiple_if_returns(statements);
34583459
for (var i = statements.length; --i >= 0;) {
34593460
var stat = statements[i];
34603461
var j = next_index(i);
34613462
var next = statements[j];
34623463

3463-
if (in_lambda && declare_only && !next && stat instanceof AST_Return
3464+
if (in_tail && declare_only && !next && stat instanceof AST_Return
34643465
&& !(self instanceof AST_SwitchBranch)
34653466
&& !(in_try && in_try.bfinally && in_async_generator(in_lambda))) {
34663467
var body = stat.value;
@@ -3563,7 +3564,7 @@ Compressor.prototype.compress = function(node) {
35633564
// if (foo()) return x; [ return ; ] ---> return foo() ? x : undefined;
35643565
// if (foo()) return bar() ? x : void 0; ---> return foo() && bar() ? x : void 0;
35653566
// if (foo()) return bar() ? void 0 : x; ---> return !foo() || bar() ? void 0 : x;
3566-
if (in_lambda && declare_only && !next && !stat.alternative && (in_bool
3567+
if (in_tail && declare_only && !next && !stat.alternative && (in_bool
35673568
|| value && multiple_if_returns
35683569
|| value instanceof AST_Conditional && (is_undefined(value.consequent, compressor)
35693570
|| is_undefined(value.alternative, compressor)))) {
@@ -3644,7 +3645,7 @@ Compressor.prototype.compress = function(node) {
36443645
function can_drop_abort(ab) {
36453646
if (ab instanceof AST_Exit) {
36463647
if (merge_jump = match_return(ab)) return true;
3647-
if (!in_lambda) return false;
3648+
if (!in_tail) return false;
36483649
if (!(ab instanceof AST_Return)) return false;
36493650
var value = ab.value;
36503651
if (value && !is_undefined(value.tail_node())) return false;
@@ -3657,7 +3658,7 @@ Compressor.prototype.compress = function(node) {
36573658
if (!(ab instanceof AST_LoopControl)) return false;
36583659
if (jump && self instanceof AST_SwitchBranch) {
36593660
if (jump instanceof AST_Exit) {
3660-
if (!in_lambda) return false;
3661+
if (!in_tail) return false;
36613662
if (jump.value) return false;
36623663
} else if (compressor.loopcontrol_target(jump) !== parent) {
36633664
return false;
@@ -6162,7 +6163,8 @@ Compressor.prototype.compress = function(node) {
61626163
function opt_arrow(self, compressor) {
61636164
if (!compressor.option("arrows")) return self;
61646165
drop_rest_farg(self, compressor);
6165-
var body = tighten_body(self.value ? [ self.first_statement() ] : self.body, compressor);
6166+
if (self.value) self.body = [ self.first_statement() ];
6167+
var body = tighten_body(self.body, compressor);
61666168
switch (body.length) {
61676169
case 1:
61686170
var stat = body[0];

test/compress/if_return.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,3 +2253,29 @@ issue_5592_2: {
22532253
"baz",
22542254
]
22552255
}
2256+
2257+
issue_5595: {
2258+
options = {
2259+
conditionals: true,
2260+
if_return: true,
2261+
}
2262+
input: {
2263+
function f(a) {
2264+
if (a) {
2265+
var b;
2266+
if (b++)
2267+
return "FAIL";
2268+
} else
2269+
return "PASS";
2270+
}
2271+
console.log(f());
2272+
}
2273+
expect: {
2274+
function f(a) {
2275+
var b;
2276+
return a ? b++ ? "FAIL" : void 0 : "PASS";
2277+
}
2278+
console.log(f());
2279+
}
2280+
expect_stdout: "PASS"
2281+
}

0 commit comments

Comments
 (0)