Skip to content

Commit c74061f

Browse files
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/beautifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ Beautifier.prototype.handle_end_block = function(current_token) {
790790
this.restore_mode();
791791
}
792792

793-
var empty_braces = this._flags.last_token.type === TOKEN.START_BLOCK;
793+
var empty_braces = !current_token.comments_before && this._flags.last_token.type === TOKEN.START_BLOCK;
794794

795795
if (this._flags.inline_frame && !empty_braces) { // try inline_frame (only set if this._options.braces-preserve-inline) first
796796
this._output.space_before_token = true;

python/jsbeautifier/javascript/beautifier.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,10 @@ def handle_end_block(self, current_token):
854854
while self._flags.mode == MODE.Statement:
855855
self.restore_mode()
856856

857-
empty_braces = self._flags.last_token.type == TOKEN.START_BLOCK
857+
empty_braces = (
858+
current_token.comments_before is None
859+
and self._flags.last_token.type == TOKEN.START_BLOCK
860+
)
858861

859862
# try inline_frame (only set if opt.braces-preserve-inline) first
860863
if self._flags.inline_frame and not empty_braces:

test/data/javascript/tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,6 +3006,24 @@ exports.test_data = {
30063006
'foo(o)'
30073007
]
30083008
},
3009+
{
3010+
comment: 'object literal containing only a block comment - closing brace on its own line',
3011+
input: 'var a = {/* c */}',
3012+
output: [
3013+
'var a = {',
3014+
' /* c */',
3015+
'}'
3016+
]
3017+
},
3018+
{
3019+
comment: 'block statement containing only a block comment - closing brace on its own line',
3020+
input: 'if (x) {/* c */}',
3021+
output: [
3022+
'if (x) {',
3023+
' /* c */',
3024+
'}'
3025+
]
3026+
},
30093027
{
30103028
comment: '#713 and #964',
30113029
unchanged: [

0 commit comments

Comments
 (0)