From a296df2dece09e84d6d77b44cc46a842cfc1ba8c Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Mon, 8 Jan 2018 12:26:56 +0800 Subject: [PATCH] fix corner case add tests --- lib/compress.js | 5 +-- test/compress/loops.js | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index f52bde68..70163656 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3418,7 +3418,7 @@ merge(Compressor.prototype, { function if_break_in_loop(self, compressor) { var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body; - if (is_break(first)) { + if (compressor.option("dead_code") && is_break(first)) { var body = []; if (self.init instanceof AST_Statement) { body.push(self.init); @@ -3432,6 +3432,7 @@ merge(Compressor.prototype, { body: self.condition })); } + extract_declarations_from_unreachable_code(compressor, self.body, body); return make_node(AST_BlockStatement, self, { body: body }); @@ -3481,7 +3482,7 @@ merge(Compressor.prototype, { } self = if_break_in_loop(self, compressor); } - }; + } OPT(AST_For, function(self, compressor){ if (!compressor.option("loops")) return self; diff --git a/test/compress/loops.js b/test/compress/loops.js index 8e677ac1..a6ebd359 100644 --- a/test/compress/loops.js +++ b/test/compress/loops.js @@ -495,6 +495,7 @@ dead_code_condition: { issue_2740_1: { options = { + dead_code: true, loops: true, } input: { @@ -521,6 +522,7 @@ issue_2740_1: { issue_2740_2: { options = { + dead_code: true, loops: true, passes: 2, } @@ -533,3 +535,73 @@ issue_2740_2: { x(); } } + +issue_2740_3: { + options = { + dead_code: true, + loops: true, + } + input: { + L1: for (var x = 0; x < 3; x++) { + L2: for (var y = 0; y < 2; y++) { + break L1; + } + } + console.log(x, y); + } + expect: { + L1: for (var x = 0; x < 3; x++) + for (var y = 0; y < 2; y++) + break L1; + console.log(x, y); + } + expect_stdout: "0 0" +} + +issue_2740_4: { + options = { + dead_code: true, + loops: true, + passes: 2, + } + input: { + L1: for (var x = 0; x < 3; x++) { + L2: for (var y = 0; y < 2; y++) { + break L2; + } + } + console.log(x, y); + } + expect: { + for (var x = 0; x < 3; x++) { + var y = 0; + y < 2; + } + console.log(x, y); + } + expect_stdout: "3 0" +} + +issue_2740_5: { + options = { + dead_code: true, + loops: true, + passes: 2, + } + input: { + L1: for (var x = 0; x < 3; x++) { + break L1; + L2: for (var y = 0; y < 2; y++) { + break L2; + } + } + console.log(x, y); + } + expect: { + var x = 0; + x < 3; + var y; + console.log(x,y); + } + expect_stdout: "0 undefined" +}