diff --git a/lib/compress.js b/lib/compress.js index 16761265..c04e5a48 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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)); diff --git a/test/compress/if_return.js b/test/compress/if_return.js index 2b41e951..0c549eac 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -254,7 +254,7 @@ if_return_cond_void_2: { } expect: { function f(a) { - return a || console.log("foo") ? void 0 : console.log("bar"); + return !a || console.log("foo") ? void 0 : console.log("bar"); } f(); f(42); @@ -1827,3 +1827,47 @@ issue_5586: { "baz", ] } + +issue_5587_1: { + options = { + if_return: true, + } + input: { + function f(a) { + if (console) + return a ? void 0 : console.log("PASS"); + } + f(); + f(42); + } + expect: { + function f(a) { + return !console || a ? void 0 : console.log("PASS"); + } + f(); + f(42); + } + expect_stdout: "PASS" +} + +issue_5587_2: { + options = { + if_return: true, + } + input: { + function f(a) { + if (console) + return a ? console.log("PASS") : void 0; + } + f(); + f(42); + } + expect: { + function f(a) { + return console && a ? console.log("PASS") : void 0; + } + f(); + f(42); + } + expect_stdout: "PASS" +}