Skip to content

Commit

Permalink
tweak do-while loops
Browse files Browse the repository at this point in the history
- `do{...}while(false)` => `{...}`
- clean up `AST_While` logic
  • Loading branch information
alexlamsl committed Jan 30, 2017
1 parent 7f8d72d commit 1077373
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1749,8 +1749,13 @@ merge(Compressor.prototype, {
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
}
} else {
return self.body;
}
}
if (self instanceof AST_While) {
return make_node(AST_For, self, self).optimize(compressor);
}
return self;
});

Expand Down Expand Up @@ -1799,16 +1804,6 @@ merge(Compressor.prototype, {
}
};

OPT(AST_While, function(self, compressor) {
if (!compressor.option("loops")) return self;
self = AST_DWLoop.prototype.optimize.call(self, compressor);
if (self instanceof AST_While) {
if_break_in_loop(self, compressor);
self = make_node(AST_For, self, self).transform(compressor);
}
return self;
});

OPT(AST_For, function(self, compressor){
var cond = self.condition;
if (cond) {
Expand Down
29 changes: 29 additions & 0 deletions test/compress/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,32 @@ keep_collapse_const_in_own_block_scope_2: {
console.log(c);
}
}

evaluate: {
options = {
loops: true,
dead_code: true,
evaluate: true,
};
input: {
while (true) {
a();
}
while (false) {
b();
}
do {
c();
} while (true);
do {
d();
} while (false);
}
expect: {
for(;;)
a();
for(;;)
c();
d();
}
}

0 comments on commit 1077373

Please sign in to comment.