Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
a4840e32
Unverified
Commit
a4840e32
authored
Oct 20, 2024
by
Jiachi Liu
Committed by
GitHub
Oct 20, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
codemod: fix props types modify for type alias (#71537)
parent
42f1e757
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
93 additions
and
5 deletions
+93
-5
access-props-38.input.tsx
...async-request-api-dynamic-props/access-props-38.input.tsx
+1
-0
access-props-38.output.tsx
...sync-request-api-dynamic-props/access-props-38.output.tsx
+1
-1
generate-metadata-access-prop-03.input.tsx
...-dynamic-props/generate-metadata-access-prop-03.input.tsx
+9
-0
generate-metadata-access-prop-03.output.tsx
...dynamic-props/generate-metadata-access-prop-03.output.tsx
+9
-0
generate-metadata-access-prop-04.input.tsx
...-dynamic-props/generate-metadata-access-prop-04.input.tsx
+9
-0
generate-metadata-access-prop-04.output.tsx
...dynamic-props/generate-metadata-access-prop-04.output.tsx
+9
-0
next-async-dynamic-prop.ts
...ansforms/lib/async-request-api/next-async-dynamic-prop.ts
+55
-4
No files found.
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-38.input.tsx
View file @
a4840e32
...
@@ -6,3 +6,4 @@ export default function Page(props: PageProps) {
...
@@ -6,3 +6,4 @@ export default function Page(props: PageProps) {
const
params
=
props
.
params
const
params
=
props
.
params
return
<
p
>
child
{
params
.
slug
}
</
p
>
return
<
p
>
child
{
params
.
slug
}
</
p
>
}
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-38.output.tsx
View file @
a4840e32
type
PageProps
=
{
type
PageProps
=
{
params
:
{
slug
:
string
}
params
:
Promise
<
{
slug
:
string
}
>
}
}
export
default
async
function
Page
(
props
:
PageProps
)
{
export
default
async
function
Page
(
props
:
PageProps
)
{
...
...
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/generate-metadata-access-prop-03.input.tsx
0 → 100644
View file @
a4840e32
type
MetadataProps
=
{
params
:
{
slug
:
string
}
}
export
function
generateMetadata
(
props
:
MetadataProps
)
{
return
{
title
:
props
.
params
.
slug
,
}
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/generate-metadata-access-prop-03.output.tsx
0 → 100644
View file @
a4840e32
type
MetadataProps
=
{
params
:
Promise
<
{
slug
:
string
}
>
}
export
async
function
generateMetadata
(
props
:
MetadataProps
)
{
return
{
title
:
(
await
props
.
params
).
slug
,
};
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/generate-metadata-access-prop-04.input.tsx
0 → 100644
View file @
a4840e32
type
MetadataProps
=
{
params
:
{
slug
:
string
}
}
export
async
function
generateMetadata
(
props
:
MetadataProps
):
Promise
<
ResolvedMetadata
>
{
return
{
title
:
props
.
params
.
slug
,
}
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/generate-metadata-access-prop-04.output.tsx
0 → 100644
View file @
a4840e32
type
MetadataProps
=
{
params
:
Promise
<
{
slug
:
string
}
>
}
export
async
function
generateMetadata
(
props
:
MetadataProps
):
Promise
<
ResolvedMetadata
>
{
return
{
title
:
(
await
props
.
params
).
slug
,
};
}
packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts
View file @
a4840e32
...
@@ -242,7 +242,6 @@ function modifyTypes(
...
@@ -242,7 +242,6 @@ function modifyTypes(
member
.
typeAnnotation
.
typeAnnotation
=
j
.
tsTypeReference
(
member
.
typeAnnotation
.
typeAnnotation
=
j
.
tsTypeReference
(
j
.
identifier
(
'Promise'
),
j
.
identifier
(
'Promise'
),
j
.
tsTypeParameterInstantiation
([
j
.
tsTypeParameterInstantiation
([
// @ts-ignore
member
.
typeAnnotation
.
typeAnnotation
,
member
.
typeAnnotation
.
typeAnnotation
,
])
])
)
)
...
@@ -303,6 +302,55 @@ function modifyTypes(
...
@@ -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(
...
@@ -414,9 +462,12 @@ export function transformDynamicProps(
modified
||=
awaited
modified
||=
awaited
}
}
if
(
modified
)
{
modified
||=
modifyTypes
(
modifyTypes
(
currentParam
.
typeAnnotation
,
propsIdentifier
,
root
,
j
)
currentParam
.
typeAnnotation
,
}
propsIdentifier
,
root
,
j
)
// cases of passing down `props` into any function
// cases of passing down `props` into any function
// Page(props) { callback(props) }
// Page(props) { callback(props) }
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment