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

codemod: skip transform when its properly used (#70936)

parent 58438e9e
'use client';
import { use } from 'react';
interface Props {
params: Promise<{
slug: string;
}>;
}
export default function Page(
props: Props,
) {
const params = use(props.params);
}
'use client';
import { use } from 'react';
interface Props {
params: Promise<{
slug: string;
}>;
}
export default function Page(
props: Props,
) {
const params = use(props.params);
}
'use client';
import { use } from 'react';
interface Props {
params: Promise<{
slug: string;
}>;
}
export default function Page(
{ params }: Props,
) {
const myParams = use(params);
}
'use client';
import { use } from 'react';
interface Props {
params: Promise<{
slug: string;
}>;
}
export default function Page(
{ params }: Props,
) {
const myParams = use(params);
}
...@@ -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 accessedPropIdExpr = 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(accessedPropIdExpr)
), ),
]) ])
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(accessedPropIdExpr)
), ),
]) ])
...@@ -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'), [accessedPropIdExpr])
), ),
]) ])
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)
......
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