From 80cdddb79bffa9b24c4d99ec000b413f2d8a3061 Mon Sep 17 00:00:00 2001 From: Antoine Pietri Date: Wed, 3 Jun 2026 19:22:42 +0200 Subject: [PATCH] Fix YAML formatting: preserve empty line after block scalar (#19205) Co-authored-by: fisker Cheung --- changelog_unreleased/yaml/19205.md | 27 +++++++++++ src/language-yaml/print/misc.js | 4 +- .../__snapshots__/format.test.js.snap | 46 +++++++++++++++++++ .../empty-line-after-block-scalar.yml | 6 +++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/yaml/19205.md create mode 100644 tests/format/yaml/block-folded/empty-line-after-block-scalar.yml diff --git a/changelog_unreleased/yaml/19205.md b/changelog_unreleased/yaml/19205.md new file mode 100644 index 000000000..20415473a --- /dev/null +++ b/changelog_unreleased/yaml/19205.md @@ -0,0 +1,27 @@ +#### Preserve empty line after block scalar (#19205 by @seirl) + + +```yaml +# Input +foo: > + a + b +bar: baz + +qux: quux + +# Prettier stable +foo: > + a + b +bar: baz +qux: quux + +# Prettier main +foo: > + a + b +bar: baz + +qux: quux +``` diff --git a/src/language-yaml/print/misc.js b/src/language-yaml/print/misc.js index 6e0d3966c..5f820e244 100644 --- a/src/language-yaml/print/misc.js +++ b/src/language-yaml/print/misc.js @@ -12,8 +12,8 @@ function printNextEmptyLine(path, originalText) { () => new Set(), ); - if (!isNextEmptyLinePrintedSet.has(node.position.end.line)) { - isNextEmptyLinePrintedSet.add(node.position.end.line); + if (!isNextEmptyLinePrintedSet.has(node.position.end.offset)) { + isNextEmptyLinePrintedSet.add(node.position.end.offset); if ( isNextLineEmpty(node, originalText) && !shouldPrintEndComments(path.parent) diff --git a/tests/format/yaml/block-folded/__snapshots__/format.test.js.snap b/tests/format/yaml/block-folded/__snapshots__/format.test.js.snap index d07f9227f..01ffa02e6 100644 --- a/tests/format/yaml/block-folded/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/block-folded/__snapshots__/format.test.js.snap @@ -125,6 +125,52 @@ parsers: ["yaml"] ================================================================================ `; +exports[`empty-line-after-block-scalar.yml - {"proseWrap":"always"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +proseWrap: "always" + printWidth: 80 (default) | +=====================================input====================================== +foo: > + a + b +bar: baz + +qux: quux + +=====================================output===================================== +foo: > + a b +bar: baz + +qux: quux + +================================================================================ +`; + +exports[`empty-line-after-block-scalar.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] + printWidth: 80 (default) | +=====================================input====================================== +foo: > + a + b +bar: baz + +qux: quux + +=====================================output===================================== +foo: > + a + b +bar: baz + +qux: quux + +================================================================================ +`; + exports[`indent.yml - {"proseWrap":"always"} format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/format/yaml/block-folded/empty-line-after-block-scalar.yml b/tests/format/yaml/block-folded/empty-line-after-block-scalar.yml new file mode 100644 index 000000000..2b442ed21 --- /dev/null +++ b/tests/format/yaml/block-folded/empty-line-after-block-scalar.yml @@ -0,0 +1,6 @@ +foo: > + a + b +bar: baz + +qux: quux