|
| 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