Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix bad global check for graphql tag #6075

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ exports[`Transforms queries in page components 1`] = `"export const query = \\"3

exports[`allows the global tag 1`] = `"export const query = \\"3687030656\\";"`;

exports[`distinguishes between the right tags 1`] = `
"const foo = styled('div')\`
{
\${foo}
}
\`;
const pulse = keyframes\`
0% {
transform: scale(1);
animation-timing-function: ease-in;
}
25% {
animation-timing-function: ease-out;
transform: scale(1.05);
}
50% {
transform: scale(1.12);
animation-timing-function: ease-in;
}
to {
transform: scale(1);
animation-timing-function: ease-out;
}
\`;
export const query = \\"3687030656\\";"
`;

exports[`handles import aliasing 1`] = `"export const query = \\"3687030656\\";"`;

exports[`handles require 1`] = `"export const query = \\"3687030656\\";"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ it(`allows the global tag`, () => {
)
})

it(`distinguishes between the right tags`, () => {
matchesSnapshot(
`
const foo = styled('div')\`
{
$\{foo}
}
\`

const pulse = keyframes\`
0% {
transform: scale(1);
animation-timing-function: ease-in;
}
25% {
animation-timing-function: ease-out;
transform: scale(1.05);
}
50% {
transform: scale(1.12);
animation-timing-function: ease-in;
}
to {
transform: scale(1);
animation-timing-function: ease-out;
}
\`;


export const query = graphql\`
{
site { siteMetadata { title }}
}
\`
`
)
})

it(`handles import aliasing`, () => {
matchesSnapshot(
`
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-plugin-remove-graphql-queries/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const graphql = require(`gatsby/graphql`)
const murmurhash = require(`./murmur`)
const nodePath = require(`path`)

const isGlobalIdentifier = tag =>
tag.isIdentifier({ name: `graphql` }) && tag.scope.hasGlobal(`graphql`)

function getTagImport(tag) {
const name = tag.node.name
const binding = tag.scope.getBinding(name)
Expand Down Expand Up @@ -41,10 +44,7 @@ function isGraphqlTag(tag) {
const identifier = isExpression ? tag.get(`object`) : tag

const importPath = getTagImport(identifier)
if (!importPath)
return (
tag.scope.hasGlobal(`graphql`) && tag.isIdentifier({ name: `graphql` })
)
if (!importPath) return isGlobalIdentifier(tag)

if (
isExpression &&
Expand Down Expand Up @@ -87,7 +87,7 @@ function removeImport(tag) {

function getGraphQLTag(path) {
const tag = path.get(`tag`)
const isGlobal = tag.scope.hasGlobal(`graphql`)
const isGlobal = isGlobalIdentifier(tag)

if (!isGlobal && !isGraphqlTag(tag)) return {}

Expand Down