Skip to content

fix: preserve JSONC comments between name/colon and value/comma (#24)#51

Merged
dsherret merged 6 commits into
dprint:mainfrom
todor-a:fix-jsonc-comment-loss-issue-24
Jun 17, 2026
Merged

fix: preserve JSONC comments between name/colon and value/comma (#24)#51
dsherret merged 6 commits into
dprint:mainfrom
todor-a:fix-jsonc-comment-loss-issue-24

Conversation

@todor-a

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

Copy link
Copy Markdown
Contributor

Fixes #24.


Transparency note: This PR was prepared with AI assistance (Claude).


Problem

Formatting JSONC silently drops comments that sit on their own line in a slot with no AST node to attach to, while still reporting success:

  • between a property name and its colon (after-key)
  • between a value and its trailing comma (after-value)

Reproduction from the issue (after-key and after-value at object spot 0 disappear):

{
  "k0"
  // after-key
  :
  // before-value
  "v0"
  // after-value
  ,
}

Cause

jsonc-parser keys each comment group by both the previous-token-end and the next-token-start. The generator, however, only emitted comments as leading (at a node start) or same-line trailing. Own-line comments in the two slots above (where the neighbouring token — colon or comma — is not an AST node) matched no emission path and were lost.

Fix

Emit those own-line comments as statements:

  • gen_object_prop — after the colon (covers after-key, keyed at the name end, and own-line before-value, keyed at the value start).
  • gen_comma_separated_value — before the comma (covers after-value; applies to both objects and arrays since they share this code).

Same-line comments keep their existing handling, so no other cases change. Line comments already emit ExpectNewLine, so no extra newline is pushed.

Tests

Added tests/specs/comments/Comments_PreservedBetweenTokens.txt with the exact issue reproduction plus an array variant. All specs pass and remain idempotent under format_twice.

todor-a and others added 6 commits May 29, 2026 08:33
…nt#24)

Comments that sat on their own line in a slot with no AST node to attach
to were silently dropped while formatting still reported success:

  - between a property name and its colon ("after-key")
  - between a value and its trailing comma ("after-value")

jsonc-parser keys each comment group by both the previous-token-end and
the next-token-start, but the generator only emitted comments as leading
(at a node start) or same-line trailing. Own-line comments in the two
slots above matched no emission path and were lost.

Emit those own-line comments as statements in gen_object_prop (after the
colon) and in gen_comma_separated_value (before the comma, covering both
objects and arrays). Same-line comments keep their existing handling, so
there is no change to other cases.

@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 2a2d2fe 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.

Formatting JSONC can lose comments yet still indicate success

2 participants