Unverified Commit a4840e32 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

codemod: fix props types modify for type alias (#71537)

parent 42f1e757
......@@ -6,3 +6,4 @@ export default function Page(props: PageProps) {
const params = props.params
return <p>child {params.slug}</p>
}
type PageProps = {
params: { slug: string }
params: Promise<{ slug: string }>
}
export default async function Page(props: PageProps) {
......
type MetadataProps = {
params: { slug: string }
}
export function generateMetadata(props: MetadataProps) {
return {
title: props.params.slug,
}
}
type MetadataProps = {
params: Promise<{ slug: string }>
}
export async function generateMetadata(props: MetadataProps) {
return {
title: (await props.params).slug,
};
}
type MetadataProps = {
params: { slug: string }
}
export async function generateMetadata(props: MetadataProps): Promise<ResolvedMetadata> {
return {
title: props.params.slug,
}
}
type MetadataProps = {
params: Promise<{ slug: string }>
}
export async function generateMetadata(props: MetadataProps): Promise<ResolvedMetadata> {
return {
title: (await props.params).slug,
};
}
......@@ -242,7 +242,6 @@ function modifyTypes(
member.typeAnnotation.typeAnnotation = j.tsTypeReference(
j.identifier('Promise'),
j.tsTypeParameterInstantiation([
// @ts-ignore
member.typeAnnotation.typeAnnotation,
])
)
......@@ -303,6 +302,55 @@ function modifyTypes(
})
}
}
// Deal with type aliases
if (foundTypes.typeAliases.length > 0) {
const typeAliasDeclaration = foundTypes.typeAliases[0]
if (j.TSTypeAliasDeclaration.check(typeAliasDeclaration)) {
const typeAlias = typeAliasDeclaration.typeAnnotation
if (
j.TSTypeLiteral.check(typeAlias) &&
typeAlias.members.length > 0
) {
const typeLiteral = typeAlias
typeLiteral.members.forEach((member) => {
if (
j.TSPropertySignature.check(member) &&
j.Identifier.check(member.key) &&
TARGET_PROP_NAMES.has(member.key.name)
) {
// if it's already a Promise, don't wrap it again, return
if (
member.typeAnnotation &&
member.typeAnnotation.typeAnnotation &&
member.typeAnnotation.typeAnnotation.type ===
'TSTypeReference' &&
member.typeAnnotation.typeAnnotation.typeName.type ===
'Identifier' &&
member.typeAnnotation.typeAnnotation.typeName.name ===
'Promise'
) {
return
}
// Wrap the prop type in Promise<>
if (
member.typeAnnotation &&
j.TSTypeLiteral.check(member.typeAnnotation.typeAnnotation)
) {
member.typeAnnotation.typeAnnotation = j.tsTypeReference(
j.identifier('Promise'),
j.tsTypeParameterInstantiation([
member.typeAnnotation.typeAnnotation,
])
)
modified = true
}
}
})
}
}
}
}
}
......@@ -414,9 +462,12 @@ export function transformDynamicProps(
modified ||= awaited
}
if (modified) {
modifyTypes(currentParam.typeAnnotation, propsIdentifier, root, j)
}
modified ||= modifyTypes(
currentParam.typeAnnotation,
propsIdentifier,
root,
j
)
// cases of passing down `props` into any function
// Page(props) { callback(props) }
......
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