Skip to content

Commit

Permalink
Normalize GraphQL multi-line strings (prettier#3632)
Browse files Browse the repository at this point in the history
* Normalize GraphQL multi-line strings

See discussion in prettier#3605 (comment)

* split
  • Loading branch information
vjeux authored Jan 2, 2018
1 parent a627ca7 commit 5356db0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/language-graphql/printer-graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ function genericPrint(path, options, print) {
}
case "StringValue": {
if (n.block) {
// It is not possible to distinguish between """\nabc\n""" and """abc"""
// https://github.com/graphql/graphql-js/issues/1188
return options.originalText.slice(util.locStart(n), util.locEnd(n));
return concat([
'"""',
hardline,
join(hardline, n.value.replace(/"""/g, "\\$&").split("\n")),
hardline,
'"""'
]);
}
return concat(['"', n.value.replace(/["\\]/g, "\\$&"), '"']);
}
Expand Down Expand Up @@ -274,6 +278,8 @@ function genericPrint(path, options, print) {

case "FieldDefinition": {
return concat([
path.call(print, "description"),
n.description ? hardline : "",
path.call(print, "name"),
n.arguments.length > 0
? group(
Expand Down
7 changes: 5 additions & 2 deletions tests/graphql_kitchen_sink/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ fragment frag on Friend {
foo(
size: $size
bar: $b
obj: { key: "value", block: """
obj: {
key: "value"
block: """
block string uses \\"""
""" }
"""
}
)
}
Expand Down
36 changes: 35 additions & 1 deletion tests/graphql_string/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,29 @@ abc
type T {
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}") {
a
}
"""abc"""
"""
abc
"""
type T {
a: Int
}
Expand All @@ -31,4 +48,21 @@ type T {
a: Int
}
"""
a
b
c
"""
type T {
a: Int
}
type Foo {
"""
This is a description
of the \`one\` field.
"""
one: Type
}
`;
15 changes: 15 additions & 0 deletions tests/graphql_string/string.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ abc
type T {
a: Int
}

"""
a
b
c
"""
type T { a: Int }

type Foo {
"""
This is a description
of the `one` field.
"""
one: Type
}

0 comments on commit 5356db0

Please sign in to comment.