Skip to content

fix: don't add trailing newline for empty values with spaceBefore#683

Closed
harihara-ai wants to merge 1 commit into
eemeli:mainfrom
harihara-ai:fix/issue-639-spaceBefore-extra-blank-line
Closed

fix: don't add trailing newline for empty values with spaceBefore#683
harihara-ai wants to merge 1 commit into
eemeli:mainfrom
harihara-ai:fix/issue-639-spaceBefore-extra-blank-line

Conversation

@harihara-ai

Copy link
Copy Markdown

Fixes #639.

Root cause

When a YAML pair has an empty value followed by blank lines (e.g. b: in a:\n b:\n\nc:), the parser stores those blank lines in the pair's sep tokens. When the nested block-map exits before a new item starts at the same level, the special "move blank lines to next item's start" handling in the parser never fires. As a result, resolveProps processes the blank lines in sep and sets spaceBefore: true on the empty value node.

In stringifyPair, this causes ws = '\n' (from vsb = !!value.spaceBefore), which adds a trailing newline to the pair string ('b:\n'). That trailing newline then combines with the blank line added at the parent collection level via pair.key.spaceBefore for the following pair, producing a double blank line.

Fix

One line in stringifyPair.ts: when ws === '\n' (set from value.spaceBefore with no commentBefore) and valueStr === '' (empty value), set ws = ''. The blank line separation between entries is already handled by the parent collection's pair.key.spaceBefore logic — the trailing newline from vsb is redundant and incorrect for empty values.

Tests

Three test cases added inside the existing No extra whitespace for empty values describe block in tests/doc/stringify.ts, matching the minimal examples from the issue:

parseDocument('a:\n  b:\n\nc:').toString()   // was 'a:\n  b:\n\n\nc:\n' → now 'a:\n  b:\n\nc:\n'
parseDocument('- a:\n\n- b').toString()       // was '- a:\n\n\n- b\n'   → now '- a:\n\n- b\n'
parseDocument('a:\n\nb:').toString()          // was 'a:\n\n\nb:\n'       → now 'a:\n\nb:\n'

When a YAML pair has an empty value followed by blank lines, the parser
stores those blank lines in the pair's sep tokens. This causes
value.spaceBefore = true on the null value, which makes stringifyPair
add a trailing newline to the pair string. That trailing newline then
combines with the blank line added at the parent collection level via
key.spaceBefore, producing a double blank line.

Fix: when ws === '\n' (from value.spaceBefore only, no comment) and
valueStr === '' (empty value), set ws = ''. The blank line separation
is already handled by the parent collection's key.spaceBefore.

Fixes eemeli#639
@eemeli

eemeli commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Closing due to undeclared LLM use.

@eemeli eemeli closed this Jun 21, 2026
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.

Empty line added for end hierarchy key after #590 fix

2 participants