Unverified Commit ca56e4fb authored by Ngô Đức Anh's avatar Ngô Đức Anh Committed by GitHub

Fix(next-types-plugin): remove unused imports (#47667)

Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

This PR fixes the issue where `next-types-plugin`'s typeguard files include unused imports, which was introduced by #47185 and might be problematic with certain configuration.

### Why?

We don't need `ResolvingMetadata` when `options.type` is `'route'` and `NextRequest` when `options.type` is `'page'`/`'layout'`, and so caused the issue.

### How?

Simply check whether `options.type` is `'route'` or `'page'`/`'layout'`.

Fixes #47646
parent e9085efe
...@@ -45,8 +45,11 @@ function createTypeGuardFile( ...@@ -45,8 +45,11 @@ function createTypeGuardFile(
) { ) {
return `// File: ${fullPath} return `// File: ${fullPath}
import * as entry from '${relativePath}' import * as entry from '${relativePath}'
import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface' ${
import type { NextRequest } from 'next/server' options.type === 'route'
? `import type { NextRequest } from 'next/server'`
: `import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface'`
}
type TEntry = typeof entry type TEntry = typeof entry
...@@ -81,44 +84,34 @@ checkFields<Diff<{ ...@@ -81,44 +84,34 @@ checkFields<Diff<{
${ ${
options.type === 'route' options.type === 'route'
? `// Check the prop type of the entry function ? HTTP_METHODS.map(
${HTTP_METHODS.map( (method) => `// Check the prop type of the entry function
(method) => `
if ('${method}' in entry) { if ('${method}' in entry) {
checkFields< checkFields<
Diff< Diff<
ParamCheck<Request | NextRequest>,
{ {
__tag__: '${method}', __tag__: '${method}'
__param_position__: string, __param_position__: 'first'
__param_type__: Request | NextRequest
},
{
__tag__: '${method}',
__param_position__: 'first',
__param_type__: FirstArg<MaybeField<TEntry, '${method}'>> __param_type__: FirstArg<MaybeField<TEntry, '${method}'>>
}, },
'${method}' '${method}'
> >
>(); >()
checkFields< checkFields<
Diff< Diff<
ParamCheck<PageParams>,
{ {
__tag__: '${method}', __tag__: '${method}'
__param_position__: string, __param_position__: 'second'
__param_type__: PageParams
},
{
__tag__: '${method}',
__param_position__: 'second',
__param_type__: SecondArg<MaybeField<TEntry, '${method}'>> __param_type__: SecondArg<MaybeField<TEntry, '${method}'>>
}, },
'${method}' '${method}'
> >
>(); >()
} }
` `
).join('')} ).join('')
`
: `// Check the prop type of the entry function : `// Check the prop type of the entry function
checkFields<Diff<${ checkFields<Diff<${
options.type === 'page' ? 'PageProps' : 'LayoutProps' options.type === 'page' ? 'PageProps' : 'LayoutProps'
...@@ -166,6 +159,12 @@ type FirstArg<T extends Function> = T extends (...args: [infer T, any]) => any ? ...@@ -166,6 +159,12 @@ type FirstArg<T extends Function> = T extends (...args: [infer T, any]) => any ?
type SecondArg<T extends Function> = T extends (...args: [any, infer T]) => any ? unknown extends T ? any : T : never type SecondArg<T extends Function> = T extends (...args: [any, infer T]) => any ? unknown extends T ? any : T : never
type MaybeField<T, K extends string> = T extends { [k in K]: infer G } ? G extends Function ? G : never : never type MaybeField<T, K extends string> = T extends { [k in K]: infer G } ? G extends Function ? G : never : never
type ParamCheck<T> = {
__tag__: string
__param_position__: string
__param_type__: T
}
function checkFields<_ extends { [k in keyof any]: never }>() {} function checkFields<_ extends { [k in keyof any]: never }>() {}
// https://github.com/sindresorhus/type-fest // https://github.com/sindresorhus/type-fest
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment