From 29c4a3e559c2183dea89109c7274ca69db8008df Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Fri, 22 Apr 2022 14:38:44 +0200 Subject: [PATCH] bugfix(graphql): Fix single line and empty description formatting (#12608) Co-authored-by: Harry Hopkinson <63599884+Harry-Hopkinson@users.noreply.github.com> Co-authored-by: fisker Cheung --- changelog_unreleased/graphql/12608.md | 59 ++++++++++ src/language-graphql/printer-graphql.js | 22 ++-- .../string/__snapshots__/jsfmt.spec.js.snap | 108 ++++++++++++++++++ .../format/graphql/string/description.graphql | 25 ++++ tests/format/graphql/string/string.graphql | 8 ++ 5 files changed, 215 insertions(+), 7 deletions(-) create mode 100644 changelog_unreleased/graphql/12608.md create mode 100644 tests/format/graphql/string/description.graphql diff --git a/changelog_unreleased/graphql/12608.md b/changelog_unreleased/graphql/12608.md new file mode 100644 index 000000000..3e6c2ac4c --- /dev/null +++ b/changelog_unreleased/graphql/12608.md @@ -0,0 +1,59 @@ +#### Fix single line and empty description formatting (#12608 by @chimurai, @fisker) + + +```graphql +# Input +""" Customer """ +type Person { + name: String +} + +"""""" +type Person { + name: String +} + +# Prettier 2.6.2 +""" + Customer +""" +type Person { + name: String +} + +""" + +""" +type Person { + name: String +} + +# Prettier 2.6.2 (second format) +""" +Customer +""" +type Person { + name: String +} + +""" + +""" +type Person { + name: String +} + +# Prettier main +""" +Customer +""" +type Person { + name: String +} + +""" +""" +type Person { + name: String +} +``` diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index e6838813c..a948880bc 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -125,13 +125,15 @@ function genericPrint(path, options, print) { } case "StringValue": { if (node.block) { - return [ - '"""', + const lines = node.value.replace(/"""/g, "\\$&").split("\n"); + if (lines.length === 1) { + lines[0] = lines[0].trim(); + } + + return join( hardline, - join(hardline, node.value.replace(/"""/g, "\\$&").split("\n")), - hardline, - '"""', - ]; + ['"""', ...(lines.length > 0 ? lines : []), '"""'].filter(Boolean) + ); } return [ '"', @@ -572,7 +574,13 @@ function printInterfaces(path, options, print) { return parts; } -function clean(/*node, newNode , parent*/) {} +function clean(node, newNode /* , parent */) { + // We print single line `""" string """` as multiple line string, + // and the parser ignores space in multiple line string + if (node.kind === "StringValue" && node.block && !node.value.includes("\n")) { + newNode.value = newNode.value.trim(); + } +} clean.ignoredProperties = new Set(["loc", "comments"]); function hasPrettierIgnore(path) { diff --git a/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap b/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap index 0ecf89041..c33b4febc 100644 --- a/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/graphql/string/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,82 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`description.graphql format 1`] = ` +====================================options===================================== +parsers: ["graphql"] +printWidth: 80 + | printWidth +=====================================input====================================== +""" Customer """ +type Person {name: String} + +""" Customer """ +type Person {name: String} + +""" + + Customer + + +""" +type Person {name: String} + +"""""" +type Person {name: String} + +""" """ +type Person {name: String} + +""" + 1 + 2 +""" +type Person {name: String} + +=====================================output===================================== +""" +Customer +""" +type Person { + name: String +} + +""" +Customer +""" +type Person { + name: String +} + +""" +Customer +""" +type Person { + name: String +} + +""" +""" +type Person { + name: String +} + +""" +""" +type Person { + name: String +} + +""" + 1 +2 +""" +type Person { + name: String +} + +================================================================================ +`; + exports[`string.graphql format 1`] = ` ====================================options===================================== parsers: ["graphql"] @@ -59,6 +136,14 @@ one: string foo(input: {multiline: "ab\\ncd"}) { id } } +{ + foo(input: {multiline: """ foo """}) { id } +} + +{ + foo(input: {multiline: """ """}) { id } +} + =====================================output===================================== query X($a: Int) @relay(meta: "{\\"lowPri\\": true}") { a @@ -129,5 +214,28 @@ input Input { } } +{ + foo( + input: { + multiline: """ + foo + """ + } + ) { + id + } +} + +{ + foo( + input: { + multiline: """ + """ + } + ) { + id + } +} + ================================================================================ `; diff --git a/tests/format/graphql/string/description.graphql b/tests/format/graphql/string/description.graphql new file mode 100644 index 000000000..c4f44850a --- /dev/null +++ b/tests/format/graphql/string/description.graphql @@ -0,0 +1,25 @@ +""" Customer """ +type Person {name: String} + +""" Customer """ +type Person {name: String} + +""" + + Customer + + +""" +type Person {name: String} + +"""""" +type Person {name: String} + +""" """ +type Person {name: String} + +""" + 1 + 2 +""" +type Person {name: String} diff --git a/tests/format/graphql/string/string.graphql b/tests/format/graphql/string/string.graphql index edf808a7b..d6b8ceb44 100644 --- a/tests/format/graphql/string/string.graphql +++ b/tests/format/graphql/string/string.graphql @@ -50,3 +50,11 @@ one: string { foo(input: {multiline: "ab\ncd"}) { id } } + +{ + foo(input: {multiline: """ foo """}) { id } +} + +{ + foo(input: {multiline: """ """}) { id } +}