Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions __tests__/repro_issue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { parseCode, hasExportName } from '../src/utils'

// Related with https://github.com/aralroca/next-translate-plugin/issues/91
describe('repro issue', () => {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the ID of the issue: #91

it('should detect re-exported getStaticProps', () => {
const code = `
export { getStaticProps } from './data'
export default function Page() {
return <h1>Page</h1>
}
`
const pagePkg = parseCode('jsx', code)
expect(hasExportName(pagePkg, 'getStaticProps')).toBe(true)
})

it('should detect re-exported getServerSideProps', () => {
const code = `
export { getServerSideProps as GSSP } from './data'
export default function Page() {
return <h1>Page</h1>
}
`
const pagePkg = parseCode('jsx', code)
// Actually hasExportName checks for the name it is exported AS.
expect(hasExportName(pagePkg, 'GSSP')).toBe(true)
})

it('should detect re-exported all from another file', () => {
const code = `
export * from './data'
export default function Page() {
return <h1>Page</h1>
}
`
const pagePkg = parseCode('jsx', code)
// If ./data is not resolved, this will fail
expect(hasExportName(pagePkg, 'getServerSideProps')).toBe(true)
})

it('should return false for non-data-fetching methods when export * is present', () => {
const code = `
export * from './data'
export default function Page() {
return <h1>Page</h1>
}
`
const pagePkg = parseCode('jsx', code)
expect(hasExportName(pagePkg, 'someOtherExport')).toBe(false)
})
})
22 changes: 21 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,27 @@ export function getDefaultExport(filePkg: ParsedFilePkg, resolveExport = true) {
}

export function hasExportName(filePkg: ParsedFilePkg, name: string) {
return Boolean(getNamedExport(filePkg, name, false))
if (Boolean(getNamedExport(filePkg, name, false))) return true

// Fallback for re-exports (export * from '...')
// When noResolve is true, the checker may not be able to resolve these
const isDataFetchingMethod = [
'getStaticProps',
'getStaticPaths',
'getServerSideProps',
'getInitialProps',
].includes(name)

if (!isDataFetchingMethod) return false

return filePkg.sourceFile.statements.some((node) => {
return (
ts.isExportDeclaration(node) &&
!node.isTypeOnly &&
!node.exportClause &&
node.moduleSpecifier !== undefined
)
})
}

export function getStaticName(
Expand Down