Skip to content

fix(stringify): use indentStep length for mapping block scalar indent indicator - #738

Open
princejain756 wants to merge 1 commit into
eemeli:mainfrom
princejain756:fix/block-scalar-indent-indicator
Open

princejain756 wants to merge 1 commit into
eemeli:mainfrom
princejain756:fix/block-scalar-indent-indicator

Conversation

@princejain756

@princejain756 princejain756 commented Oct 22, 2026 •

Copy link
Copy Markdown

Issue

fixes #733

LLM Use

I used an LLM tool when working on the code change, and wrote this PR description and comments myself.

Description

In blockString(), the indentation indicator for a block scalar starting with spaces was hardcoded to '2' whenever indent was non-empty. That works for the default 2-space indent and for block sequence items (since #stringifyBlockSeq() always indents item content by ' '), but mapping and set values are indented by ctx.indentStep. When stringify() is called with a custom indent like 1, 3, or 4, the header still emitted |2 or >2 while the body was indented by ctx.indentStep.length spaces, so parsing the output back either failed or shifted the leading spaces.

This change uses String(ctx.indentStep.length) in blockString() when ctx.implicitKey === false (mapping and set values), and resets implicitKey: undefined in YAMLSeq.#stringifyBlockSeq() so block sequences nested inside a mapping value still use '2' to match their 2-space item indent.

… indicator

When a block scalar starts with a space and is serialized inside a mapping or set, its content is indented by `ctx.indentStep` relative to its parent node. Previously `blockString()` hardcoded `'2'` whenever `indent` was non-empty, causing `stringify()` with `indent` other than 2 to emit an indentation indicator that disagreed with the content indentation.

Reset `implicitKey` to `undefined` for block sequence items (which always indent by 2 spaces relative to `-`) and use `ctx.indentStep.length` when `ctx.implicitKey === false`.

Fixes eemeli#733

@eemeli eemeli left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite the description in your own words.

@char0n's comment on the parent issue, "You should be fine, AI or no AI.", does not follow the contributing guide instructions:

Prose contributions and comments must be your own writing, not the product of large language models (LLMs) or other tools. This includes code comments added in PRs, as well as all issue and PR contents.

He is also not a maintainer of the project. I probably need to refactor the guide once again to really underline the point that LLM use is acceptable for code contributions only.

Comment thread src/nodes/YAMLSeq.ts
} = ctx
const itemIndent = indent + ' '
const itemCtx = { ...ctx, indent: itemIndent }
const itemCtx = { ...ctx, implicitKey: undefined, indent: itemIndent }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? And is this covered by the tests added here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a block sequence is the value of a mapping pair (like seq: [' lead\nx\n']), stringifyPair() sets ctx.implicitKey = false before stringifying the value. Because #stringifyBlockSeq() spreads ctx into itemCtx and always indents sequence items by 2 spaces (indent + ' '), leaving implicitKey: false on itemCtx causes blockString() to emit - |4 instead of - |2 when indent: 4, which strips the two leading spaces on parse.

Yes, the nested case in the test (const nested = { a: { k: ' lead\nx\n' }, seq: [' lead\nx\n', { m: ' lead\nx\n' }] } with { indent: 4 }) covers this: without resetting implicitKey here, YAML.parse(strNested).seq[0] comes back as 'lead\nx\n' instead of ' lead\nx\n'.

@princejain756

Copy link
Copy Markdown
Author

Sorry for the confusion around @char0n's comment earlier. I rewrote the PR description in my own words and replied inline on the YAMLSeq question.

@char0n

char0n commented Nov 11, 2026

Copy link
Copy Markdown

@eemeli is right, sorry for interfering with the repo processes.

This branch has not been deployed

No deployments
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() writes wrong block scalar indentation indicator for mapping values with indent other than 2

3 participants