From 685c87c9463ac815e836979054bc4fd91bdcad20 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 23 Nov 2022 19:27:06 +0100 Subject: [PATCH] Show error for invalid page props in the TS plugin (#43300) For page entries, currently it's only allowed to have `params` and `searchParams` in props: CleanShot 2022-11-23 at 16 18 57@2x In the future, this will cover more cases for layout entries too. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: Jiachi Liu --- packages/next/server/next-typescript.ts | 49 ++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/next/server/next-typescript.ts b/packages/next/server/next-typescript.ts index db244af381de..9ff0c6612ac5 100644 --- a/packages/next/server/next-typescript.ts +++ b/packages/next/server/next-typescript.ts @@ -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< @@ -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)) ) } @@ -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(), + }) + } + } + } + } + } } }) }