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:
parent
3c99206313
commit
d97237a292
4 changed files with 66 additions and 7 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
10
test/integration/page-config/pages/valid/config-import.js
Normal file
10
test/integration/page-config/pages/valid/config-import.js
Normal 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>
|
|
@ -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>
|
|
@ -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>
|
Loading…
Reference in a new issue