fix: preserve blank line before trailing comments at end of object/array (#34)#52
Open
todor-a wants to merge 1 commit into
Open
Conversation
…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).
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.
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 callsgen_comments_as_statementswithlast_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 throughgen_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.txtcovering: object (the issue repro), array, the no-blank-line case (no regression), and multiple-blank-line collapse. All specs pass and remain idempotent underformat_twice.