Prevent formatting GraphQL embedded in JS if it contains invalid escape sequences (#4278)
parent
01e8e2bb8a
commit
a92838facf
|
@ -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];
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
`;
|
Loading…
Reference in New Issue