Skip to content

Commit 6f13a41

Browse files
committed
Adds typescript recipe
1 parent 9f68915 commit 6f13a41

File tree

3 files changed

+475
-17
lines changed

3 files changed

+475
-17
lines changed

website/docs/recipes/react.mdx

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ With the help of these techniques, you can make changes to your codebase with ea
1616

1717
## Props
1818

19-
### Inserting props
19+
Props in jscodeshift are represented by the `j.JSXAttribute` node type, these can be as a simple array on JSX elememts (`j.JSXElement`).
20+
21+
It's important to remember props can be represented an a few different forms, their AST counterparts will change to match.
22+
23+
- [Boolean props](https://reactjs.org/docs/jsx-in-depth.html#props-default-to-true): `isDisabled`
24+
- [String props](https://reactjs.org/docs/jsx-in-depth.html#string-literals): `className="hello"`
25+
- [Expression props](https://reactjs.org/docs/jsx-in-depth.html#javascript-expressions-as-props): `counter={5+4}`
26+
27+
### Inserting a boolean prop
28+
29+
Insert a new boolean prop `isDisabled`.
2030

2131
**Transform:**
2232

@@ -29,18 +39,58 @@ export default function transformer(file, { jscodeshift: j }, options) {
2939
// Find all components called button
3040
.filter(path => path.value.openingElement.name.name === 'button')
3141
.forEach(element => {
32-
const newComponent = j.jsxElement(
33-
j.jsxOpeningElement(element.node.openingElement.name, [
34-
...element.node.openingElement.attributes,
35-
// build and insert our new prop
36-
j.jsxAttribute(j.jsxIdentifier('disabled'), j.stringLiteral('true')),
37-
]),
38-
element.node.closingElement,
39-
element.node.children,
40-
);
42+
element.node.openingElement.attributes = [
43+
...element.node.openingElement.attributes,
44+
// build and insert our new prop
45+
j.jsxAttribute(j.jsxIdentifier('isDisabled')),
46+
];
47+
});
48+
49+
return source.toSource();
50+
}
51+
```
52+
53+
**Input:**
54+
55+
```jsx
56+
import React from 'react';
57+
58+
const Button = props => {
59+
return <button>Submit</button>;
60+
};
61+
```
62+
63+
**Output:**
64+
65+
```diff
66+
import React from 'react';
67+
68+
const Button = props => {
69+
- return <button>Submit</button>;
70+
+ return <button isDisabled>Submit</button>;
71+
};
72+
```
73+
74+
### Inserting a string prop
75+
76+
Insert a new string prop `className`.
4177

42-
// Replace our original component with our modified one
43-
j(element).replaceWith(newComponent);
78+
**Transform:**
79+
80+
```javascript
81+
export default function transformer(file, { jscodeshift: j }, options) {
82+
const source = j(file.source);
83+
84+
source
85+
.find(j.JSXElement)
86+
// Find all components called button
87+
.filter(path => path.value.openingElement.name.name === 'button')
88+
.forEach(element => {
89+
element.node.openingElement.attributes = [
90+
...element.node.openingElement.attributes,
91+
// build and insert our new prop
92+
j.jsxAttribute(j.jsxIdentifier('className'), j.stringLiteral('helloWorld')),
93+
];
4494
});
4595

4696
return source.toSource();
@@ -53,7 +103,7 @@ export default function transformer(file, { jscodeshift: j }, options) {
53103
import React from 'react';
54104

55105
const Button = props => {
56-
return <button className="button">Submit</button>;
106+
return <button>Submit</button>;
57107
};
58108
```
59109

@@ -63,8 +113,8 @@ const Button = props => {
63113
import React from 'react';
64114

65115
const Button = props => {
66-
- return <button className="button">Submit</button>;
67-
+ return <button className="button" disabled="true">Submit</button>;
116+
- return <button>Submit</button>;
117+
+ return <button className="helloWorld">Submit</button>;
68118
};
69119
```
70120

@@ -199,7 +249,22 @@ const Button = props => {
199249
};
200250
```
201251

202-
### Rest props
252+
### Spread props
253+
254+
Spread props (aka [spread attributes](https://reactjs.org/docs/jsx-in-depth.html#spread-attributes)),
255+
allow you to pass an entire object into a jsx element as props.
256+
257+
They are represented by the following notation:
258+
259+
```jsx
260+
function App() {
261+
const props = { firstName: 'Ben', lastName: 'Hector' };
262+
return <Greeting {...props} />;
263+
}
264+
```
265+
266+
As an AST, these are represented by the `j.JSXSpreadAttribute` node,
267+
which accepts an `j.Identifier` (name) and `j.ObjectExpression` (props).
203268

204269
**Transform:**
205270

@@ -271,6 +336,8 @@ const Button = () => {
271336

272337
## JSX
273338

339+
340+
274341
### Wrapping components
275342

276343
Wrapping react components with react components is a fairly common operation.

0 commit comments

Comments
 (0)