From b1932b081a95bf535d82faef1c4d7d7ca3de2940 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Mon, 7 Nov 2022 16:02:28 +0100 Subject: [PATCH] Refactor error codes in the TS plugin (#42585) Small code refactoring. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `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` - [ ] Integration 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` ## 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) --- packages/next/server/next-typescript.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/next/server/next-typescript.ts b/packages/next/server/next-typescript.ts index 7625bc3b60bc212..b4946c6eb445979 100644 --- a/packages/next/server/next-typescript.ts +++ b/packages/next/server/next-typescript.ts @@ -29,6 +29,13 @@ const DISALLOWED_SERVER_REACT_APIS: string[] = [ const ALLOWED_EXPORTS = ['config', 'generateStaticParams'] +const NEXT_TS_ERRORS = { + INVALID_SERVER_API: 71001, + INVALID_ENTRY_EXPORT: 71002, + INVALID_OPTION_VALUE: 71003, + MISPLACED_CLIENT_ENTRY: 71004, +} + const API_DOCS: Record< string, { @@ -496,7 +503,7 @@ export function createTSPlugin(modules: { prior.push({ file: source, category: ts.DiagnosticCategory.Error, - code: 71004, + code: NEXT_TS_ERRORS.MISPLACED_CLIENT_ENTRY, ...e, }) isClientEntry = false @@ -519,7 +526,7 @@ export function createTSPlugin(modules: { prior.push({ file: source, category: ts.DiagnosticCategory.Error, - code: 71001, + code: NEXT_TS_ERRORS.INVALID_SERVER_API, messageText: `"${name}" is not allowed in Server Components.`, start: element.name.getStart(), length: @@ -546,7 +553,7 @@ export function createTSPlugin(modules: { prior.push({ file: source, category: ts.DiagnosticCategory.Error, - code: 71002, + code: NEXT_TS_ERRORS.INVALID_ENTRY_EXPORT, messageText: `"${name.text}" is not a valid Next.js entry export value.`, start: name.getStart(), length: name.getEnd() - name.getStart(), @@ -620,7 +627,7 @@ export function createTSPlugin(modules: { prior.push({ file: source, category: ts.DiagnosticCategory.Error, - code: 71003, + code: NEXT_TS_ERRORS.INVALID_OPTION_VALUE, messageText: errorMessage || `"${displayedValue}" is not a valid value for the "${name.text}" option.`,