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