diff --git a/changelog_unreleased/yaml/16074.md b/changelog_unreleased/yaml/16074.md new file mode 100644 index 000000000..b091fc1ac --- /dev/null +++ b/changelog_unreleased/yaml/16074.md @@ -0,0 +1,27 @@ +#### Do not add line break before empty map or sequence (#16074 by @BapRx) + + +```yaml +# Input +--- +myDict: {} + # comment +myList: [] + # comment + +# Prettier stable +--- +myDict: + {} + # comment +myList: + [] + # comment + +# Prettier main +--- +myDict: {} + # comment +myList: [] + # comment +``` diff --git a/src/language-yaml/print/mapping-item.js b/src/language-yaml/print/mapping-item.js index b04c88d7a..42760c3b7 100644 --- a/src/language-yaml/print/mapping-item.js +++ b/src/language-yaml/print/mapping-item.js @@ -98,6 +98,13 @@ function printMappingItem(path, options, print) { // as part of the mapping value const implicitMappingValueParts = [spaceBeforeColon, ":"]; if ( + hasEndComments(value) && + value.content && + isNode(value.content, ["flowMapping", "flowSequence"]) && + value.content.children.length === 0 + ) { + implicitMappingValueParts.push(" "); + } else if ( hasLeadingComments(value.content) || (hasEndComments(value) && value.content && diff --git a/tests/format/yaml/comment/__snapshots__/format.test.js.snap b/tests/format/yaml/comment/__snapshots__/format.test.js.snap index db44b1163..c30efb37f 100644 --- a/tests/format/yaml/comment/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/comment/__snapshots__/format.test.js.snap @@ -253,6 +253,54 @@ printWidth: 80 ================================================================================ `; +exports[`issue-16074.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +--- +myDict: {} + # exampleKey1: value + # exampleKey2: value + +myList: [] + # - name: example + # value: example + +myOtherMap: + 1: example + 2: example + # 3: example + +myOtherList: + - example1 + - example2 + # - example3 + +=====================================output===================================== +--- +myDict: {} + # exampleKey1: value + # exampleKey2: value + +myList: [] + # - name: example + # value: example + +myOtherMap: + 1: example + 2: example + # 3: example + +myOtherList: + - example1 + - example2 + # - example3 + +================================================================================ +`; + exports[`map.yml format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/format/yaml/comment/issue-16074.yml b/tests/format/yaml/comment/issue-16074.yml new file mode 100644 index 000000000..a7ec10f96 --- /dev/null +++ b/tests/format/yaml/comment/issue-16074.yml @@ -0,0 +1,18 @@ +--- +myDict: {} + # exampleKey1: value + # exampleKey2: value + +myList: [] + # - name: example + # value: example + +myOtherMap: + 1: example + 2: example + # 3: example + +myOtherList: + - example1 + - example2 + # - example3