Unverified Commit 2c05914f authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

codemod: fix type only react import (#71053)

Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent c21a74c7
'use client' 'use client'
import { useState } from 'react' import { useState, type JSX } from 'react'
export default function Page({ params }: { params: { slug: string } }) { export default function Page({ params }: { params: { slug: string } }): JSX.Element {
const [text, setText] = useState('') const [text, setText] = useState('')
// usage of `params` // usage of `params`
globalThis.f1(params) globalThis.f1(params)
......
'use client' 'use client'
import { useState, use } from 'react'; import { useState, type JSX, use } from 'react';
export default function Page(props: { params: Promise<{ slug: string }> }) { export default function Page(props: { params: Promise<{ slug: string }> }): JSX.Element {
const params = use(props.params); const params = use(props.params);
const [text, setText] = useState('') const [text, setText] = useState('')
// usage of `params` // usage of `params`
......
'use client'
import type { JSX } from 'react'
interface Props {
params: { slug: string }
}
export default function Page({ params }: Props): JSX.Element {
const { slug } = params
return <p>{slug}</p>
}
'use client';
import { use } from "react";
import type { JSX } from 'react'
interface Props {
params: Promise<{ slug: string }>
}
export default function Page(props: Props): JSX.Element {
const params = use(props.params);
const { slug } = params
return <p>{slug}</p>
}
...@@ -188,6 +188,8 @@ export function insertReactUseImport(root: Collection<any>, j: API['j']) { ...@@ -188,6 +188,8 @@ export function insertReactUseImport(root: Collection<any>, j: API['j']) {
source: { source: {
value: 'react', value: 'react',
}, },
// Skip the type only react imports
importKind: 'value',
}) })
if (reactImportDeclaration.size() > 0) { if (reactImportDeclaration.size() > 0) {
...@@ -197,14 +199,8 @@ export function insertReactUseImport(root: Collection<any>, j: API['j']) { ...@@ -197,14 +199,8 @@ export function insertReactUseImport(root: Collection<any>, j: API['j']) {
importNode.specifiers.push(j.importSpecifier(j.identifier('use'))) importNode.specifiers.push(j.importSpecifier(j.identifier('use')))
} else { } else {
// Final all type imports to 'react' // Final all type imports to 'react'
const reactImport = root.find(j.ImportDeclaration, { if (reactImportDeclaration.size() > 0) {
source: { reactImportDeclaration
value: 'react',
},
})
if (reactImport.size() > 0) {
reactImport
.get() .get()
.node.specifiers.push(j.importSpecifier(j.identifier('use'))) .node.specifiers.push(j.importSpecifier(j.identifier('use')))
} else { } else {
......
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