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 tagsmaster
parent
a459743eb2
commit
87e109f884
|
@ -107,7 +107,7 @@ function embed(path, print, textToDoc /*, options */) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc) {
|
if (doc) {
|
||||||
doc = escapeBackticks(doc);
|
doc = escapeTemplateCharacters(doc, false);
|
||||||
if (!isFirst && startsWithBlankLine) {
|
if (!isFirst && startsWithBlankLine) {
|
||||||
parts.push("");
|
parts.push("");
|
||||||
}
|
}
|
||||||
|
@ -175,7 +175,7 @@ function embed(path, print, textToDoc /*, options */) {
|
||||||
|
|
||||||
function printMarkdown(text) {
|
function printMarkdown(text) {
|
||||||
const doc = textToDoc(text, { parser: "markdown", __inJsTemplate: true });
|
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];
|
return firstMatchedIndent === null ? "" : firstMatchedIndent[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeBackticks(doc) {
|
function escapeTemplateCharacters(doc, raw) {
|
||||||
return mapDoc(doc, currentDoc => {
|
return mapDoc(doc, currentDoc => {
|
||||||
if (!currentDoc.parts) {
|
if (!currentDoc.parts) {
|
||||||
return currentDoc;
|
return currentDoc;
|
||||||
|
@ -206,7 +206,11 @@ function escapeBackticks(doc) {
|
||||||
|
|
||||||
currentDoc.parts.forEach(part => {
|
currentDoc.parts.forEach(part => {
|
||||||
if (typeof part === "string") {
|
if (typeof part === "string") {
|
||||||
parts.push(part.replace(/(\\*)`/g, "$1$1\\`"));
|
parts.push(
|
||||||
|
raw
|
||||||
|
? part.replace(/(\\*)`/g, "$1$1\\`")
|
||||||
|
: part.replace(/([\\`]|\$\{)/g, "\\$1")
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
parts.push(part);
|
parts.push(part);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,5 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// 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`] = `
|
exports[`comment-tag.js - babylon-verify 1`] = `
|
||||||
const query = /* GraphQL */\`
|
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`] = `
|
exports[`expressions.js - babylon-verify 1`] = `
|
||||||
graphql(schema, \`
|
graphql(schema, \`
|
||||||
query allPartsByManufacturerName($name: String!) {
|
query allPartsByManufacturerName($name: String!) {
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
gql`
|
|
||||||
"\`foo\` mutation payload."
|
|
||||||
type FooPayload {
|
|
||||||
bar: String
|
|
||||||
}
|
|
||||||
`
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
`
|
|
@ -79,6 +79,7 @@ markdown\`
|
||||||
markdown\`
|
markdown\`
|
||||||
- \\\`
|
- \\\`
|
||||||
- \\\\\\\`
|
- \\\\\\\`
|
||||||
|
- \\\\ a
|
||||||
- \\\\\\\\
|
- \\\\\\\\
|
||||||
- \\$
|
- \\$
|
||||||
- \\u1234
|
- \\u1234
|
||||||
|
@ -91,6 +92,7 @@ markdown\`
|
||||||
markdown\`
|
markdown\`
|
||||||
- \\\`
|
- \\\`
|
||||||
- \\\\\\\`
|
- \\\\\\\`
|
||||||
|
- \\\\ a
|
||||||
- \\\\\\\\
|
- \\\\\\\\
|
||||||
- \\\\$
|
- \\\\$
|
||||||
- \\u1234
|
- \\u1234
|
||||||
|
|
|
@ -7,6 +7,7 @@ markdown`
|
||||||
markdown`
|
markdown`
|
||||||
- \`
|
- \`
|
||||||
- \\\`
|
- \\\`
|
||||||
|
- \\ a
|
||||||
- \\\\
|
- \\\\
|
||||||
- \$
|
- \$
|
||||||
- \u1234
|
- \u1234
|
||||||
|
|
Loading…
Reference in New Issue