Escape not just backticks but `${` as well in graphql tags (#5137)

* Escape not just backticks but `${` as well in graphql tags

Fixes #4974.

* Also escape backslashes correctly in graphql tags
master
Simon Lydell 2018-09-29 09:42:44 +02:00 committed by GitHub
parent a459743eb2
commit 87e109f884
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 139 additions and 27 deletions

View File

@ -107,7 +107,7 @@ function embed(path, print, textToDoc /*, options */) {
}
if (doc) {
doc = escapeBackticks(doc);
doc = escapeTemplateCharacters(doc, false);
if (!isFirst && startsWithBlankLine) {
parts.push("");
}
@ -175,7 +175,7 @@ function embed(path, print, textToDoc /*, options */) {
function printMarkdown(text) {
const doc = textToDoc(text, { parser: "markdown", __inJsTemplate: true });
return stripTrailingHardline(escapeBackticks(doc));
return stripTrailingHardline(escapeTemplateCharacters(doc, true));
}
}
@ -196,7 +196,7 @@ function getIndentation(str) {
return firstMatchedIndent === null ? "" : firstMatchedIndent[1];
}
function escapeBackticks(doc) {
function escapeTemplateCharacters(doc, raw) {
return mapDoc(doc, currentDoc => {
if (!currentDoc.parts) {
return currentDoc;
@ -206,7 +206,11 @@ function escapeBackticks(doc) {
currentDoc.parts.forEach(part => {
if (typeof part === "string") {
parts.push(part.replace(/(\\*)`/g, "$1$1\\`"));
parts.push(
raw
? part.replace(/(\\*)`/g, "$1$1\\`")
: part.replace(/([\\`]|\$\{)/g, "\\$1")
);
} else {
parts.push(part);
}

View File

@ -1,22 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`backticks.js - babylon-verify 1`] = `
gql\`
"\\\`foo\\\` mutation payload."
type FooPayload {
bar: String
}
\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gql\`
"\\\`foo\\\` mutation payload."
type FooPayload {
bar: String
}
\`;
`;
exports[`comment-tag.js - babylon-verify 1`] = `
const query = /* GraphQL */\`
{
@ -63,6 +46,93 @@ graphql\`
`;
exports[`escape.js - babylon-verify 1`] = `
gql\`
"\\\`foo\\\` mutation payload."
type FooPayload {
bar: String
}
\`
gql\`
type Project {
"Pattern: \\\`\\\${project}\\\`"
pattern: String
"""
Pattern: \\\`\\\${project}\\\`
"""
pattern: String
# Also: Escaping the first parentheses...
"Pattern: \\\`$\\{project}\\\`"
pattern: String
# Or escaping the first and second parentheses...
"Pattern: \\\`$\\{project\\}\\\`"
pattern: String
}
\`
gql\`
"""
- \\\`
- \\\\\\\`
- \\\\ a
- \\\\\\\\
- $
- \\$
- \\\${
- \\\\\\\${
- \\u1234
"""
type A {
a
}
\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gql\`
"\\\`foo\\\` mutation payload."
type FooPayload {
bar: String
}
\`;
gql\`
type Project {
"Pattern: \\\`\\\${project}\\\`"
pattern: String
"""
Pattern: \\\`\\\${project}\\\`
"""
pattern: String
# Also: Escaping the first parentheses...
"Pattern: \\\`\\\${project}\\\`"
pattern: String
# Or escaping the first and second parentheses...
"Pattern: \\\`\\\${project}\\\`"
pattern: String
}
\`;
gql\`
"""
- \\\`
- \\\\\\\`
- \\\\ a
- \\\\\\\\
- $
- \\$
- \\\${
- \\\\\\\${
- \\u1234
"""
type A {
a
}
\`;
`;
exports[`expressions.js - babylon-verify 1`] = `
graphql(schema, \`
query allPartsByManufacturerName($name: String!) {

View File

@ -1,6 +0,0 @@
gql`
"\`foo\` mutation payload."
type FooPayload {
bar: String
}
`

View File

@ -0,0 +1,41 @@
gql`
"\`foo\` mutation payload."
type FooPayload {
bar: String
}
`
gql`
type Project {
"Pattern: \`\${project}\`"
pattern: String
"""
Pattern: \`\${project}\`
"""
pattern: String
# Also: Escaping the first parentheses...
"Pattern: \`$\{project}\`"
pattern: String
# Or escaping the first and second parentheses...
"Pattern: \`$\{project\}\`"
pattern: String
}
`
gql`
"""
- \`
- \\\`
- \\ a
- \\\\
- $
- \$
- \${
- \\\${
- \u1234
"""
type A {
a
}
`

View File

@ -79,6 +79,7 @@ markdown\`
markdown\`
- \\\`
- \\\\\\\`
- \\\\ a
- \\\\\\\\
- \\$
- \\u1234
@ -91,6 +92,7 @@ markdown\`
markdown\`
- \\\`
- \\\\\\\`
- \\\\ a
- \\\\\\\\
- \\\\$
- \\u1234

View File

@ -7,6 +7,7 @@ markdown`
markdown`
- \`
- \\\`
- \\ a
- \\\\
- \$
- \u1234