fix corner case

add tests
This commit is contained in:
alexlamsl 2018-01-08 12:26:56 +08:00
parent 30ea1a2006
commit a296df2dec
2 changed files with 75 additions and 2 deletions

View File

@ -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;

View File

@ -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"
}