Skip to content

Commit

Permalink
fix corner case in if_return (#5596)
Browse files Browse the repository at this point in the history
fixes #5595
  • Loading branch information
alexlamsl authored Aug 3, 2022
1 parent 64e3cee commit 8076d66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3452,15 +3452,16 @@ Compressor.prototype.compress = function(node) {
var parent = compressor.parent();
var self = compressor.self();
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 in_tail = in_lambda && self instanceof AST_Block && self.body === statements;
var in_iife = in_tail && parent && parent.TYPE == "Call" && parent.expression === self;
var chain_if_returns = in_tail && compressor.option("conditionals") && compressor.option("sequences");
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
if (in_tail && declare_only && !next && stat instanceof AST_Return
&& !(self instanceof AST_SwitchBranch)
&& !(in_try && in_try.bfinally && in_async_generator(in_lambda))) {
var body = stat.value;
Expand Down Expand Up @@ -3563,7 +3564,7 @@ Compressor.prototype.compress = function(node) {
// if (foo()) return x; [ return ; ] ---> return foo() ? x : undefined;
// if (foo()) return bar() ? x : void 0; ---> return foo() && bar() ? x : void 0;
// if (foo()) return bar() ? void 0 : x; ---> return !foo() || bar() ? void 0 : x;
if (in_lambda && declare_only && !next && !stat.alternative && (in_bool
if (in_tail && declare_only && !next && !stat.alternative && (in_bool
|| value && multiple_if_returns
|| value instanceof AST_Conditional && (is_undefined(value.consequent, compressor)
|| is_undefined(value.alternative, compressor)))) {
Expand Down Expand Up @@ -3644,7 +3645,7 @@ Compressor.prototype.compress = function(node) {
function can_drop_abort(ab) {
if (ab instanceof AST_Exit) {
if (merge_jump = match_return(ab)) return true;
if (!in_lambda) return false;
if (!in_tail) return false;
if (!(ab instanceof AST_Return)) return false;
var value = ab.value;
if (value && !is_undefined(value.tail_node())) return false;
Expand All @@ -3657,7 +3658,7 @@ Compressor.prototype.compress = function(node) {
if (!(ab instanceof AST_LoopControl)) return false;
if (jump && self instanceof AST_SwitchBranch) {
if (jump instanceof AST_Exit) {
if (!in_lambda) return false;
if (!in_tail) return false;
if (jump.value) return false;
} else if (compressor.loopcontrol_target(jump) !== parent) {
return false;
Expand Down Expand Up @@ -6162,7 +6163,8 @@ Compressor.prototype.compress = function(node) {
function opt_arrow(self, compressor) {
if (!compressor.option("arrows")) return self;
drop_rest_farg(self, compressor);
var body = tighten_body(self.value ? [ self.first_statement() ] : self.body, compressor);
if (self.value) self.body = [ self.first_statement() ];
var body = tighten_body(self.body, compressor);
switch (body.length) {
case 1:
var stat = body[0];
Expand Down
26 changes: 26 additions & 0 deletions test/compress/if_return.js
Original file line number Diff line number Diff line change
Expand Up @@ -2253,3 +2253,29 @@ issue_5592_2: {
"baz",
]
}

issue_5595: {
options = {
conditionals: true,
if_return: true,
}
input: {
function f(a) {
if (a) {
var b;
if (b++)
return "FAIL";
} else
return "PASS";
}
console.log(f());
}
expect: {
function f(a) {
var b;
return a ? b++ ? "FAIL" : void 0 : "PASS";
}
console.log(f());
}
expect_stdout: "PASS"
}

0 comments on commit 8076d66

Please sign in to comment.