diff --git a/lib/compress.js b/lib/compress.js index c04e5a48..e4ba897f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); diff --git a/test/compress/const.js b/test/compress/const.js index b39b9474..f275ef5c 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -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" +} diff --git a/test/compress/if_return.js b/test/compress/if_return.js index 0c549eac..477e4367 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -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); @@ -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; @@ -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; @@ -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, @@ -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", + ] +} diff --git a/test/compress/let.js b/test/compress/let.js index 43280ace..c070f6b7 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -2122,3 +2122,51 @@ issue_5476: { expect_stdout: "undefined" 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 { + let a; + 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 { + let a; + return; + } + case null: + FAIL; + } + } + f(); + } + expect_stdout: [ + "foo", + "bar", + "baz", + ] + node_version: ">=4" +}