Skip to content

Commit

Permalink
codemod: fix type only react import (#71053)
Browse files Browse the repository at this point in the history
Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
2 people authored and kdy1 committed Oct 10, 2024
1 parent 3126c41 commit 67f750e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'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('')
// usage of `params`
globalThis.f1(params)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'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 [text, setText] = useState('')
// usage of `params`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'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>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'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>
}
12 changes: 4 additions & 8 deletions packages/next-codemod/transforms/lib/async-request-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export function insertReactUseImport(root: Collection<any>, j: API['j']) {
source: {
value: 'react',
},
// Skip the type only react imports
importKind: 'value',
})

if (reactImportDeclaration.size() > 0) {
Expand All @@ -197,14 +199,8 @@ export function insertReactUseImport(root: Collection<any>, j: API['j']) {
importNode.specifiers.push(j.importSpecifier(j.identifier('use')))
} else {
// Final all type imports to 'react'
const reactImport = root.find(j.ImportDeclaration, {
source: {
value: 'react',
},
})

if (reactImport.size() > 0) {
reactImport
if (reactImportDeclaration.size() > 0) {
reactImportDeclaration
.get()
.node.specifiers.push(j.importSpecifier(j.identifier('use')))
} else {
Expand Down

0 comments on commit 67f750e

Please sign in to comment.