From 94b9a058eb761043353e03beb0bfd9b91530aafa Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Fri, 19 Sep 2025 12:47:28 +0800 Subject: [PATCH] Preserve empty line before endComment (#17843) Co-authored-by: fisker --- changelog_unreleased/yaml/17843.md | 18 ++++++++++++++++++ src/language-yaml/printer-yaml.js | 8 +++++++- .../comment/__snapshots__/format.test.js.snap | 18 ++++++++++++++++++ tests/format/yaml/comment/issue-10922.yml | 3 +++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/yaml/17843.md create mode 100644 tests/format/yaml/comment/issue-10922.yml diff --git a/changelog_unreleased/yaml/17843.md b/changelog_unreleased/yaml/17843.md new file mode 100644 index 000000000..53212ea2e --- /dev/null +++ b/changelog_unreleased/yaml/17843.md @@ -0,0 +1,18 @@ +#### Preserve empty line between map and comment (#17843 by @kovsu) + + +```yaml +# Input +only: issues + +# Comment + +# Prettier stable +only: issues +# Comment + +# Prettier main +only: issues + +# Comment +``` diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index b1f447c0e..8a15b3757 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -211,7 +211,13 @@ function printNode(path, options, print) { separator = [hardline, hardline]; } } else { - separator = hardline; + // Check if we need to preserve empty line before end comments + const lastChild = children.at(-1); + const shouldPreserveEmptyLine = + isNode(lastChild, ["mapping"]) && + isPreviousLineEmpty(options.originalText, locStart(endComments[0])); + + separator = shouldPreserveEmptyLine ? [hardline, hardline] : hardline; } } diff --git a/tests/format/yaml/comment/__snapshots__/format.test.js.snap b/tests/format/yaml/comment/__snapshots__/format.test.js.snap index 6d820c1d7..80c2dbfcb 100644 --- a/tests/format/yaml/comment/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/comment/__snapshots__/format.test.js.snap @@ -253,6 +253,24 @@ printWidth: 80 ================================================================================ `; +exports[`issue-10922.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +foo: bar + +# End Comment - Don't remove previous empty line + +=====================================output===================================== +foo: bar + +# End Comment - Don't remove previous empty line + +================================================================================ +`; + exports[`issue-16074.yml format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/format/yaml/comment/issue-10922.yml b/tests/format/yaml/comment/issue-10922.yml new file mode 100644 index 000000000..0149d4473 --- /dev/null +++ b/tests/format/yaml/comment/issue-10922.yml @@ -0,0 +1,3 @@ +foo: bar + +# End Comment - Don't remove previous empty line