fix: preserve JSONC comments between name/colon and value/comma (#24)#51
Merged
Merged
Conversation
…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.
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 #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:
after-key)after-value)Reproduction from the issue (
after-keyandafter-valueat object spot 0 disappear):{ "k0" // after-key : // before-value "v0" // after-value , }Cause
jsonc-parserkeys 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 (coversafter-key, keyed at the name end, and own-linebefore-value, keyed at the value start).gen_comma_separated_value— before the comma (coversafter-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.txtwith the exact issue reproduction plus an array variant. All specs pass and remain idempotent underformat_twice.