fix corner cases in if_return (#5585)

fixes #5583
fixes #5584
fixes #5586
This commit is contained in:
Alex Lam S.L
2022-07-30 09:07:19 +01:00
committed by GitHub
parent 08c386f363
commit ab5c7e6863
3 changed files with 277 additions and 13 deletions

View File

@@ -3547,7 +3547,6 @@ Compressor.prototype.compress = function(node) {
if (stat instanceof AST_If && stat.body instanceof AST_Return) {
var value = stat.body.value;
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) {
changed = true;
@@ -3558,16 +3557,33 @@ Compressor.prototype.compress = function(node) {
statements.splice(j, 1);
continue;
}
//---
// if (foo()) return x; [ return ; ] ---> return foo() ? x : undefined;
if (!stat.alternative && !next && in_lambda && (in_bool || value && multiple_if_returns)) {
changed = true;
stat = stat.clone();
stat.alternative = make_node(AST_Return, stat, { value: null });
statements.splice(i, 1, stat.transform(compressor));
continue;
if (!stat.alternative && !next && in_lambda) {
// if (foo()) return x; [ return ; ] ---> return foo() ? x : undefined;
if (in_bool || value && multiple_if_returns) {
changed = true;
stat = stat.clone();
stat.alternative = make_node(AST_Return, stat, { value: null });
statements.splice(i, 1, stat.transform(compressor));
continue;
}
// 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;
var or;
if (value instanceof AST_Conditional
&& ((or = is_undefined(value.consequent, compressor))
|| is_undefined(value.alternative, compressor))) {
changed = true;
var ret = stat.body.clone();
ret.value = value.clone();
ret.value.condition = make_node(AST_Binary, stat, {
operator: or ? "||" : "&&",
left: stat.condition,
right: value.condition,
});
statements.splice(i, 1, ret.transform(compressor));
continue;
}
}
//---
// if (a) return b; if (c) return d; e; ---> return a ? b : c ? d : void e;
//
// if sequences is not enabled, this can lead to an endless loop (issue #866).
@@ -3647,14 +3663,23 @@ Compressor.prototype.compress = function(node) {
if (!(ab instanceof AST_Return)) return false;
var value = ab.value;
if (value && !is_undefined(value.tail_node())) return false;
if (self instanceof AST_SwitchBranch) merge_jump = 4;
if (!(self instanceof AST_SwitchBranch)) return true;
if (jump instanceof AST_Break) {
merge_jump = 4;
} else if (jump instanceof AST_Exit && !jump.value) {
merge_jump = true;
}
return true;
}
if (!(ab instanceof AST_LoopControl)) return false;
if (jump && self instanceof AST_SwitchBranch) {
if (jump instanceof AST_Exit && jump.value) return false;
if (compressor.loopcontrol_target(jump) instanceof AST_IterationStatement) return false;
merge_jump = true;
}
var lct = compressor.loopcontrol_target(ab);
if (ab instanceof AST_Continue) return match_target(loop_body(lct));
if (lct instanceof AST_IterationStatement) return false;
if (jump) merge_jump = jump.equals(ab);
return match_target(lct);
}