Skip to content

fix: Round-trip block scalars with leading space on first line#693

Open
Noethix55555 wants to merge 1 commit into
eemeli:mainfrom
Noethix55555:fix/blockscalar-leading-space-roundtrip
Open

fix: Round-trip block scalars with leading space on first line#693
Noethix55555 wants to merge 1 commit into
eemeli:mainfrom
Noethix55555:fix/blockscalar-leading-space-roundtrip

Conversation

@Noethix55555

Copy link
Copy Markdown

Fixes #692.

Problem

stringify() of a multiline string whose first content line starts with a space produced a block scalar whose declared indentation indicator did not match the body indentation. At the document root ctx.indent is '', but the indicator was emitted as 1, while body lines were prefixed with the (empty) root indent. As a result the output either silently dropped a leading space per line or was not parseable by parse():

parse(stringify('  a\n  b'))           // => " a\n b"  (lost a leading space)
parse(stringify('  indented\nlines'))  // throws YAMLParseError

Fix

In blockString (src/stringify/stringifyString.ts) introduce a bodyIndent that honors the declared indicator at the root (a single space when the value starts with a space and indent is empty), and use it for the body in both the literal and folded paths. The folded path additionally accounts for this added indent when deciding whether the first line is "more-indented", so a more-indented first line is no longer incorrectly folded.

Non-root and non-leading-space behavior is unchanged.

Tests

  • Added round-trip regression tests in tests/doc/stringify.ts covering several leading-space cases. These fail on main and pass with the fix.
  • Updated the existing tests/doc/foldFlowLines.ts "More-indented first line (Long strings that start with spaces aren't rendered properly (and lose newlines) #55)" test: it was asserting the buggy output, whose expected string was itself unparseable. It now asserts the corrected, round-trippable output and additionally checks that the output parses back to the original.

The full vitest run suite passes (the two external test-suite files require submodule data and fail identically on a pristine tree).

LLM assistance

This pull request was prepared with the assistance of an LLM (declared per CONTRIBUTING.md). All changes were reviewed and verified locally.

…i#692)

At the document root the indentation indicator was emitted as 1 while the
body lines were indented with the empty root indent, so stringify() output
either dropped a leading space per line or was unparseable by parse().

Indent the body to match the declared indicator, and account for the added
indent when deciding whether a folded first line is more-indented.

The existing foldFlowLines eemeli#55 test asserted the buggy, unparseable output;
its expectation is updated to the corrected, round-trippable output.
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.

stringify() of multiline string with leading space on first line does not round-trip through parse()

1 participant