Unverified Commit 4d7d9abc authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[ts-plugin] keep showing the types in the function body (#86273)

parent 7e093afb
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
isPositionInsideNode, isPositionInsideNode,
getTs, getTs,
removeStringQuotes, removeStringQuotes,
getTypeChecker,
} from '../utils' } from '../utils'
import { NEXT_TS_ERRORS, ALLOWED_EXPORTS } from '../constant' import { NEXT_TS_ERRORS, ALLOWED_EXPORTS } from '../constant'
import type tsModule from 'typescript/lib/tsserverlibrary' import type tsModule from 'typescript/lib/tsserverlibrary'
...@@ -308,9 +309,49 @@ const config = { ...@@ -308,9 +309,49 @@ const config = {
API_DOCS[entryConfig].link, API_DOCS[entryConfig].link,
} }
if (value && isPositionInsideNode(position, value)) { // When the value is a flexible type (like a function), also compute its
// Hovers the value of the config // inferred type so we can surface it alongside the docs. This is useful
const isString = ts.isStringLiteral(value) // even when the value is considered invalid by the config validation,
// as long as it's not a direct literal export.
let displayParts: tsModule.SymbolDisplayPart[] = []
const typeChecker = getTypeChecker()
const isString = !!value && ts.isStringLiteral(value)
const isFunctionValue =
!!value &&
!isString &&
(ts.isArrowFunction(value) ||
ts.isFunctionExpression(value) ||
ts.isFunctionDeclaration(value))
if (typeChecker && value && isFunctionValue) {
try {
// If we're hovering the config identifier, ask for the type at the
// identifier; otherwise, ask at the value node. This makes sure
// highlighting `generateMetadata` itself also shows the inferred type.
const typeTarget = isPositionInsideNode(position, name) ? name : value
const type = typeChecker.getTypeAtLocation(typeTarget)
if (type) {
const typeString = typeChecker.typeToString(type, typeTarget)
if (typeString) {
displayParts = [
{
text: typeString,
kind: 'typeName',
},
]
}
}
} catch {
// If type checking fails, continue without type info.
}
}
// For non-function values (like literals), hovering the value should show
// option-specific docs. For function-valued configs (e.g. `generateMetadata`),
// we let TypeScript handle hover anywhere in the initializer except for the
// export identifier itself.
if (value && !isFunctionValue && isPositionInsideNode(position, value)) {
// Hovering the value of the config
const text = removeStringQuotes(value.getText()) const text = removeStringQuotes(value.getText())
const key = isString ? `"${text}"` : text const key = isString ? `"${text}"` : text
...@@ -339,7 +380,8 @@ const config = { ...@@ -339,7 +380,8 @@ const config = {
], ],
} }
} else { } else {
// Wrong value, display the docs link // Wrong value: still show the docs link, and when available, the
// inferred type for non-literal (i.e. non-direct) exports.
overridden = { overridden = {
kind: ts.ScriptElementKind.enumElement, kind: ts.ScriptElementKind.enumElement,
kindModifiers: ts.ScriptElementKindModifier.none, kindModifiers: ts.ScriptElementKindModifier.none,
...@@ -347,11 +389,23 @@ const config = { ...@@ -347,11 +389,23 @@ const config = {
start: value.getStart(), start: value.getStart(),
length: value.getWidth(), length: value.getWidth(),
}, },
displayParts: [], displayParts,
documentation: [docsLink], documentation: [docsLink],
} }
} }
} else { } else {
// For function-valued configs, if we're hovering anywhere within the
// initializer (including `async`, parameters, or the body) but not on
// the export identifier itself, don't override TypeScript's default
// hover. We only want to override when hovering the config identifier
// (e.g. `generateMetadata`), not arbitrary tokens within the function.
if (
isFunctionValue &&
isPositionInsideNode(position, value) && // hover is somewhere within the function initializer
!isPositionInsideNode(position, name) // ...but not on the export identifier itself
) {
return
}
// Hovers the name of the config // Hovers the name of the config
overridden = { overridden = {
kind: ts.ScriptElementKind.enumElement, kind: ts.ScriptElementKind.enumElement,
...@@ -360,7 +414,7 @@ const config = { ...@@ -360,7 +414,7 @@ const config = {
start: name.getStart(), start: name.getStart(),
length: name.getWidth(), length: name.getWidth(),
}, },
displayParts: [], displayParts,
documentation: [ documentation: [
{ {
kind: 'text', kind: 'text',
......
import { ClientComponent } from './client'
const noop = () => {}
export default function Page() { export default function Page() {
return ( return <>hello world</>
<>
<ClientComponent unknown={noop} unknownAction={noop} />
</>
)
} }
import { Metadata } from 'next'
export default function Layout({ children }: { children: React.ReactNode }) {
return <>{children}</>
}
export const metadata: Metadata = {
title: 'Project Layout',
}
export default async function Page(props: PageProps<'/project/[slug]'>) {
const { slug } = await props.params
return <p>project {slug}</p>
}
export const generateMetadata = async ({
params,
}: PageProps<'/project/[slug]'>) => {
const { slug } = await params
return {
title: `Project ${slug}`,
}
}
...@@ -2,3 +2,12 @@ export default async function Page(props: PageProps<'/project/[slug]'>) { ...@@ -2,3 +2,12 @@ export default async function Page(props: PageProps<'/project/[slug]'>) {
const { slug } = await props.params const { slug } = await props.params
return <p>project {slug}</p> return <p>project {slug}</p>
} }
export const generateMetadata = async ({
params,
}: PageProps<'/project/[slug]'>) => {
const { slug } = await params
return {
title: `Project ${slug}`,
}
}
...@@ -8,11 +8,6 @@ const nextConfig = { ...@@ -8,11 +8,6 @@ const nextConfig = {
typedRoutes: true, typedRoutes: true,
async redirects() { async redirects() {
return [ return [
{
source: '/project/:slug',
destination: '/project/:slug',
permanent: true,
},
{ {
source: '/blog/:category/:slug*', source: '/blog/:category/:slug*',
destination: '/posts/:category/:slug*', destination: '/posts/:category/:slug*',
......
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