Skip to content

Commit e48f0b7

Browse files
committed
docs: improved documentation on notes. added one test case
1 parent 3404ed3 commit e48f0b7

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,52 @@ jscodeshift -t Path/To/Repo/transform.ts --extensions=tsx --verbose=2 <FOLDER-YO
110110

111111
## Notes
112112

113-
- The codemod focuses in replacing the nodes but does not do styling. You might want to run Prettier or your favorite formatting tool after the code has been modified.
114-
- If your component was using the implicit definition of `children` provided by `React.FC`, you will have to add the explicit definition or the code won't compile. The value that `React.FC` provides (that accepts anything you would accept in js as children) is `{ children?: ReactNode }`, but you can restrict it to what you only want to accept (for instance, just a string, a number, only one component, and so on)
113+
- The codemod focuses in replacing the nodes but does not do styling. You might want to run Prettier or your favorite formatting tool after the code has been modified. For example, in the following code
114+
115+
```tsx
116+
import React from 'react'
117+
118+
interface Props { id: number, text: string }
119+
const Component: React.FC<Props> = (props) => (
120+
<div>
121+
<span>{props.id}</span>
122+
</div>
123+
)
124+
```
125+
126+
after running the codemod, you might lose the parenthesis
127+
128+
```tsx
129+
import React from 'react'
130+
131+
interface Props { id: number, text: string }
132+
const Component = (props: Props) => <div>
133+
<span>{props.id}</span>
134+
</div>
135+
```
136+
137+
this is because those parenthesis are not strictly required for the code to work. You can fix this by running `Prettier` (or whatever tool you're using to format your code) easily, as the code is still valid
138+
139+
140+
141+
- If your component was using the implicit definition of `children` provided by `React.FC`, you will have to add the explicit definition or the code won't compile. For example, the following code
142+
143+
```tsx
144+
import React from 'react'
145+
146+
type Props = { title: string }
147+
const Component: React.FC<Props> = ({ title, children }) => <div title={title}>{children}</div>
148+
```
149+
150+
will be transformed into this after running the codemod
151+
```tsx
152+
import React from 'react'
153+
154+
type Props = { title: string }
155+
const Component = ({ title, children }: Props) => <div title={title}>{children}</div>
156+
```
157+
158+
However, it won't compile because `children` is not part of your `Props` definition anymore. You can solve this by manually adding the type of `children` again.
159+
160+
161+
The value that `React.FC` provides (that accepts anything you would accept in js as children) is `{ children?: ReactNode }`. I'm intentionally not automatically adding it because you can restrict it to what you only want to accept (for instance, just a string, a number, only one component, and so on), and you know better than I do what you need.

transform.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ const testCases: TestCase[] = [
187187
const MultipleProps = ( { id, ...restProps }: Props1 & { text?: string } ) => <span>foo</span>
188188
`,
189189
},
190+
{
191+
input: `
192+
import React from 'react'
193+
194+
interface Props { id: number, text: string }
195+
const Component: React.FC<Props> = (props) => (
196+
<div>
197+
<span>{props.id}</span>
198+
</div>
199+
)
200+
`,
201+
output: `
202+
import React from 'react'
203+
204+
interface Props { id: number, text: string }
205+
const Component = (props: Props) => <div>
206+
<span>{props.id}</span>
207+
</div>
208+
`,
209+
},
190210
]
191211

192212
function escapeLineEndingsAndMultiWhiteSpaces(text: string | null | undefined) {

0 commit comments

Comments
 (0)