Skip to content

fix: keep indent indicator on empty block scalar before a comment#697

Open
maximilize wants to merge 1 commit into
eemeli:mainfrom
maximilize:fix/687-empty-block-scalar-comment
Open

fix: keep indent indicator on empty block scalar before a comment#697
maximilize wants to merge 1 commit into
eemeli:mainfrom
maximilize:fix/687-empty-block-scalar-comment

Conversation

@maximilize

Copy link
Copy Markdown

Problem

Document.toString() corrupts an empty block scalar that is followed by a document comment — the comment round-trips back as the scalar's content:

const doc = parseDocument('|5\n#comment')
doc.toJSON() // '' — correct: #comment is at indent 0 < 5, so it's a comment, not content

const out = doc.toString() // '|\n\n\n#comment\n'
parseDocument(out).toJSON() // '\n\n#comment\n'  ← should be ''

Fixes #687.

Cause

blockString() returns a bare |/> for an empty value, dropping the indent indicator even when ctx.forceBlockIndent is set — which stringifyDocument sets precisely when a document comment follows. Without the indicator the empty scalar has no explicit extent, so the trailing comment is absorbed as its content on re-parse.

Fix

Emit the indent indicator for the empty-value case when forceBlockIndent is set, mirroring the existing indentSize logic a few lines below.

Tests

Added a round-trip regression test in tests/doc/comments.ts. The full suite passes, including the YAML and JSON test suites — no regressions.


🤖 This pull request was written with AI assistance (Claude Code).

An empty block scalar followed by a document comment dropped its explicit
indent indicator, so on re-parse the comment was absorbed as the scalar's
content. Emit the indicator for the empty-value case under forceBlockIndent,
mirroring the existing indentSize logic. Fixes eemeli#687.
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.

toString() incorrectly converts comment to string

1 participant