Skip to content

Commit

Permalink
Fix invalid config export errors (#16834)
Browse files Browse the repository at this point in the history
This corrects detecting of invalid page config exports and adds additional test cases for non-invalid config import/exports

Closes: #16775
  • Loading branch information
ijjk authored Sep 4, 2020
1 parent 3c99206 commit d97237a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
41 changes: 34 additions & 7 deletions packages/next/build/babel/plugins/next-page-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,47 @@ export default function nextPageConfig({
exportPath.scope.getBinding(CONFIG_KEY)?.path.node,
].filter(Boolean)

for (const declaration of declarations) {
if (
!BabelTypes.isIdentifier(declaration.id, {
name: CONFIG_KEY,
})
) {
if (BabelTypes.isImportSpecifier(declaration)) {
for (const specifier of exportPath.node.specifiers) {
if (specifier.exported.name === CONFIG_KEY) {
// export {} from 'somewhere'
if (BabelTypes.isStringLiteral(exportPath.node.source)) {
throw new Error(
errorMessage(
exportState,
`Expected object but got import`
)
)
// import hello from 'world'
// export { hello as config }
} else if (
BabelTypes.isIdentifier(
(specifier as BabelTypes.ExportSpecifier).local
)
) {
if (
BabelTypes.isImportSpecifier(
exportPath.scope.getBinding(
(specifier as BabelTypes.ExportSpecifier).local.name
)?.path.node
)
) {
throw new Error(
errorMessage(
exportState,
`Expected object but got import`
)
)
}
}
}
}

for (const declaration of declarations) {
if (
!BabelTypes.isIdentifier(declaration.id, {
name: CONFIG_KEY,
})
) {
continue
}

Expand Down
10 changes: 10 additions & 0 deletions test/integration/page-config/pages/valid/config-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line no-unused-vars
import { config } from '../../config'

export const getServerSideProps = () => {
return {
props: {},
}
}

export default () => <p>hello world</p>
10 changes: 10 additions & 0 deletions test/integration/page-config/pages/valid/not-config-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line no-unused-vars
export { config as notConfig } from '../../config'

export const getServerSideProps = () => {
return {
props: {},
}
}

export default () => <p>hello world</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line no-unused-vars
import { config } from '../../config'

export { config as notConfig }

export const getServerSideProps = () => {
return {
props: {},
}
}

export default () => <p>hello world</p>

0 comments on commit d97237a

Please sign in to comment.