Fix invalid config export errors (#16834)

This corrects detecting of invalid page config exports and adds additional test cases for non-invalid config import/exports

Closes: https://github.com/vercel/next.js/issues/16775
This commit is contained in:
JJ Kasper 2020-09-04 08:19:12 -05:00 committed by GitHub
parent 3c99206313
commit d97237a292
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 7 deletions

View file

@ -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
}

View file

@ -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>

View file

@ -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>

View file

@ -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>