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 <lionkay@gmail.com>
This commit is contained in:
authored and GitHub committed 2022-04-22 20:38:44 +08:00
1 parent 56b3c6c7e4
commit 29c4a3e559
5 files changed
+215 -7

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
#### Fix single line and empty description formatting (#12608 by @chimurai, @fisker)
<!-- prettier-ignore -->
```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
}
```
+15 -7
View File
@@ -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) {
@@ -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
}
}
================================================================================
`;
@@ -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}
@@ -50,3 +50,11 @@ one: string
{
foo(input: {multiline: "ab\ncd"}) { id }
}
{
foo(input: {multiline: """ foo """}) { id }
}
{
foo(input: {multiline: """ """}) { id }
}