Skip to content

fix: preserve blank line before trailing comments at end of object/array (#34)#52

Merged
dsherret merged 2 commits into
dprint:mainfrom
todor-a:fix-blank-line-before-trailing-comments-issue-34
Jun 17, 2026
Merged

fix: preserve blank line before trailing comments at end of object/array (#34)#52
dsherret merged 2 commits into
dprint:mainfrom
todor-a:fix-blank-line-before-trailing-comments-issue-34

Conversation

@todor-a

@todor-a todor-a commented May 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #34.

Problem

A blank line before a comment at the end of an object (or array) is dropped, even though dprint preserves blank lines before comments everywhere else:

{
  // This is a setting.
  "foo": "",

  // These settings are temporarily commented out.
  // "baz": "",
  // "qux": ""
}

The blank line before // These settings... is removed.

Cause

Comments at the end of a container are emitted via the close-token leading-comments path in gen_surrounded_by_tokens, which calls gen_comments_as_statements with last_node = None. With no previous node, the blank-line gap between the last member and the comment is never computed, so the blank line is lost. Comments between members go through gen_comments_as_leading, which is why they keep their blank line.

Fix

Before emitting the close-token leading comments, add a single newline when the source had a blank line between the preceding token and the first comment. The separating newline is already emitted by the surrounding structure, so only the blank line itself is added (and multiple blank lines still collapse to one).

Applies to both objects and arrays.

Tests

Added tests/specs/comments/Comments_BlankLineBeforeTrailing.txt covering: object (the issue repro), array, the no-blank-line case (no regression), and multiple-blank-line collapse. All specs pass and remain idempotent under format_twice.

todor-a and others added 2 commits May 29, 2026 08:52
…ray (dprint#34)

Comments at the end of an object or array (before the closing token) are
emitted via the close-token leading-comments path, which called
gen_comments_as_statements with no "last node". The blank-line gap between
the previous member and the comment was therefore never computed, so an
intentional blank line before a trailing comment block was dropped.

Emit one extra newline before those comments when the source had a blank
line between the preceding token and the first comment. The surrounding
structure already emits the single separating newline, so only the blank
line itself is added (and multiple blank lines still collapse to one).

@dsherret dsherret left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dsherret dsherret merged commit 7f23b2d into dprint:main Jun 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comments at the end of objects have their preceding newlines removed

2 participants