enhance if_return (#5588)

fixes #5587
This commit is contained in:
Alex Lam S.L
2022-07-30 13:53:46 +01:00
committed by GitHub
parent ab5c7e6863
commit 6667440aaf
2 changed files with 47 additions and 3 deletions

View File

@@ -3567,7 +3567,7 @@ Compressor.prototype.compress = function(node) {
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;
// 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))
@@ -3577,7 +3577,7 @@ Compressor.prototype.compress = function(node) {
ret.value = value.clone();
ret.value.condition = make_node(AST_Binary, stat, {
operator: or ? "||" : "&&",
left: stat.condition,
left: or ? stat.condition.negate(compressor) : stat.condition,
right: value.condition,
});
statements.splice(i, 1, ret.transform(compressor));