From 1ce5eeea7708508a1c6530d873a9deecd5fcd50c Mon Sep 17 00:00:00 2001 From: Fu Yuchen Date: Tue, 1 Oct 2024 19:54:46 +0800 Subject: [PATCH] Fix yaml comment in non-singleline items missing space before `#` (#16489) Co-authored-by: SUZUKI Sosuke --- changelog_unreleased/yaml/16489.md | 16 +++++++++ src/language-yaml/print/mapping-item.js | 2 ++ .../mapping/__snapshots__/format.test.js.snap | 33 +++++++++++++++++++ .../yaml/mapping/comment-value-align.yml | 2 ++ 4 files changed, 53 insertions(+) create mode 100644 changelog_unreleased/yaml/16489.md create mode 100644 tests/format/yaml/mapping/comment-value-align.yml diff --git a/changelog_unreleased/yaml/16489.md b/changelog_unreleased/yaml/16489.md new file mode 100644 index 000000000..53ab5c14c --- /dev/null +++ b/changelog_unreleased/yaml/16489.md @@ -0,0 +1,16 @@ +#### Fix yaml comment in non-singleline items missing space before `#` (#16489 by @fyc09) + + +```yaml +# Input +123: # hello + # comment + +# Prettier stable +123:# hello + # comment + +# Prettier main +123: # hello + # comment +``` diff --git a/src/language-yaml/print/mapping-item.js b/src/language-yaml/print/mapping-item.js index 1a5be79ad..c9c4b408f 100644 --- a/src/language-yaml/print/mapping-item.js +++ b/src/language-yaml/print/mapping-item.js @@ -112,6 +112,8 @@ function printMappingItem(path, print, options) { implicitMappingValueParts.push(hardline); } else if (value.content) { implicitMappingValueParts.push(line); + } else if (hasTrailingComment(value)) { + implicitMappingValueParts.push(" "); } implicitMappingValueParts.push(printedValue); const implicitMappingValue = alignWithSpaces( diff --git a/tests/format/yaml/mapping/__snapshots__/format.test.js.snap b/tests/format/yaml/mapping/__snapshots__/format.test.js.snap index ffb14a8ab..aed27f30a 100644 --- a/tests/format/yaml/mapping/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/mapping/__snapshots__/format.test.js.snap @@ -320,6 +320,39 @@ printWidth: 80 ================================================================================ `; +exports[`comment-value-align.yml - {"tabWidth":4} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +tabWidth: 4 + | printWidth +=====================================input====================================== +123: # hello + # comment + +=====================================output===================================== +123: # hello + # comment + +================================================================================ +`; + +exports[`comment-value-align.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +123: # hello + # comment + +=====================================output===================================== +123: # hello + # comment + +================================================================================ +`; + exports[`common.yml - {"tabWidth":4} format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/format/yaml/mapping/comment-value-align.yml b/tests/format/yaml/mapping/comment-value-align.yml new file mode 100644 index 000000000..58cfb6489 --- /dev/null +++ b/tests/format/yaml/mapping/comment-value-align.yml @@ -0,0 +1,2 @@ +123: # hello + # comment