plan B for IE8 do-while semi-colon fix (#1572)

- omitting trailing semi-colon in do-while breaks non-browser parser, e.g. uglify-js 1.x
- trailing semi-colon only breaks IE8 if followed by `else` or `while`
- always use braces in do-while body to workaround 2nd case with no size loss in compression

fixes #1568
This commit is contained in:
Alex Lam S.L
2017-03-08 05:07:05 +08:00
committed by GitHub
parent bd6dee52ab
commit dedbeeff15
2 changed files with 17 additions and 29 deletions

View File

@@ -777,16 +777,14 @@ function OutputStream(options) {
DEFPRINT(AST_Do, function(self, output){ DEFPRINT(AST_Do, function(self, output){
output.print("do"); output.print("do");
output.space(); output.space();
self._do_print_body(output); make_block(self.body, output);
output.space(); output.space();
output.print("while"); output.print("while");
output.space(); output.space();
output.with_parens(function(){ output.with_parens(function(){
self.condition.print(output); self.condition.print(output);
}); });
if (output.option("beautify") && output.option("screw_ie8")) {
output.semicolon(); output.semicolon();
}
}); });
DEFPRINT(AST_While, function(self, output){ DEFPRINT(AST_While, function(self, output){
output.print("while"); output.print("while");
@@ -906,10 +904,10 @@ function OutputStream(options) {
/* -----[ if ]----- */ /* -----[ if ]----- */
function make_then(self, output) { function make_then(self, output) {
if (output.option("bracketize")) { var b = self.body;
make_block(self.body, output); if (output.option("bracketize")
return; || !output.option("screw_ie8") && b instanceof AST_Do)
} return make_block(b, output);
// The squeezer replaces "block"-s that contain only a single // The squeezer replaces "block"-s that contain only a single
// statement with the statement itself; technically, the AST // statement with the statement itself; technically, the AST
// is correct, but this can create problems when we output an // is correct, but this can create problems when we output an
@@ -917,9 +915,7 @@ function OutputStream(options) {
// IF *without* an ELSE block (then the outer ELSE would refer // IF *without* an ELSE block (then the outer ELSE would refer
// to the inner IF). This function checks for this case and // to the inner IF). This function checks for this case and
// adds the block brackets if needed. // adds the block brackets if needed.
if (!self.body) if (!b) return output.force_semicolon();
return output.force_semicolon();
var b = self.body;
while (true) { while (true) {
if (b instanceof AST_If) { if (b instanceof AST_If) {
if (!b.alternative) { if (!b.alternative) {
@@ -1328,15 +1324,7 @@ function OutputStream(options) {
function force_statement(stat, output) { function force_statement(stat, output) {
if (output.option("bracketize")) { if (output.option("bracketize")) {
if (!stat || stat instanceof AST_EmptyStatement) make_block(stat, output);
output.print("{}");
else if (stat instanceof AST_BlockStatement)
stat.print(output);
else output.with_block(function(){
output.indent();
stat.print(output);
output.newline();
});
} else { } else {
if (!stat || stat instanceof AST_EmptyStatement) if (!stat || stat instanceof AST_EmptyStatement)
output.force_semicolon(); output.force_semicolon();
@@ -1385,11 +1373,11 @@ function OutputStream(options) {
}; };
function make_block(stmt, output) { function make_block(stmt, output) {
if (stmt instanceof AST_BlockStatement) { if (!stmt || stmt instanceof AST_EmptyStatement)
output.print("{}");
else if (stmt instanceof AST_BlockStatement)
stmt.print(output); stmt.print(output);
return; else output.with_block(function(){
}
output.with_block(function(){
output.indent(); output.indent();
stmt.print(output); stmt.print(output);
output.newline(); output.newline();

View File

@@ -257,7 +257,7 @@ issue_186: {
else else
bar(); bar();
} }
expect_exact: 'var x=3;if(foo())do do alert(x);while(--x)while(x)else bar();' expect_exact: 'var x=3;if(foo())do{do{alert(x)}while(--x)}while(x);else bar();'
} }
issue_186_ie8: { issue_186_ie8: {
@@ -276,7 +276,7 @@ issue_186_ie8: {
else else
bar(); bar();
} }
expect_exact: 'var x=3;if(foo())do do alert(x);while(--x)while(x)else bar();' expect_exact: 'var x=3;if(foo()){do{do{alert(x)}while(--x)}while(x)}else bar();'
} }
issue_186_beautify: { issue_186_beautify: {
@@ -295,7 +295,7 @@ issue_186_beautify: {
else else
bar(); bar();
} }
expect_exact: 'var x = 3;\n\nif (foo()) do do alert(x); while (--x); while (x); else bar();' expect_exact: 'var x = 3;\n\nif (foo()) do {\n do {\n alert(x);\n } while (--x);\n} while (x); else bar();'
} }
issue_186_beautify_ie8: { issue_186_beautify_ie8: {
@@ -314,7 +314,7 @@ issue_186_beautify_ie8: {
else else
bar(); bar();
} }
expect_exact: 'var x = 3;\n\nif (foo()) do do alert(x); while (--x) while (x) else bar();' expect_exact: 'var x = 3;\n\nif (foo()) {\n do {\n do {\n alert(x);\n } while (--x);\n } while (x);\n} else bar();'
} }
issue_186_bracketize: { issue_186_bracketize: {
@@ -394,5 +394,5 @@ issue_186_beautify_bracketize_ie8: {
else else
bar(); bar();
} }
expect_exact: 'var x = 3;\n\nif (foo()) {\n do {\n do {\n alert(x);\n } while (--x)\n } while (x)\n} else {\n bar();\n}' expect_exact: 'var x = 3;\n\nif (foo()) {\n do {\n do {\n alert(x);\n } while (--x);\n } while (x);\n} else {\n bar();\n}'
} }