Skip to content

Commit

Permalink
fix corner cases in if_return (#5590)
Browse files Browse the repository at this point in the history
fixes #5589
fixes #5591
fixes #5592
  • Loading branch information
alexlamsl authored Jul 30, 2022
1 parent 6667440 commit e39f33e
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 11 deletions.
16 changes: 9 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3664,17 +3664,19 @@ Compressor.prototype.compress = function(node) {
var value = ab.value;
if (value && !is_undefined(value.tail_node())) return false;
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;
}
if (!jump) return false;
if (jump instanceof AST_Exit && jump.value) return false;
merge_jump = 4;
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;
if (jump instanceof AST_Exit) {
if (!in_lambda) return false;
if (jump.value) return false;
} else if (compressor.loopcontrol_target(jump) !== parent) {
return false;
}
merge_jump = true;
}
var lct = compressor.loopcontrol_target(ab);
Expand Down
48 changes: 48 additions & 0 deletions test/compress/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -2096,3 +2096,51 @@ issue_5580_2: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_5591: {
options = {
dead_code: true,
if_return: true,
}
input: {
"use strict";
function f(a) {
switch (console.log("foo")) {
case console.log("bar"):
if (console.log("baz"))
return;
else {
const a = 42;
return;
}
break;
case null:
FAIL;
}
}
f();
}
expect: {
"use strict";
function f(a) {
switch (console.log("foo")) {
case console.log("bar"):
if (console.log("baz"))
return;
else {
const a = 42;
return;
}
case null:
FAIL;
}
}
f();
}
expect_stdout: [
"foo",
"bar",
"baz",
]
node_version: ">=4"
}
256 changes: 252 additions & 4 deletions test/compress/if_return.js
Original file line number Diff line number Diff line change
Expand Up @@ -1759,8 +1759,7 @@ issue_5584_1: {
function f(a) {
switch (a) {
case 42:
if (console.log("PASS"))
return FAIL;
return console.log("PASS") ? FAIL : void 0;
}
}
f(42);
Expand All @@ -1775,7 +1774,7 @@ issue_5584_2: {
input: {
function f(a) {
switch (a) {
case console.log("PASS"):
case console.log("PASS"):
if (console)
break;
return FAIL;
Expand All @@ -1786,7 +1785,7 @@ issue_5584_2: {
expect: {
function f(a) {
switch (a) {
case console.log("PASS"):
case console.log("PASS"):
if (console)
break;
return FAIL;
Expand All @@ -1797,6 +1796,76 @@ issue_5584_2: {
expect_stdout: "PASS"
}

issue_5584_3: {
options = {
if_return: true,
}
input: {
function f() {
switch (console.log("foo")) {
case console.log("bar"):
if (console)
break;
return;
}
console.log("baz");
}
f();
}
expect: {
function f() {
switch (console.log("foo")) {
case console.log("bar"):
if (console)
break;
return;
}
console.log("baz");
}
f();
}
expect_stdout: [
"foo",
"bar",
"baz",
]
}

issue_5584_4: {
options = {
if_return: true,
}
input: {
function f(a) {
switch (console.log("foo")) {
case console.log("bar"):
if (a)
return;
break;
}
console.log("baz");
}
f();
}
expect: {
function f(a) {
switch (console.log("foo")) {
case console.log("bar"):
if (a)
return;
break;
}
console.log("baz");
}
f();
}
expect_stdout: [
"foo",
"bar",
"baz",
]
}

issue_5586: {
options = {
if_return: true,
Expand Down Expand Up @@ -1871,3 +1940,182 @@ issue_5587_2: {
}
expect_stdout: "PASS"
}

issue_5589_1: {
options = {
if_return: true,
}
input: {
function f(a) {
switch (a) {
case 42:
if (!console.log("PASS"))
return;
return 0;
break;
case null:
FAIL;
}
}
f(42);
}
expect: {
function f(a) {
switch (a) {
case 42:
if (console.log("PASS"))
return 0;
break;
case null:
FAIL;
}
}
f(42);
}
expect_stdout: "PASS"
}

issue_5589_2: {
options = {
if_return: true,
}
input: {
function f(a) {
switch (console.log("foo")) {
case console.log("bar"):
if (a)
return void console.log("baz");
return;
}
}
f();
f(42);
}
expect: {
function f(a) {
switch (console.log("foo")) {
case console.log("bar"):
if (a)
void console.log("baz");
return;
}
}
f();
f(42);
}
expect_stdout: [
"foo",
"bar",
"foo",
"bar",
"baz",
]
}

issue_5589_3: {
options = {
if_return: true,
}
input: {
function f(a) {
do {
switch (console.log("foo")) {
case console.log("bar"):
if (a)
return void console.log("baz");
continue;
}
} while (console.log("moo"));
}
f();
f(42);
}
expect: {
function f(a) {
do {
switch (console.log("foo")) {
case console.log("bar"):
if (a)
return void console.log("baz");
continue;
}
} while (console.log("moo"));
}
f();
f(42);
}
expect_stdout: [
"foo",
"bar",
"moo",
"foo",
"bar",
"baz",
]
}

issue_5592_1: {
options = {
if_return: true,
}
input: {
L: {
do {
switch (console.log("foo")) {
case console.log("bar"):
if (console)
break;
break L;
}
} while (console.log("baz"));
}
}
expect: {
L: do {
switch (console.log("foo")) {
case console.log("bar"):
if (console)
break;
break L;
}
} while (console.log("baz"));
}
expect_stdout: [
"foo",
"bar",
"baz",
]
}

issue_5592_2: {
options = {
if_return: true,
}
input: {
L: {
do {
switch (console.log("foo")) {
case console.log("bar"):
if (!console)
break L;
break;
}
} while (console.log("baz"));
}
}
expect: {
L: do {
switch (console.log("foo")) {
case console.log("bar"):
if (console)
break;
break L;
}
} while (console.log("baz"));
}
expect_stdout: [
"foo",
"bar",
"baz",
]
}
Loading

0 comments on commit e39f33e

Please sign in to comment.