Skip to content

Commit

Permalink
fix(gatsby): Allow export { default } named exports in page templat…
Browse files Browse the repository at this point in the history
…es (#29553)

Co-authored-by: Lennart <[email protected]>
  • Loading branch information
yanneves and LekoArts authored Jul 22, 2022
1 parent b6d9784 commit a558313
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ describe(`no-anonymous-exports-page-templates`, () => {
test({
code: `import { graphql } from "gatsby"\nconst Template = () => {}\nconst query = graphql\`test\`\nexport { query }\nexport default Template`,
}),
test({
code: `import { graphql } from "gatsby"\nimport Template from './Template'\nconst query = graphql\`test\`\nexport { query }\nexport default Template`,
}),
test({
code: `import { graphql } from "gatsby"\nexport { default } from './Template'\nexport const query = graphql\`test\``,
}),
test({
code: `import { graphql } from "gatsby"\nconst query = graphql\`test\`\nexport { query }\nexport { default } from './Template'`,
}),
test({
code: `import { graphql } from "gatsby"\nexport { Template as default } from './Template'\nexport const query = graphql\`test\``,
}),
// getServerData
test({
code: `import { graphql, Link } from "gatsby"\nconst Template = () => {}\nexport const query = graphql\`test\`\nexport default Template\nexport function getServerData() { return { props: { foo: "bar" }}}`,
Expand Down Expand Up @@ -121,6 +133,18 @@ describe(`no-anonymous-exports-page-templates`, () => {
code: `import { graphql } from "gatsby"\nconst Template = () => {}\nexport const hello = 10, query = graphql\`test\`\nexport default Template`,
errors: [{ messageId: `limitedExportsPageTemplates` }],
}),
test({
code: `import { graphql } from "gatsby"\nexport { default } from './Template'\nexport const hello = 10, query = graphql\`test\``,
errors: [{ messageId: `limitedExportsPageTemplates` }],
}),
test({
code: `import { graphql } from "gatsby"\nconst query = graphql\`test\`\nexport { query }\nexport { default } from './Template'\nexport function Test() {}`,
errors: [{ messageId: `limitedExportsPageTemplates` }],
}),
test({
code: `import { graphql } from "gatsby"\nexport { Template as default } from './Template'\nexport const query = graphql\`test\`\nexport function Test() {}`,
errors: [{ messageId: `limitedExportsPageTemplates` }],
}),
test({
code: `import { graphql, Link } from "gatsby"\nconst Template = () => {}\nexport const query = graphql\`test\`\nexport default Template\nexport class NotHead extends React.Component { render() { return null } }`,
errors: [{ messageId: `limitedExportsPageTemplates` }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ function hasOneValidNamedDeclaration(
// Checks for:
// const query = graphql``
// export { query }
if (
node.type === `ExportNamedDeclaration` &&
node.declaration === null &&
varName
) {
if (node.type === `ExportNamedDeclaration` && node.declaration === null) {
// For export { foobar, query } the declaration will be null and specifiers exists
// For { foobar, query } it'll return true, for { query } it'll return false
const nonQueryExports = node.specifiers.some(
e => e.exported.name !== varName
// It will ignore any { default } declarations since these are allowed
const nonQueryExports = node.specifiers.some(e =>
varName
? e.exported.name !== varName && e.exported.name !== `default`
: e.exported.name !== `default`
)
return !nonQueryExports
}
Expand Down

0 comments on commit a558313

Please sign in to comment.