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

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

Merged
merged 5 commits into from
Mar 30, 2023
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
45 changes: 22 additions & 23 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ function createTypeGuardFile(
) {
return `// File: ${fullPath}
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

Expand Down Expand Up @@ -81,44 +84,34 @@ checkFields<Diff<{

${
options.type === 'route'
? `// Check the prop type of the entry function
${HTTP_METHODS.map(
(method) => `
? HTTP_METHODS.map(
(method) => `// Check the prop type of the entry function
if ('${method}' in entry) {
checkFields<
Diff<
ParamCheck<Request | NextRequest>,
{
__tag__: '${method}',
__param_position__: string,
__param_type__: Request | NextRequest
},
{
__tag__: '${method}',
__param_position__: 'first',
__tag__: '${method}'
__param_position__: 'first'
__param_type__: FirstArg<MaybeField<TEntry, '${method}'>>
},
'${method}'
>
>();
>()
checkFields<
Diff<
ParamCheck<PageParams>,
{
__tag__: '${method}',
__param_position__: string,
__param_type__: PageParams
},
{
__tag__: '${method}',
__param_position__: 'second',
__tag__: '${method}'
__param_position__: 'second'
__param_type__: SecondArg<MaybeField<TEntry, '${method}'>>
},
'${method}'
>
>();
>()
}
`
).join('')}
`
).join('')
: `// Check the prop type of the entry function
checkFields<Diff<${
options.type === 'page' ? 'PageProps' : 'LayoutProps'
Expand Down Expand Up @@ -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 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 }>() {}

// https://github.com/sindresorhus/type-fest
Expand Down