Skip to content

Commit 6667440

Browse files
authored
enhance if_return (#5588)
fixes #5587
1 parent ab5c7e6 commit 6667440

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/compress.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3567,7 +3567,7 @@ Compressor.prototype.compress = function(node) {
35673567
continue;
35683568
}
35693569
// if (foo()) return bar() ? x : void 0; ---> return foo() && bar() ? x : void 0;
3570-
// if (foo()) return bar() ? void 0 : x; ---> return foo() || bar() ? void 0 : x;
3570+
// if (foo()) return bar() ? void 0 : x; ---> return !foo() || bar() ? void 0 : x;
35713571
var or;
35723572
if (value instanceof AST_Conditional
35733573
&& ((or = is_undefined(value.consequent, compressor))
@@ -3577,7 +3577,7 @@ Compressor.prototype.compress = function(node) {
35773577
ret.value = value.clone();
35783578
ret.value.condition = make_node(AST_Binary, stat, {
35793579
operator: or ? "||" : "&&",
3580-
left: stat.condition,
3580+
left: or ? stat.condition.negate(compressor) : stat.condition,
35813581
right: value.condition,
35823582
});
35833583
statements.splice(i, 1, ret.transform(compressor));

test/compress/if_return.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ if_return_cond_void_2: {
254254
}
255255
expect: {
256256
function f(a) {
257-
return a || console.log("foo") ? void 0 : console.log("bar");
257+
return !a || console.log("foo") ? void 0 : console.log("bar");
258258
}
259259
f();
260260
f(42);
@@ -1827,3 +1827,47 @@ issue_5586: {
18271827
"baz",
18281828
]
18291829
}
1830+
1831+
issue_5587_1: {
1832+
options = {
1833+
if_return: true,
1834+
}
1835+
input: {
1836+
function f(a) {
1837+
if (console)
1838+
return a ? void 0 : console.log("PASS");
1839+
}
1840+
f();
1841+
f(42);
1842+
}
1843+
expect: {
1844+
function f(a) {
1845+
return !console || a ? void 0 : console.log("PASS");
1846+
}
1847+
f();
1848+
f(42);
1849+
}
1850+
expect_stdout: "PASS"
1851+
}
1852+
1853+
issue_5587_2: {
1854+
options = {
1855+
if_return: true,
1856+
}
1857+
input: {
1858+
function f(a) {
1859+
if (console)
1860+
return a ? console.log("PASS") : void 0;
1861+
}
1862+
f();
1863+
f(42);
1864+
}
1865+
expect: {
1866+
function f(a) {
1867+
return console && a ? console.log("PASS") : void 0;
1868+
}
1869+
f();
1870+
f(42);
1871+
}
1872+
expect_stdout: "PASS"
1873+
}

0 commit comments

Comments
 (0)