Place closing brace on its own line for comment-only blocks - #2469
Open
sarathfrancis90 wants to merge 1 commit into
Open
Place closing brace on its own line for comment-only blocks#2469sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit into
Conversation
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.
sarathfrancis90
force-pushed
the
fix/js-object-comment-only-closing-brace
branch
from
July 19, 2026 00:23
d9bf076 to
c74061f
Compare
Author
|
Rebased onto current main; Quick recap since it has been a while: a block containing only comments was treated as empty in No rush — just flagging that it is current again if you want to take a look. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
main)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. The output was not idempotent: beautifying the output a second time moved the brace onto its own line, so
beautify(beautify(x)) !== beautify(x).Example
Input:
Current output (one pass):
Beautifying that output again yields the correct, stable result:
Root cause
In
handle_end_block,empty_braceswas computed solely fromlast_tokenbeing the opening brace:Comments held in
comments_beforeare emitted without updatinglast_token, so a block whose only content is a comment was treated as a genuinely empty{}and no newline was printed before the closing brace. The symmetrichandle_start_blockalready guards this case with!next_token.comments_before; this PR applies the same guard tohandle_end_block:Genuinely empty braces (
{}) are unaffected and remain collapsed. The same change is applied to the Python implementation to keep the two implementations in parity.Tests
Two regression cases were added to the shared test data (
test/data/javascript/tests.js), covering both an object literal and a block statement that contain only a block comment.make cipasses locally for both the JavaScript and Python implementations.Fixes Issue:
Before Merge Checklist
These items can be completed after PR is created.
(Check any items that are not applicable (NA) for this PR)