fix corner case in loops (#4183)

fixes #4182
This commit is contained in:
Alex Lam S.L
2020-10-05 10:28:46 +01:00
committed by GitHub
parent 2dbe40b01b
commit b7a57fc69d
2 changed files with 79 additions and 7 deletions

View File

@@ -700,7 +700,7 @@ merge(Compressor.prototype, {
tw.in_loop = this;
push(tw);
this.body.walk(tw);
if (has_break_or_continue(this, tw.parent())) {
if (has_loop_control(this, tw.parent())) {
pop(tw);
push(tw);
}
@@ -717,7 +717,7 @@ merge(Compressor.prototype, {
if (this.condition) this.condition.walk(tw);
this.body.walk(tw);
if (this.step) {
if (has_break_or_continue(this, tw.parent())) {
if (has_loop_control(this, tw.parent())) {
pop(tw);
push(tw);
}
@@ -5812,11 +5812,12 @@ merge(Compressor.prototype, {
return compressor.option("loops") ? make_node(AST_For, self, self).optimize(compressor) : self;
});
function has_break_or_continue(loop, parent) {
function has_loop_control(loop, parent, type) {
if (!type) type = AST_LoopControl;
var found = false;
var tw = new TreeWalker(function(node) {
if (found || node instanceof AST_Scope) return true;
if (node instanceof AST_LoopControl && tw.loopcontrol_target(node) === loop) {
if (node instanceof type && tw.loopcontrol_target(node) === loop) {
return found = true;
}
});
@@ -5840,7 +5841,7 @@ merge(Compressor.prototype, {
]
})
}).optimize(compressor);
if (!has_break_or_continue(self, compressor.parent())) {
if (!has_loop_control(self, compressor.parent())) {
return make_node(AST_BlockStatement, self.body, {
body: [
self.body,
@@ -5851,14 +5852,14 @@ merge(Compressor.prototype, {
}).optimize(compressor);
}
}
if (self.body instanceof AST_BlockStatement) {
if (self.body instanceof AST_BlockStatement && !has_loop_control(self, compressor.parent(), AST_Continue)) {
var body = self.body.body;
for (var i = body.length; --i >= 0;) {
var stat = body[i];
if (stat instanceof AST_If
&& !stat.alternative
&& stat.body instanceof AST_Break
&& compressor.loopcontrol_target(stat.body) === compressor.self()) {
&& compressor.loopcontrol_target(stat.body) === self) {
self.condition = make_node(AST_Binary, self, {
operator: "&&",
left: stat.condition.negate(compressor),