Skip to content

Commit d29d076

Browse files
doc: add reverse react compiler example
1 parent 73480f5 commit d29d076

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

website/catalog/html/extract-i18n-key.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ In practice, you may want to map the extracted text to a key in a dictionary fil
1010

1111
### YAML
1212
```yaml
13+
id: extract-i18n-key
14+
language: html
1315
rule:
1416
kind: text
1517
pattern: $T
@@ -39,4 +41,4 @@ fix: "{{ $('$T') }}"
3941
```
4042

4143
### Contributed by
42-
Inspired by [Vue.js RFC](https://github.com/vuejs/rfcs/discussions/705#discussion-7255672)
44+
Inspired by [Vue.js RFC](https://github.com/vuejs/rfcs/discussions/705#discussion-7255672)

website/catalog/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
4646
* [Rewrite MobX Component Style](/catalog/tsx/#rewrite-mobx-component-style)
4747
* [Unnecessary React Hook](/catalog/tsx/#avoid-unnecessary-react-hook)
4848
* [Unnecessary `useState` Type](/catalog/tsx/#unnecessary-usestate-type)
49+
* [Reverse React Compiler™](/catalog/tsx/#reverse-react-compilertm)
4950
* [YAML](/catalog/yaml/)
5051
* [Find key/value and show message](/catalog/yaml/#find-key-value-and-show-message-using-those-key-vals)

website/catalog/tsx/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ In order to reduce rule duplication, you can use the [`languageGlobs`](/referenc
1212
<!--@include: ./redundant-usestate-type.md-->
1313
<!--@include: ./avoid-jsx-short-circuit.md-->
1414
<!--@include: ./rewrite-mobx-component.md-->
15-
<!--@include: ./unnecessary-react-hook.md-->
15+
<!--@include: ./unnecessary-react-hook.md-->
16+
<!--@include: ./reverse-react-compiler.md-->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## Reverse React Compiler™ <Badge type="tip" text="Has Fix" />
2+
3+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InRzeCIsInF1ZXJ5IjoiIiwicmV3cml0ZSI6IiIsInN0cmljdG5lc3MiOiJyZWxheGVkIiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJpZDogcmV3cml0ZS1jYWNoZSBcbmxhbmd1YWdlOiB0c3hcbnJ1bGU6XG4gIGFueTpcbiAgLSBwYXR0ZXJuOiB1c2VDYWxsYmFjaygkRk4sICQkJClcbiAgLSBwYXR0ZXJuOiBtZW1vKCRGTiwgJCQkKVxuZml4OiAkRk5cblxuLS0tXG5cbmlkOiByZXdyaXRlLXVzZS1tZW1vXG5sYW5ndWFnZTogdHN4XG5ydWxlOiB7IHBhdHRlcm46ICd1c2VNZW1vKCRGTiwgJCQkKScgfVxuZml4OiAoJEZOKSgpIiwic291cmNlIjoiY29uc3QgQ29tcG9uZW50ID0gKCkgPT4ge1xuICBjb25zdCBbY291bnQsIHNldENvdW50XSA9IHVzZVN0YXRlKDApXG4gIGNvbnN0IGluY3JlbWVudCA9IHVzZUNhbGxiYWNrKCgpID0+IHtcbiAgICBzZXRDb3VudCgocHJldkNvdW50KSA9PiBwcmV2Q291bnQgKyAxKVxuICB9LCBbXSlcbiAgY29uc3QgZXhwZW5zaXZlQ2FsY3VsYXRpb24gPSB1c2VNZW1vKCgpID0+IHtcbiAgICAvLyBtb2NrIEV4cGVuc2l2ZSBjYWxjdWxhdGlvblxuICAgIHJldHVybiBjb3VudCAqIDJcbiAgfSwgW2NvdW50XSlcblxuICByZXR1cm4gKFxuICAgIDw+XG4gICAgICA8cD5FeHBlbnNpdmUgUmVzdWx0OiB7ZXhwZW5zaXZlQ2FsY3VsYXRpb259PC9wPlxuICAgICAgPGJ1dHRvbiBvbkNsaWNrPXtpbmNyZW1lbnR9Pntjb3VudH08L2J1dHRvbj5cbiAgICA8Lz5cbiAgKVxufSJ9)
4+
5+
### Description
6+
7+
React Compiler is a build-time only tool that automatically optimizes your React app, working with plain JavaScript and understanding the Rules of React without requiring a rewrite. It optimizes apps by automatically memoizing code, similar to `useMemo`, `useCallback`, and `React.memo`, reducing unnecessary recomputation due to incorrect or forgotten memoization.
8+
9+
10+
Reverse React Compiler™ is a [parody tweet](https://x.com/aidenybai/status/1881397529369034997) that works in the opposite direction. It takes React code and removes memoization, guaranteed to make your code slower. ([not](https://x.com/kentcdodds/status/1881404373646880997) [necessarily](https://dev.to/prathamisonline/are-you-over-using-usememo-and-usecallback-hooks-in-react-5lp))
11+
12+
It is originally written in Babel and this is an [ast-grep version](https://x.com/hd_nvim/status/1881402678493970620) of it.
13+
14+
:::details The Original Babel Implementation
15+
For comparison purposes only. Note the original code [does not correctly rewrite](https://x.com/hd_nvim/status/1881404893136896415) `useMemo`.
16+
```js
17+
const ReverseReactCompiler = ({ types: t }) => ({
18+
visitor: {
19+
CallExpression(path) {
20+
const callee = path.node.callee;
21+
if (
22+
t.isIdentifier(callee, { name: "useMemo" }) ||
23+
t.isIdentifier(callee, { name: "useCallback" }) ||
24+
t.isIdentifier(callee, { name: "memo" })
25+
) {
26+
path.replaceWith(args[0]);
27+
}
28+
},
29+
},
30+
});
31+
```
32+
:::
33+
34+
### YAML
35+
36+
```yaml
37+
id: rewrite-cache
38+
language: tsx
39+
rule:
40+
any:
41+
- pattern: useCallback($FN, $$$)
42+
- pattern: memo($FN, $$$)
43+
fix: $FN
44+
---
45+
id: rewrite-use-memo
46+
language: tsx
47+
rule: { pattern: 'useMemo($FN, $$$)' }
48+
fix: ($FN)() # need IIFE to wrap memo function
49+
```
50+
51+
### Example
52+
53+
```tsx {3-5,6-9}
54+
const Component = () => {
55+
const [count, setCount] = useState(0)
56+
const increment = useCallback(() => {
57+
setCount((prevCount) => prevCount + 1)
58+
}, [])
59+
const expensiveCalculation = useMemo(() => {
60+
// mock Expensive calculation
61+
return count * 2
62+
}, [count])
63+
64+
return (
65+
<>
66+
<p>Expensive Result: {expensiveCalculation}</p>
67+
<button onClick={increment}>{count}</button>
68+
</>
69+
)
70+
}
71+
```
72+
73+
### Diff
74+
```tsx
75+
const Component = () => {
76+
const [count, setCount] = useState(0)
77+
const increment = useCallback(() => { // [!code --]
78+
setCount((prevCount) => prevCount + 1) // [!code --]
79+
}, []) // [!code --]
80+
const increment = () => { // [!code ++]
81+
setCount((prevCount) => prevCount + 1) // [!code ++]
82+
} // [!code ++]
83+
const expensiveCalculation = useMemo(() => { // [!code --]
84+
// mock Expensive calculation // [!code --]
85+
return count * 2 // [!code --]
86+
}, [count]) // [!code --]
87+
const expensiveCalculation = (() => { // [!code ++]
88+
// mock Expensive calculation // [!code ++]
89+
return count * 2 // [!code ++]
90+
})() // [!code ++]
91+
return (
92+
<>
93+
<p>Expensive Result: {expensiveCalculation}</p>
94+
<button onClick={increment}>{count}</button>
95+
</>
96+
)
97+
}
98+
```
99+
100+
### Contributed by
101+
102+
Inspired by [Aiden Bai](https://twitter.com/aidenybai)

0 commit comments

Comments
 (0)