Skip to content

Commit a4840e3

Browse files
authored
codemod: fix props types modify for type alias (#71537)
1 parent 42f1e75 commit a4840e3

File tree

7 files changed

+93
-5
lines changed

7 files changed

+93
-5
lines changed

packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-38.input.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export default function Page(props: PageProps) {
66
const params = props.params
77
return <p>child {params.slug}</p>
88
}
9+

packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-38.output.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type PageProps = {
2-
params: { slug: string }
2+
params: Promise<{ slug: string }>
33
}
44

55
export default async function Page(props: PageProps) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type MetadataProps = {
2+
params: { slug: string }
3+
}
4+
5+
export function generateMetadata(props: MetadataProps) {
6+
return {
7+
title: props.params.slug,
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type MetadataProps = {
2+
params: Promise<{ slug: string }>
3+
}
4+
5+
export async function generateMetadata(props: MetadataProps) {
6+
return {
7+
title: (await props.params).slug,
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type MetadataProps = {
2+
params: { slug: string }
3+
}
4+
5+
export async function generateMetadata(props: MetadataProps): Promise<ResolvedMetadata> {
6+
return {
7+
title: props.params.slug,
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type MetadataProps = {
2+
params: Promise<{ slug: string }>
3+
}
4+
5+
export async function generateMetadata(props: MetadataProps): Promise<ResolvedMetadata> {
6+
return {
7+
title: (await props.params).slug,
8+
};
9+
}

packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ function modifyTypes(
242242
member.typeAnnotation.typeAnnotation = j.tsTypeReference(
243243
j.identifier('Promise'),
244244
j.tsTypeParameterInstantiation([
245-
// @ts-ignore
246245
member.typeAnnotation.typeAnnotation,
247246
])
248247
)
@@ -303,6 +302,55 @@ function modifyTypes(
303302
})
304303
}
305304
}
305+
306+
// Deal with type aliases
307+
if (foundTypes.typeAliases.length > 0) {
308+
const typeAliasDeclaration = foundTypes.typeAliases[0]
309+
if (j.TSTypeAliasDeclaration.check(typeAliasDeclaration)) {
310+
const typeAlias = typeAliasDeclaration.typeAnnotation
311+
if (
312+
j.TSTypeLiteral.check(typeAlias) &&
313+
typeAlias.members.length > 0
314+
) {
315+
const typeLiteral = typeAlias
316+
typeLiteral.members.forEach((member) => {
317+
if (
318+
j.TSPropertySignature.check(member) &&
319+
j.Identifier.check(member.key) &&
320+
TARGET_PROP_NAMES.has(member.key.name)
321+
) {
322+
// if it's already a Promise, don't wrap it again, return
323+
if (
324+
member.typeAnnotation &&
325+
member.typeAnnotation.typeAnnotation &&
326+
member.typeAnnotation.typeAnnotation.type ===
327+
'TSTypeReference' &&
328+
member.typeAnnotation.typeAnnotation.typeName.type ===
329+
'Identifier' &&
330+
member.typeAnnotation.typeAnnotation.typeName.name ===
331+
'Promise'
332+
) {
333+
return
334+
}
335+
336+
// Wrap the prop type in Promise<>
337+
if (
338+
member.typeAnnotation &&
339+
j.TSTypeLiteral.check(member.typeAnnotation.typeAnnotation)
340+
) {
341+
member.typeAnnotation.typeAnnotation = j.tsTypeReference(
342+
j.identifier('Promise'),
343+
j.tsTypeParameterInstantiation([
344+
member.typeAnnotation.typeAnnotation,
345+
])
346+
)
347+
modified = true
348+
}
349+
}
350+
})
351+
}
352+
}
353+
}
306354
}
307355
}
308356

@@ -414,9 +462,12 @@ export function transformDynamicProps(
414462
modified ||= awaited
415463
}
416464

417-
if (modified) {
418-
modifyTypes(currentParam.typeAnnotation, propsIdentifier, root, j)
419-
}
465+
modified ||= modifyTypes(
466+
currentParam.typeAnnotation,
467+
propsIdentifier,
468+
root,
469+
j
470+
)
420471

421472
// cases of passing down `props` into any function
422473
// Page(props) { callback(props) }

0 commit comments

Comments
 (0)