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
a805a774
Unverified
Commit
a805a774
authored
Oct 08, 2024
by
Jiachi Liu
Committed by
GitHub
Oct 08, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
codemod: skip transform when its properly used (#70936)
parent
58438e9e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
133 additions
and
17 deletions
+133
-17
access-props-24.input.tsx
...async-request-api-dynamic-props/access-props-24.input.tsx
+15
-0
access-props-24.output.tsx
...sync-request-api-dynamic-props/access-props-24.output.tsx
+15
-0
access-props-25.input.tsx
...async-request-api-dynamic-props/access-props-25.input.tsx
+15
-0
access-props-25.output.tsx
...sync-request-api-dynamic-props/access-props-25.output.tsx
+15
-0
next-async-dynamic-prop.ts
...ansforms/lib/async-request-api/next-async-dynamic-prop.ts
+73
-17
No files found.
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-24.input.tsx
0 → 100644
View file @
a805a774
'use client'
;
import
{
use
}
from
'react'
;
interface
Props
{
params
:
Promise
<
{
slug
:
string
;
}
>
;
}
export
default
function
Page
(
props
:
Props
,
)
{
const
params
=
use
(
props
.
params
);
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-24.output.tsx
0 → 100644
View file @
a805a774
'use client'
;
import
{
use
}
from
'react'
;
interface
Props
{
params
:
Promise
<
{
slug
:
string
;
}
>
;
}
export
default
function
Page
(
props
:
Props
,
)
{
const
params
=
use
(
props
.
params
);
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-25.input.tsx
0 → 100644
View file @
a805a774
'use client'
;
import
{
use
}
from
'react'
;
interface
Props
{
params
:
Promise
<
{
slug
:
string
;
}
>
;
}
export
default
function
Page
(
{
params
}:
Props
,
)
{
const
myParams
=
use
(
params
);
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-25.output.tsx
0 → 100644
View file @
a805a774
'use client'
;
import
{
use
}
from
'react'
;
interface
Props
{
params
:
Promise
<
{
slug
:
string
;
}
>
;
}
export
default
function
Page
(
{
params
}:
Props
,
)
{
const
myParams
=
use
(
params
);
}
packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts
View file @
a805a774
...
@@ -73,6 +73,21 @@ function awaitMemberAccessOfProp(
...
@@ -73,6 +73,21 @@ function awaitMemberAccessOfProp(
return
hasAwaited
return
hasAwaited
}
}
function
isParentUseCallExpression
(
path
:
ASTPath
<
any
>
,
j
:
API
[
'jscodeshift'
])
{
if
(
// member access parentPath is argument
j
.
CallExpression
.
check
(
path
.
parent
.
value
)
&&
// member access is first argument
path
.
parent
.
value
.
arguments
[
0
]
===
path
.
value
&&
// function name is `use`
j
.
Identifier
.
check
(
path
.
parent
.
value
.
callee
)
&&
path
.
parent
.
value
.
callee
.
name
===
'use'
)
{
return
true
}
return
false
}
function
applyUseAndRenameAccessedProp
(
function
applyUseAndRenameAccessedProp
(
propIdName
:
string
,
propIdName
:
string
,
path
:
ASTPath
<
FunctionScope
>
,
path
:
ASTPath
<
FunctionScope
>
,
...
@@ -95,6 +110,11 @@ function applyUseAndRenameAccessedProp(
...
@@ -95,6 +110,11 @@ function applyUseAndRenameAccessedProp(
const
accessedNames
:
string
[]
=
[]
const
accessedNames
:
string
[]
=
[]
// rename each member access
// rename each member access
memberAccess
.
forEach
((
memberAccessPath
)
=>
{
memberAccess
.
forEach
((
memberAccessPath
)
=>
{
// If the member access expression is first argument of `use()`, we skip
if
(
isParentUseCallExpression
(
memberAccessPath
,
j
))
{
return
}
const
member
=
memberAccessPath
.
value
const
member
=
memberAccessPath
.
value
const
memberProperty
=
member
.
property
const
memberProperty
=
member
.
property
if
(
j
.
Identifier
.
check
(
memberProperty
))
{
if
(
j
.
Identifier
.
check
(
memberProperty
))
{
...
@@ -313,8 +333,8 @@ export function transformDynamicProps(
...
@@ -313,8 +333,8 @@ export function transformDynamicProps(
currentParam
.
properties
.
forEach
((
prop
)
=>
{
currentParam
.
properties
.
forEach
((
prop
)
=>
{
if
(
if
(
// Could be `Property` or `ObjectProperty`
// Could be `Property` or `ObjectProperty`
'key'
in
prop
&&
(
j
.
Property
.
check
(
prop
)
||
j
.
ObjectProperty
.
check
(
prop
))
&&
prop
.
key
.
type
===
'Identifier'
&&
j
.
Identifier
.
check
(
prop
.
key
)
&&
TARGET_PROP_NAMES
.
has
(
prop
.
key
.
name
)
TARGET_PROP_NAMES
.
has
(
prop
.
key
.
name
)
)
{
)
{
const
value
=
'value'
in
prop
?
prop
.
value
:
null
const
value
=
'value'
in
prop
?
prop
.
value
:
null
...
@@ -322,12 +342,6 @@ export function transformDynamicProps(
...
@@ -322,12 +342,6 @@ export function transformDynamicProps(
}
}
})
})
modifyTypes
(
currentParam
.
typeAnnotation
,
propsIdentifier
,
root
,
j
)
// Override the first param to `props`
params
[
propsArgumentIndex
]
=
propsIdentifier
modified
=
true
modifiedPropArgument
=
true
modifiedPropArgument
=
true
}
else
if
(
currentParam
.
type
===
'Identifier'
)
{
}
else
if
(
currentParam
.
type
===
'Identifier'
)
{
// case of accessing the props.params.<name>:
// case of accessing the props.params.<name>:
...
@@ -380,13 +394,23 @@ export function transformDynamicProps(
...
@@ -380,13 +394,23 @@ export function transformDynamicProps(
}
}
if
(
modifiedPropArgument
)
{
if
(
modifiedPropArgument
)
{
resolveAsyncProp
(
const
isModified
=
resolveAsyncProp
(
path
,
path
,
propertiesMap
,
propertiesMap
,
propsIdentifier
.
name
,
propsIdentifier
.
name
,
allProperties
,
allProperties
,
isDefaultExport
isDefaultExport
)
)
if
(
isModified
)
{
// Make TS happy
if
(
j
.
ObjectPattern
.
check
(
currentParam
))
{
modifyTypes
(
currentParam
.
typeAnnotation
,
propsIdentifier
,
root
,
j
)
}
// Override the first param to `props`
params
[
propsArgumentIndex
]
=
propsIdentifier
modified
=
true
}
}
}
}
}
...
@@ -397,7 +421,7 @@ export function transformDynamicProps(
...
@@ -397,7 +421,7 @@ export function transformDynamicProps(
propsIdentifierName
:
string
,
propsIdentifierName
:
string
,
allProperties
:
ObjectPattern
[
'properties'
],
allProperties
:
ObjectPattern
[
'properties'
],
isDefaultExport
:
boolean
isDefaultExport
:
boolean
)
{
)
:
boolean
{
// Rename props to `prop` argument for the function
// Rename props to `prop` argument for the function
const
insertedRenamedPropFunctionNames
=
new
Set
<
string
>
()
const
insertedRenamedPropFunctionNames
=
new
Set
<
string
>
()
const
node
=
path
.
value
const
node
=
path
.
value
...
@@ -512,11 +536,32 @@ export function transformDynamicProps(
...
@@ -512,11 +536,32 @@ export function transformDynamicProps(
}
}
}
}
let
modifiedPropertyCount
=
0
for
(
const
[
matchedPropName
,
paramsProperty
]
of
propertiesMap
)
{
for
(
const
[
matchedPropName
,
paramsProperty
]
of
propertiesMap
)
{
if
(
!
TARGET_PROP_NAMES
.
has
(
matchedPropName
))
{
if
(
!
TARGET_PROP_NAMES
.
has
(
matchedPropName
))
{
continue
continue
}
}
// In client component, if the param is already wrapped with `use()`, skip the transformation
if
(
isClientComponent
)
{
let
shouldSkip
=
false
const
propPaths
=
j
(
path
).
find
(
j
.
Identifier
,
{
name
:
matchedPropName
,
})
for
(
const
propPath
of
propPaths
.
paths
())
{
if
(
isParentUseCallExpression
(
propPath
,
j
))
{
// Skip transformation
shouldSkip
=
true
break
}
}
if
(
shouldSkip
)
{
continue
}
}
const
propRenamedId
=
j
.
Identifier
.
check
(
paramsProperty
)
const
propRenamedId
=
j
.
Identifier
.
check
(
paramsProperty
)
?
paramsProperty
.
name
?
paramsProperty
.
name
:
null
:
null
...
@@ -524,16 +569,25 @@ export function transformDynamicProps(
...
@@ -524,16 +569,25 @@ export function transformDynamicProps(
// if propName is not used in lower scope, and it stars with unused prefix `_`,
// if propName is not used in lower scope, and it stars with unused prefix `_`,
// also skip the transformation
// also skip the transformation
const
hasDeclared
=
path
.
scope
.
declares
(
propName
)
if
(
!
hasDeclared
&&
propName
.
startsWith
(
'_'
))
continue
const
functionBodyPath
=
path
.
get
(
'body'
)
const
hasUsedInBody
=
j
(
functionBodyPath
)
.
find
(
j
.
Identifier
,
{
name
:
propName
,
})
.
size
()
>
0
if
(
!
hasUsedInBody
&&
propName
.
startsWith
(
'_'
))
continue
modifiedPropertyCount
++
const
propNameIdentifier
=
j
.
identifier
(
matchedPropName
)
const
propNameIdentifier
=
j
.
identifier
(
matchedPropName
)
const
propsIdentifier
=
j
.
identifier
(
propsIdentifierName
)
const
propsIdentifier
=
j
.
identifier
(
propsIdentifierName
)
const
accessedPropId
=
j
.
memberExpression
(
const
accessedPropId
Expr
=
j
.
memberExpression
(
propsIdentifier
,
propsIdentifier
,
propNameIdentifier
propNameIdentifier
)
)
// Check param property value, if it's destructed, we need to destruct it as well
// Check param property value, if it's destructed, we need to destruct it as well
// e.g.
// e.g.
// input: Page({ params: { slug } })
// input: Page({ params: { slug } })
...
@@ -576,7 +630,7 @@ export function transformDynamicProps(
...
@@ -576,7 +630,7 @@ export function transformDynamicProps(
const
paramAssignment
=
j
.
variableDeclaration
(
'const'
,
[
const
paramAssignment
=
j
.
variableDeclaration
(
'const'
,
[
j
.
variableDeclarator
(
j
.
variableDeclarator
(
j
.
identifier
(
propName
),
j
.
identifier
(
propName
),
j
.
awaitExpression
(
accessedPropId
)
j
.
awaitExpression
(
accessedPropId
Expr
)
),
),
])
])
if
(
!
insertedRenamedPropFunctionNames
.
has
(
uid
)
&&
functionBody
)
{
if
(
!
insertedRenamedPropFunctionNames
.
has
(
uid
)
&&
functionBody
)
{
...
@@ -598,7 +652,7 @@ export function transformDynamicProps(
...
@@ -598,7 +652,7 @@ export function transformDynamicProps(
const
paramAssignment
=
j
.
variableDeclaration
(
'const'
,
[
const
paramAssignment
=
j
.
variableDeclaration
(
'const'
,
[
j
.
variableDeclarator
(
j
.
variableDeclarator
(
j
.
identifier
(
propName
),
j
.
identifier
(
propName
),
j
.
awaitExpression
(
accessedPropId
)
j
.
awaitExpression
(
accessedPropId
Expr
)
),
),
])
])
...
@@ -611,7 +665,7 @@ export function transformDynamicProps(
...
@@ -611,7 +665,7 @@ export function transformDynamicProps(
const
paramAssignment
=
j
.
variableDeclaration
(
'const'
,
[
const
paramAssignment
=
j
.
variableDeclaration
(
'const'
,
[
j
.
variableDeclarator
(
j
.
variableDeclarator
(
j
.
identifier
(
propName
),
j
.
identifier
(
propName
),
j
.
callExpression
(
j
.
identifier
(
'use'
),
[
accessedPropId
])
j
.
callExpression
(
j
.
identifier
(
'use'
),
[
accessedPropId
Expr
])
),
),
])
])
if
(
!
insertedRenamedPropFunctionNames
.
has
(
uid
)
&&
functionBody
)
{
if
(
!
insertedRenamedPropFunctionNames
.
has
(
uid
)
&&
functionBody
)
{
...
@@ -622,6 +676,8 @@ export function transformDynamicProps(
...
@@ -622,6 +676,8 @@ export function transformDynamicProps(
}
}
}
}
}
}
return
modifiedPropertyCount
>
0
}
}
const
defaultExportsDeclarations
=
root
.
find
(
j
.
ExportDefaultDeclaration
)
const
defaultExportsDeclarations
=
root
.
find
(
j
.
ExportDefaultDeclaration
)
...
...
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