Unverified Commit 3dbbcadc authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

codemod: handle the async api type (#70785)

parent 47497454
import { cookies } from 'next/headers'
export type Cookie = ReturnType<typeof cookies>
export function foo(c: ReturnType<typeof cookies>) {
return c
}
import { cookies } from 'next/headers'
export type Cookie = Awaited<ReturnType<typeof cookies>>
export function foo(c: Awaited<ReturnType<typeof cookies>>) {
return c
}
......@@ -205,6 +205,42 @@ export function transformDynamicAPI(
}
}
})
// Handle type usage of async API, e.g. `type Cookie = ReturnType<typeof cookies>`
// convert it to `type Cookie = Awaited<ReturnType<typeof cookies>>`
root
.find(j.TSTypeReference, {
typeName: {
type: 'Identifier',
name: 'ReturnType',
},
})
.forEach((path) => {
const typeParam = path.node.typeParameters?.params[0]
// Check if the ReturnType is for 'cookies'
if (
typeParam &&
j.TSTypeQuery.check(typeParam) &&
j.Identifier.check(typeParam.exprName) &&
typeParam.exprName.name === asyncRequestApiName
) {
// Replace ReturnType<typeof cookies> with Awaited<ReturnType<typeof cookies>>
const awaitedTypeReference = j.tsTypeReference(
j.identifier('Awaited'),
j.tsTypeParameterInstantiation([
j.tsTypeReference(
j.identifier('ReturnType'),
j.tsTypeParameterInstantiation([typeParam])
),
])
)
j(path).replaceWith(awaitedTypeReference)
modified = true
}
})
}
const isClientComponent = determineClientDirective(root, j)
......
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