Commit c74061f
committed
Place closing brace on its own line for comment-only blocks
When a block or object literal contains only block comment(s) and no
other tokens, the closing brace was kept on the same line as the last
comment, producing output such as:
var a = {
/* c */ }
This is not idempotent: running the beautifier on that output a second
time correctly moves the brace to its own line, so beautify(beautify(x))
differed from beautify(x).
The cause is in handle_end_block: empty_braces was computed solely from
last_token being the opening brace. Comments stored in comments_before
are emitted without updating last_token, so a brace-only-with-comment
block was treated as a genuinely empty {} and no newline was printed
before the closing brace. handle_start_block already guards the
symmetric case with !next_token.comments_before; this applies the same
guard to handle_end_block so the closing brace is placed on its own line
whenever the block contains comments.
Genuinely empty braces ({}) are unaffected and remain collapsed.
The change is applied to both the JavaScript and Python implementations,
with regression tests added to the shared test data.1 parent 305a0c0 commit c74061f
3 files changed
Lines changed: 23 additions & 2 deletions
File tree
- js/src/javascript
- python/jsbeautifier/javascript
- test/data/javascript
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
790 | 790 | | |
791 | 791 | | |
792 | 792 | | |
793 | | - | |
| 793 | + | |
794 | 794 | | |
795 | 795 | | |
796 | 796 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
854 | 854 | | |
855 | 855 | | |
856 | 856 | | |
857 | | - | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
858 | 861 | | |
859 | 862 | | |
860 | 863 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3006 | 3006 | | |
3007 | 3007 | | |
3008 | 3008 | | |
| 3009 | + | |
| 3010 | + | |
| 3011 | + | |
| 3012 | + | |
| 3013 | + | |
| 3014 | + | |
| 3015 | + | |
| 3016 | + | |
| 3017 | + | |
| 3018 | + | |
| 3019 | + | |
| 3020 | + | |
| 3021 | + | |
| 3022 | + | |
| 3023 | + | |
| 3024 | + | |
| 3025 | + | |
| 3026 | + | |
3009 | 3027 | | |
3010 | 3028 | | |
3011 | 3029 | | |
| |||
0 commit comments