Normalize GraphQL multi-line strings (#3632)

* Normalize GraphQL multi-line strings

See discussion in https://github.com/prettier/prettier/pull/3605#discussion_r159120321

* split
master
Christopher Chedeau 2018-01-02 11:55:53 -08:00 committed by GitHub
parent a627ca7b5d
commit 5356db0e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 6 deletions

View File

@ -126,9 +126,13 @@ function genericPrint(path, options, print) {
} }
case "StringValue": { case "StringValue": {
if (n.block) { if (n.block) {
// It is not possible to distinguish between """\nabc\n""" and """abc""" return concat([
// https://github.com/graphql/graphql-js/issues/1188 '"""',
return options.originalText.slice(util.locStart(n), util.locEnd(n)); hardline,
join(hardline, n.value.replace(/"""/g, "\\$&").split("\n")),
hardline,
'"""'
]);
} }
return concat(['"', n.value.replace(/["\\]/g, "\\$&"), '"']); return concat(['"', n.value.replace(/["\\]/g, "\\$&"), '"']);
} }
@ -274,6 +278,8 @@ function genericPrint(path, options, print) {
case "FieldDefinition": { case "FieldDefinition": {
return concat([ return concat([
path.call(print, "description"),
n.description ? hardline : "",
path.call(print, "name"), path.call(print, "name"),
n.arguments.length > 0 n.arguments.length > 0
? group( ? group(

View File

@ -110,9 +110,12 @@ fragment frag on Friend {
foo( foo(
size: $size size: $size
bar: $b bar: $b
obj: { key: "value", block: """ obj: {
key: "value"
block: """
block string uses \\""" block string uses \\"""
""" } """
}
) )
} }

View File

@ -14,12 +14,29 @@ abc
type T { type T {
a: Int a: Int
} }
"""
a
b
c
"""
type T { a: Int }
type Foo {
"""
This is a description
of the \`one\` field.
"""
one: Type
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
query X($a: Int) @relay(meta: "{\\"lowPri\\": true}") { query X($a: Int) @relay(meta: "{\\"lowPri\\": true}") {
a a
} }
"""abc""" """
abc
"""
type T { type T {
a: Int a: Int
} }
@ -31,4 +48,21 @@ type T {
a: Int a: Int
} }
"""
a
b
c
"""
type T {
a: Int
}
type Foo {
"""
This is a description
of the \`one\` field.
"""
one: Type
}
`; `;

View File

@ -11,3 +11,18 @@ abc
type T { type T {
a: Int a: Int
} }
"""
a
b
c
"""
type T { a: Int }
type Foo {
"""
This is a description
of the `one` field.
"""
one: Type
}