Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error for invalid page props in the TS plugin #43300

Merged
merged 4 commits into from Nov 23, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 48 additions & 1 deletion packages/next/server/next-typescript.ts
Expand Up @@ -29,11 +29,14 @@ const DISALLOWED_SERVER_REACT_APIS: string[] = [

const ALLOWED_EXPORTS = ['config', 'generateStaticParams']

const ALLOWED_PAGE_PROPS = ['params', 'searchParams']

const NEXT_TS_ERRORS = {
INVALID_SERVER_API: 71001,
INVALID_ENTRY_EXPORT: 71002,
INVALID_OPTION_VALUE: 71003,
MISPLACED_CLIENT_ENTRY: 71004,
INVALID_PAGE_PROP: 71005,
}

const API_DOCS: Record<
Expand Down Expand Up @@ -197,7 +200,13 @@ export function createTSPlugin(modules: {
const isAppEntryFile = (filePath: string) => {
return (
filePath.startsWith(appDir) &&
/(page|layout)\.(mjs|js|jsx|ts|tsx)$/.test(path.basename(filePath))
/^(page|layout)\.(mjs|js|jsx|ts|tsx)$/.test(path.basename(filePath))
)
}
const isPageFile = (filePath: string) => {
return (
filePath.startsWith(appDir) &&
/^page\.(mjs|js|jsx|ts|tsx)$/.test(path.basename(filePath))
)
}

Expand Down Expand Up @@ -666,6 +675,44 @@ export function createTSPlugin(modules: {
}
}
}
} else if (ts.isFunctionDeclaration(node)) {
let hasExportKeyword = false
let hasDefaultKeyword = false

if (node.modifiers) {
for (const modifier of node.modifiers) {
if (modifier.kind === ts.SyntaxKind.ExportKeyword) {
hasExportKeyword = true
} else if (modifier.kind === ts.SyntaxKind.DefaultKeyword) {
hasDefaultKeyword = true
}
}
}

// `export default function`
if (hasExportKeyword && hasDefaultKeyword) {
if (isPageFile(fileName)) {
const props = node.parameters?.[0]?.name
// For page entries (page.js), it can only have `params` and `searchParams`
// as the prop names.
if (props && ts.isObjectBindingPattern(props)) {
for (const prop of (props as ts.ObjectBindingPattern)
.elements) {
const propName = prop.name.getText()
if (!ALLOWED_PAGE_PROPS.includes(propName)) {
prior.push({
file: source,
category: ts.DiagnosticCategory.Error,
code: NEXT_TS_ERRORS.INVALID_PAGE_PROP,
messageText: `"${propName}" is not a valid page property.`,
start: prop.getStart(),
length: prop.getWidth(),
})
}
}
}
}
}
}
})
}
Expand Down