fix: preserve blank line after inline block comment in stringify#65
Open
spokodev wants to merge 1 commit into
Open
fix: preserve blank line after inline block comment in stringify#65spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
A blank line after an inline block comment (/* ... */) that follows a comma was silently dropped by stringify, while the identical case with a line comment (// ...) round-tripped correctly. The parse trees for both cases are identical (both carry the BlankLine token), so the defect is in stringify. In join(), the one && two branch did one + two.trim() + LF + gap, and two.trim() discarded the blank-line count carried by two. The line-comment case survived only because process_comments appends a trailing LF after a LineComment, which landed the missing break in one by coincidence. Emit the blank lines that two carries beyond those already supplied by the trailing LF + gap and the trailing line breaks of 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.
The bug
A blank line that follows an inline block comment (
/* ... */) placed after a comma is silently dropped bystringify. The identical construct with a line comment (// ...) round-trips correctly.It also reproduces in arrays (
[\n 1, /* x */\n\n 2\n]) and collapses multiple blank lines to zero.Why it is a stringify defect
For both the line- and block-comment inputs,
parseproduces the same structure. The only difference is the commenttype; theBlankLinetoken inbefore:bis captured identically in both. Yet it renders only in the line-comment case, so the loss happens purely instringify.Root cause
In
join(), theone && twobranch did:Here
twois the renderedbefore:<key>slot and carries the blank-line count as leading line breaks.two.trim()discards them, and the branch hardcodes a singleLF + gap. The line-comment case survives only becauseprocess_commentsappends a trailingLFafter aLineComment, which lands the missing break inoneby coincidence; the block-comment case has no such trailingLF, so the blank line is lost.The fix
Emit the blank lines that
twocarries beyond those already supplied by the trailingLF + gapand the trailing line breaks ofone:count_leading_line_breaksmirrors the existingcount_trailing_line_breakshelper. Whentwocarries no extra blank lines the clamp yields0, so the output is byte-identical to before for every existing case.Tests
Added three round-trip tests (block after object property, line-comment control, block in array). The block cases fail on
masterand pass with the fix; the line-comment control passes both ways. The full suite stays green (403 tests, 100% branch coverage onstringify.js).