diff --git a/changelog_unreleased/yaml/pr-8356.md b/changelog_unreleased/yaml/pr-8356.md new file mode 100644 index 000000000..9d5d03791 --- /dev/null +++ b/changelog_unreleased/yaml/pr-8356.md @@ -0,0 +1,54 @@ +#### Preserve blank lines in comments ([#8356](https://github.com/prettier/prettier/pull/8356) by [@fisker](https://github.com/fisker)) + + +```yaml +# Input +a: + - a: a + + # - b: b + + # - c: c + + - d: d + + +b: + - a: a + + # - b: b + + # - c: c + +# Prettier stable +a: + - a: a + + # - b: b + + # - c: c + + - d: d + +b: + - a: a + # - b: b + # - c: c + +// Prettier master +a: + - a: a + + # - b: b + + # - c: c + + - d: d + +b: + - a: a + + # - b: b + + # - c: c +``` diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 70f7ff43f..4269f77c6 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -38,7 +38,7 @@ const { markAsRoot, softline, } = docBuilders; -const { replaceEndOfLineWith } = require("../common/util"); +const { replaceEndOfLineWith, isPreviousLineEmpty } = require("../common/util"); function preprocess(ast) { return mapNode(ast, defineShortcuts); @@ -131,7 +131,26 @@ function genericPrint(path, options, print) { hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"]) ? align( node.type === "sequenceItem" ? 2 : 0, - concat([hardline, join(hardline, path.map(print, "endComments"))]) + concat([ + hardline, + join( + hardline, + path.map( + (path) => + concat([ + isPreviousLineEmpty( + options.originalText, + path.getValue(), + options.locStart + ) + ? hardline + : "", + print(path), + ]), + "endComments" + ) + ), + ]) ) : "", ]); diff --git a/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap index 927245e6b..b4c7f45e9 100644 --- a/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap @@ -123,6 +123,151 @@ a: ================================================================================ `; +exports[`map.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +foo1: + - foo: item1 + bar: item1 + + # - foo: item2 + # bar: item2 + + # - foo: item3 + # bar: item3 + + - foo: item4 + bar: item4 + +foo2: + - foo: item11 + bar: item11 + + # - foo: item22 + # bar: item22 + + # - foo: item33 + # bar: item33 + +=====================================output===================================== +foo1: + - foo: item1 + bar: item1 + + # - foo: item2 + # bar: item2 + + # - foo: item3 + # bar: item3 + + - foo: item4 + bar: item4 + +foo2: + - foo: item11 + bar: item11 + + # - foo: item22 + # bar: item22 + + # - foo: item33 + # bar: item33 + +================================================================================ +`; + +exports[`map-2.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +foo1: + - foo + + # - foo + + # - foo + + - foo + +foo2: + - foo2 + + # - foo2 + + + + + + + # - foo2 + # - foo2 + +=====================================output===================================== +foo1: + - foo + + # - foo + + # - foo + + - foo + +foo2: + - foo2 + + # - foo2 + + # - foo2 + # - foo2 + +================================================================================ +`; + +exports[`map-3.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +foo1: + - foo + + # - foo + + # - foo + + - foo + +foo2: + - foo2 + + # first line + # next line + +=====================================output===================================== +foo1: + - foo + + # - foo + + # - foo + + - foo + +foo2: + - foo2 + + # first line + # next line + +================================================================================ +`; + exports[`root.yml format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/yaml/comment/map-2.yml b/tests/yaml/comment/map-2.yml new file mode 100644 index 000000000..eb048fc4e --- /dev/null +++ b/tests/yaml/comment/map-2.yml @@ -0,0 +1,21 @@ +foo1: + - foo + + # - foo + + # - foo + + - foo + +foo2: + - foo2 + + # - foo2 + + + + + + + # - foo2 + # - foo2 diff --git a/tests/yaml/comment/map-3.yml b/tests/yaml/comment/map-3.yml new file mode 100644 index 000000000..a98808ef3 --- /dev/null +++ b/tests/yaml/comment/map-3.yml @@ -0,0 +1,14 @@ +foo1: + - foo + + # - foo + + # - foo + + - foo + +foo2: + - foo2 + + # first line + # next line diff --git a/tests/yaml/comment/map.yml b/tests/yaml/comment/map.yml new file mode 100644 index 000000000..213f696fb --- /dev/null +++ b/tests/yaml/comment/map.yml @@ -0,0 +1,22 @@ +foo1: + - foo: item1 + bar: item1 + + # - foo: item2 + # bar: item2 + + # - foo: item3 + # bar: item3 + + - foo: item4 + bar: item4 + +foo2: + - foo: item11 + bar: item11 + + # - foo: item22 + # bar: item22 + + # - foo: item33 + # bar: item33