diff --git a/lib/compress.js b/lib/compress.js index 99d9b9c1..399bfdcb 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3331,14 +3331,11 @@ merge(Compressor.prototype, { var stat = statements[i]; if (stat instanceof AST_LoopControl) { var lct = compressor.loopcontrol_target(stat); - if (stat instanceof AST_Break - && !(lct instanceof AST_IterationStatement) - && loop_body(lct) === self - || stat instanceof AST_Continue - && loop_body(lct) === self) { - if (stat.label) remove(stat.label.thedef.references, stat); - } else { + if (loop_body(lct) !== self + || stat instanceof AST_Break && lct instanceof AST_IterationStatement) { statements[n++] = stat; + } else if (stat.label) { + remove(stat.label.thedef.references, stat); } } else { statements[n++] = stat; @@ -5485,6 +5482,21 @@ merge(Compressor.prototype, { return compressor.option("unused") && self.label.references.length == 0 ? self.body : self; }); + OPT(AST_LoopControl, function(self, compressor) { + if (!compressor.option("dead_code")) return self; + var label = self.label; + if (label) { + var lct = compressor.loopcontrol_target(self); + self.label = null; + if (compressor.loopcontrol_target(self) === lct) { + remove(label.thedef.references, self); + } else { + self.label = label; + } + } + return self; + }); + OPT(AST_Block, function(self, compressor) { self.body = tighten_body(self.body, compressor); return self; diff --git a/test/compress/labels.js b/test/compress/labels.js index 6b7c5a3d..4bf32648 100644 --- a/test/compress/labels.js +++ b/test/compress/labels.js @@ -83,8 +83,9 @@ labels_5: { conditionals: true, dead_code: true, if_return: true, + unused: true, } - // should keep the break-s in the following + // should keep `break`s below input: { while (foo) { if (bar) break; @@ -100,8 +101,8 @@ labels_5: { if (bar) break; console.log("foo"); } - out: while (foo) { - if (bar) break out; + while (foo) { + if (bar) break; console.log("foo"); } } @@ -189,23 +190,22 @@ labels_10: { conditionals: true, dead_code: true, if_return: true, + unused: true, } input: { - out: while (foo) { - x(); - y(); + out: while (42) { + console.log("PASS"); break out; - z(); - k(); + console.log("FAIL"); } }; expect: { - out: while (foo) { - x(); - y(); - break out; + while (42) { + console.log("PASS"); + break; } } + expect_stdout: "PASS" } issue_4466_1: {