From 91a5b912c8888fb1ac7436b52f12cc6d41ac15ce Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Wed, 15 Jun 2022 21:07:07 +0000 Subject: [PATCH 1/6] bugfix(graphql): fix between empty lines in description --- changelog_unreleased/graphql/13013.md | 34 +++++++++++++++++++ src/language-graphql/printer-graphql.js | 15 +++++--- .../string/__snapshots__/jsfmt.spec.js.snap | 16 +++++++++ .../format/graphql/string/description.graphql | 7 ++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 changelog_unreleased/graphql/13013.md diff --git a/changelog_unreleased/graphql/13013.md b/changelog_unreleased/graphql/13013.md new file mode 100644 index 000000000000..611aebd73bde --- /dev/null +++ b/changelog_unreleased/graphql/13013.md @@ -0,0 +1,34 @@ +#### Fix between 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..2e03da1d2b9b 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -125,15 +125,20 @@ function genericPrint(path, options, print) { } case "StringValue": { if (node.block) { - const lines = node.value.replace(/"""/g, "\\$&").split("\n"); + let lines = node.value.replace(/"""/g, "\\$&").split("\n"); if (lines.length === 1) { lines[0] = lines[0].trim(); } - return join( - hardline, - ['"""', ...(lines.length > 0 ? lines : []), '"""'].filter(Boolean) - ); + if (lines.every((line) => line === "")) { + lines = lines.filter(Boolean); + } + + return join(hardline, [ + '"""', + ...(lines.length > 0 ? 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..9608b9d1b6ee 100644 --- a/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap @@ -32,6 +32,13 @@ type Person {name: String} """ type Person {name: String} +""" +First line + +Second Line +""" +type Person {name: String} + =====================================output===================================== """ Customer @@ -74,6 +81,15 @@ 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..538f6e716cab 100644 --- a/tests/format/graphql/string/description.graphql +++ b/tests/format/graphql/string/description.graphql @@ -23,3 +23,10 @@ type Person {name: String} 2 """ type Person {name: String} + +""" +First line + +Second Line +""" +type Person {name: String} From 3eef913bb90f7b9db4fd453cf37a77797db1ea3b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 16 Jun 2022 08:47:41 +0800 Subject: [PATCH 2/6] Simply logic --- src/language-graphql/printer-graphql.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 2e03da1d2b9b..cbed5a6f3491 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -125,13 +125,13 @@ function genericPrint(path, options, print) { } case "StringValue": { if (node.block) { - let lines = node.value.replace(/"""/g, "\\$&").split("\n"); + const lines = node.value.replace(/"""/g, "\\$&").split("\n"); if (lines.length === 1) { lines[0] = lines[0].trim(); } if (lines.every((line) => line === "")) { - lines = lines.filter(Boolean); + lines.length = 0; } return join(hardline, [ From bb6345ac0f4483747fcd15d2f02abddfe1f25ba5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 16 Jun 2022 09:06:52 +0800 Subject: [PATCH 3/6] Remove unnecessary ternary --- src/language-graphql/printer-graphql.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index cbed5a6f3491..be0608ed2545 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -134,11 +134,7 @@ function genericPrint(path, options, print) { lines.length = 0; } - return join(hardline, [ - '"""', - ...(lines.length > 0 ? lines : []), - '"""', - ]); + return join(hardline, ['"""', ...lines, '"""', ]); } return [ '"', From 52be6bd5d79ea2bbfa622756b519adca920340d5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 16 Jun 2022 09:29:25 +0800 Subject: [PATCH 4/6] Style --- src/language-graphql/printer-graphql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index be0608ed2545..2b2a7515eea5 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -134,7 +134,7 @@ function genericPrint(path, options, print) { lines.length = 0; } - return join(hardline, ['"""', ...lines, '"""', ]); + return join(hardline, ['"""', ...lines, '"""']); } return [ '"', From f9a6dd1cbaff8645d4a7f52c16deb6cb6170ee27 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 16 Jun 2022 13:01:30 +0800 Subject: [PATCH 5/6] Fix changelog --- changelog_unreleased/graphql/13013.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/graphql/13013.md b/changelog_unreleased/graphql/13013.md index 611aebd73bde..d36d6bfd7fd5 100644 --- a/changelog_unreleased/graphql/13013.md +++ b/changelog_unreleased/graphql/13013.md @@ -1,4 +1,4 @@ -#### Fix between empty lines in description (#13013 by @chimurai) +#### Keep useful empty lines in description (#13013 by @chimurai) ```graphql From d7d92d17bfa08b20faf3cd79780f18a642d342c7 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 16 Jun 2022 13:10:26 +0800 Subject: [PATCH 6/6] Add more tests --- .../string/__snapshots__/jsfmt.spec.js.snap | 81 +++++++++++++++++++ .../format/graphql/string/description.graphql | 42 ++++++++++ 2 files changed, 123 insertions(+) diff --git a/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap b/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap index 9608b9d1b6ee..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,30 @@ 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 @@ -39,6 +63,24 @@ Second Line """ type Person {name: String} +""" + + +First line + +Second Line +""" +type Person {name: String} + +""" +First line + +Second Line + + +""" +type Person {name: String} + =====================================output===================================== """ Customer @@ -81,6 +123,45 @@ 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 diff --git a/tests/format/graphql/string/description.graphql b/tests/format/graphql/string/description.graphql index 538f6e716cab..977a5a80abfd 100644 --- a/tests/format/graphql/string/description.graphql +++ b/tests/format/graphql/string/description.graphql @@ -24,9 +24,51 @@ 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}