Unverified Commit d97237a2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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
parent 3c992063
......@@ -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
}
......
// eslint-disable-next-line no-unused-vars
import { config } from '../../config'
export const getServerSideProps = () => {
return {
props: {},
}
}
export default () => <p>hello world</p>
// eslint-disable-next-line no-unused-vars
export { config as notConfig } from '../../config'
export const getServerSideProps = () => {
return {
props: {},
}
}
export default () => <p>hello world</p>
// 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>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment