Commit 9889f83
committed
[compiler][rfc] Enable hook guards in dev mode by default
This validation ensures that React compiler-enabled apps remain correct. That is, code that errors with this validation is most likely ***invalid*** with React compiler is enabled (specifically, hook calls will be compiled to if-else memo blocks).
Hook guards are used extensively for Meta's react compiler rollouts. There, they're enabled for developers (for dev builds) and on e2e test runs. Let's enable by default for oss as well
### Examples of inputs this rule throws on
* Components should not be invoked directly as React Compiler could memoize the call to AnotherComponent, which introduces conditional hook calls in its compiled output.
```js
function Invalid1(props) {
const myJsx = AnotherComponent(props);
return <div> { myJsx } </div>;
}
```
* Hooks must be named as hooks. Similarly, hook calls may not appear in functions that are not components or hooks.
```js
const renamedHook = useState;
function Invalid2() {
const [state, setState] = renamedHook(0);
}
function Invalid3() {
const myFunc = () => useContext(...);
myFunc();
}
```
* Hooks must be directly called (from the body of a component or hook)
```
function call(fn) {
return fn();
}
function Invalid4() {
const result = call(useMyHook);
}
```1 parent e9252bc commit 9889f83
File tree
4 files changed
+93
-18
lines changed- compiler/packages
- babel-plugin-react-compiler
- src
- Babel
- HIR
- react-compiler-runtime/src
4 files changed
+93
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
64 | 76 | | |
65 | 77 | | |
66 | 78 | | |
| |||
Lines changed: 23 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
399 | | - | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
400 | 420 | | |
401 | 421 | | |
402 | 422 | | |
| |||
668 | 688 | | |
669 | 689 | | |
670 | 690 | | |
| 691 | + | |
671 | 692 | | |
672 | 693 | | |
673 | 694 | | |
| |||
711 | 732 | | |
712 | 733 | | |
713 | 734 | | |
| 735 | + | |
714 | 736 | | |
715 | 737 | | |
716 | 738 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
| |||
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | 60 | | |
| 61 | + | |
65 | 62 | | |
66 | 63 | | |
67 | 64 | | |
| |||
74 | 71 | | |
75 | 72 | | |
76 | 73 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
86 | 97 | | |
87 | 98 | | |
88 | 99 | | |
| |||
92 | 103 | | |
93 | 104 | | |
94 | 105 | | |
95 | | - | |
96 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
97 | 113 | | |
98 | 114 | | |
99 | 115 | | |
| |||
134 | 150 | | |
135 | 151 | | |
136 | 152 | | |
137 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
138 | 156 | | |
139 | 157 | | |
140 | 158 | | |
| |||
169 | 187 | | |
170 | 188 | | |
171 | 189 | | |
172 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
173 | 213 | | |
174 | 214 | | |
175 | 215 | | |
| |||
0 commit comments