Skip to content

fix(yaml): use a non-zero indent when stringifying flat documents - #103

Merged
pi0 merged 2 commits into
unjs:mainfrom
RaphaelFakhri:fix/yaml-flat-indent
Oct 29, 2026
Merged

pi0 merged 2 commits into
unjs:mainfrom
RaphaelFakhri:fix/yaml-flat-indent

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Oct 19, 2026 •

Copy link
Copy Markdown

Problem

When parseYAML reads a document with no indented lines, such as a: 1, the detected indent is "". stringifyYAML passes its length, 0, to js-yaml's dump. Any nested value added to that object is then written at column 0, which changes the data when read back:

const config = parseYAML("a: 1\n");
config.b = { c: [1, 2] };
stringifyYAML(config); // "a: 1\nb:\nc:\n- 1\n- 2\n"
parseYAML(stringifyYAML(config)); // { a: 1, b: null, c: [1, 2] }

Change

stringifyYAML falls back to an indent of 2 when the detected indent is 0. This is the same default getFormat uses for values that were not parsed. Detected non-zero indents and an explicit indent option behave as before.

Tests

A new yaml test parses a flat document, adds a nested object and array, and checks both the output and that it parses back to the same data.

  • Without the src/yaml.ts change: 1 failed, 28 passed, 1 skipped.
  • With the change: 29 passed, 1 skipped. pnpm test (oxlint, oxfmt, tsc, vitest) and pnpm build pass.

Fixes #102

Summary by CodeRabbit

  • Bug Fixes

    • YAML output now uses two-space indentation when no indentation is detected, while preserving detected indentation when available.
  • Tests

    • Added coverage for serializing and parsing YAML documents with nested objects and arrays.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 746d71fc-caed-4de8-91ea-ccd2e02f564b
📥 Commits

Reviewing files that changed from the base of the PR and between eebbd54 and 1ac84a3.

📒 Files selected for processing: 2
  • src/yaml.ts
  • test/index.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

stringifyYAML now defaults to two-space indentation when no indentation is detected. A test covers serializing nested values added to a flat YAML document and reparsing the result.

Changes: YAML serialization

Layer / File(s) Summary
Serialization indentation and validation
src/yaml.ts, test/index.test.ts
stringifyYAML uses an indentation width of 2 when detected indentation is missing or zero. The test checks the serialized text and confirms that reparsing it reproduces the augmented object.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: pi0

Merge Risk: Minimal · previous review 1ac84

Flat YAML extended with nested values now has a two-space indentation fallback, with a test for serialization and reparsing. No actionable merge risk remains in the reviewed changes.

Architecture risk: Low · up to 1ac84

The change affects 2 systems.

Changed systems: src, test

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — src (service) was modified; 1 changed file maps to changed impact.
  • observed — test (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in src/yaml.ts: stringifyYAML now defaults a missing or zero detected indentation width to 2; previously it passed the detected value through without a fallback.
  • observed — Modified behavior in test/index.test.ts: Added a test that adds nested object and array values to a parsed flat YAML document, asserts the exact stringified output, and asserts that reparsing it equals the augmented object.
Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: using a non-zero indent when stringifying flat YAML documents.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pi0
pi0 merged commit dafb56a into unjs:main Oct 29, 2026
2 of 3 checks passed
@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@eebbd54). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #103   +/-   ##
=======================================
  Coverage        ?   98.75%           
=======================================
  Files           ?        8           
  Lines           ?       80           
  Branches        ?       17           
=======================================
  Hits            ?       79           
  Misses          ?        1           
  Partials        ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

stringifyYAML writes invalid nesting for values parsed from a flat YAML document

2 participants