diff --git a/changelog_unreleased/graphql/13013.md b/changelog_unreleased/graphql/13013.md new file mode 100644 index 000000000000..d36d6bfd7fd5 --- /dev/null +++ b/changelog_unreleased/graphql/13013.md @@ -0,0 +1,34 @@ +#### Keep useful empty lines in description (#13013 by @chimurai) + + +```graphql +# Input +""" +First line + +Second Line +""" +type Person { + name: String +} + +# Prettier 2.7.0 +""" +First line +Second Line +""" +type Person { + name: String +} + + +# Prettier main +""" +First line + +Second Line +""" +type Person { + name: String +} +``` diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index fb70c360ad51..2b2a7515eea5 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -130,10 +130,11 @@ function genericPrint(path, options, print) { lines[0] = lines[0].trim(); } - return join( - hardline, - ['"""', ...(lines.length > 0 ? lines : []), '"""'].filter(Boolean) - ); + if (lines.every((line) => line === "")) { + lines.length = 0; + } + + return join(hardline, ['"""', ...lines, '"""']); } return [ '"', diff --git a/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap b/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap index c33b4febc16d..237af44f5481 100644 --- a/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap @@ -32,6 +32,55 @@ type Person {name: String} """ type Person {name: String} + +""" + + +Empty lines before +""" +type Person {name: String} + +""" +Empty lines after + + +""" +type Person {name: String} + +""" + + +Empty lines around + + +""" +type Person {name: String} + +""" +First line + +Second Line +""" +type Person {name: String} + +""" + + +First line + +Second Line +""" +type Person {name: String} + +""" +First line + +Second Line + + +""" +type Person {name: String} + =====================================output===================================== """ Customer @@ -74,6 +123,54 @@ type Person { name: String } +""" +Empty lines before +""" +type Person { + name: String +} + +""" +Empty lines after +""" +type Person { + name: String +} + +""" +Empty lines around +""" +type Person { + name: String +} + +""" +First line + +Second Line +""" +type Person { + name: String +} + +""" +First line + +Second Line +""" +type Person { + name: String +} + +""" +First line + +Second Line +""" +type Person { + name: String +} + ================================================================================ `; diff --git a/tests/format/graphql/string/description.graphql b/tests/format/graphql/string/description.graphql index c4f44850a855..977a5a80abfd 100644 --- a/tests/format/graphql/string/description.graphql +++ b/tests/format/graphql/string/description.graphql @@ -23,3 +23,52 @@ type Person {name: String} 2 """ type Person {name: String} + + +""" + + +Empty lines before +""" +type Person {name: String} + +""" +Empty lines after + + +""" +type Person {name: String} + +""" + + +Empty lines around + + +""" +type Person {name: String} + +""" +First line + +Second Line +""" +type Person {name: String} + +""" + + +First line + +Second Line +""" +type Person {name: String} + +""" +First line + +Second Line + + +""" +type Person {name: String}