Skip to content

Commit

Permalink
improve beautified output of switch blocks (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Mar 28, 2017
1 parent ae740b9 commit fef0bf9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,24 +960,24 @@ function OutputStream(options) {
self.expression.print(output);
});
output.space();
if (self.body.length > 0) output.with_block(function(){
self.body.forEach(function(stmt, i){
if (i) output.newline();
var last = self.body.length - 1;
if (last < 0) output.print("{}");
else output.with_block(function(){
self.body.forEach(function(branch, i){
output.indent(true);
stmt.print(output);
branch.print(output);
if (i < last && branch.body.length > 0)
output.newline();
});
});
else output.print("{}");
});
AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output){
if (this.body.length > 0) {
output.newline();
this.body.forEach(function(stmt){
output.indent();
stmt.print(output);
output.newline();
this.body.forEach(function(stmt){
output.indent();
stmt.print(output);
output.newline();
});
}
});
});
DEFPRINT(AST_Default, function(self, output){
output.print("default:");
Expand Down
41 changes: 41 additions & 0 deletions test/compress/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,44 @@ issue_1705_3: {
}
expect_stdout: true
}

beautify: {
beautify = {
beautify: true,
}
input: {
switch (a) {
case 0:
case 1:
break;
case 2:
default:
}
switch (b) {
case 3:
foo();
bar();
default:
break;
}
}
expect_exact: [
"switch (a) {",
" case 0:",
" case 1:",
" break;",
"",
" case 2:",
" default:",
"}",
"",
"switch (b) {",
" case 3:",
" foo();",
" bar();",
"",
" default:",
" break;",
"}",
]
}

0 comments on commit fef0bf9

Please sign in to comment.