Prevent formatting GraphQL embedded in JS if it contains invalid escape sequences (#4278)

master
Lucas Duailibe 2018-04-07 12:03:56 -03:00 committed by GitHub
parent 01e8e2bb8a
commit a92838facf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 0 deletions

View File

@ -78,6 +78,13 @@ function embed(path, print, textToDoc /*, options */) {
const isFirst = i === 0;
const isLast = i === numQuasis - 1;
const text = templateElement.value.cooked;
// Bail out if any of the quasis have an invalid escape sequence
// (which would make the `cooked` value be `null` or `undefined`)
if (typeof text !== "string") {
return null;
}
const lines = text.split("\n");
const numLines = lines.length;
const expressionDoc = expressionDocs[i];

View File

@ -409,6 +409,55 @@ gql\`
`;
exports[`invalid.js 1`] = `
// none of the embedded GraphQL should be formatted
// for they have an invalid escape sequence
gql\`
"\\x"
type Foo {
a: string
}
\`;
gql\`
type Foo {
a: string
}
\${stuff}
"\\x"
type Bar {
b : string
}
\`;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// none of the embedded GraphQL should be formatted
// for they have an invalid escape sequence
gql\`
"\\x"
type Foo {
a: string
}
\`;
gql\`
type Foo {
a: string
}
\${stuff}
"\\x"
type Bar {
b : string
}
\`;
`;
exports[`react-relay.js 1`] = `
const { graphql } = require("react-relay");

View File

@ -0,0 +1,22 @@
// none of the embedded GraphQL should be formatted
// for they have an invalid escape sequence
gql`
"\x"
type Foo {
a: string
}
`;
gql`
type Foo {
a: string
}
${stuff}
"\x"
type Bar {
b : string
}
`;