Skip to content

Commit 88ee1f5

Browse files
authored
Add reporting modes for react-hooks/exhaustive-effect-dependencies and temporarily enable (facebook#35365)
`react-hooks/exhaustive-effect-dependencies` from `ValidateExhaustiveDeps` reports errors for both missing and extra effect deps. We already have `react-hooks/exhaustive-deps` that errors on missing dependencies. In the future we'd like to consolidate this all to the compiler based error, but for now there's a lot of overlap. Let's enable testing the extra dep warning by splitting out reporting modes. This PR - Creates `on`, `off`, `missing-only`, and `extra-only` reporting modes for the effect dep validation flag - Temporarily enables the new rule with `extra-only` in `eslint-plugin-react-hooks` - Adds additional null checking to `manualMemoLoc` to fix a bug found when running against the fixture
1 parent bcf97c7 commit 88ee1f5

17 files changed

+293
-22
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,15 @@ export const EnvironmentConfigSchema = z.object({
225225

226226
/**
227227
* Validate that dependencies supplied to effect hooks are exhaustive.
228+
* Can be:
229+
* - 'off': No validation (default)
230+
* - 'all': Validate and report both missing and extra dependencies
231+
* - 'missing-only': Only report missing dependencies
232+
* - 'extra-only': Only report extra/unnecessary dependencies
228233
*/
229-
validateExhaustiveEffectDependencies: z.boolean().default(false),
234+
validateExhaustiveEffectDependencies: z
235+
.enum(['off', 'all', 'missing-only', 'extra-only'])
236+
.default('off'),
230237

231238
/**
232239
* When this is true, rather than pruning existing manual memoization but ensuring or validating

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export function validateExhaustiveDependencies(
141141
reactive,
142142
startMemo.depsLoc,
143143
ErrorCategory.MemoDependencies,
144+
'all',
144145
);
145146
if (diagnostic != null) {
146147
error.pushDiagnostic(diagnostic);
@@ -159,7 +160,7 @@ export function validateExhaustiveDependencies(
159160
onStartMemoize,
160161
onFinishMemoize,
161162
onEffect: (inferred, manual, manualMemoLoc) => {
162-
if (env.config.validateExhaustiveEffectDependencies === false) {
163+
if (env.config.validateExhaustiveEffectDependencies === 'off') {
163164
return;
164165
}
165166
if (DEBUG) {
@@ -195,12 +196,17 @@ export function validateExhaustiveDependencies(
195196
});
196197
}
197198
}
199+
const effectReportMode =
200+
typeof env.config.validateExhaustiveEffectDependencies === 'string'
201+
? env.config.validateExhaustiveEffectDependencies
202+
: 'all';
198203
const diagnostic = validateDependencies(
199204
Array.from(inferred),
200205
manualDeps,
201206
reactive,
202207
manualMemoLoc,
203208
ErrorCategory.EffectExhaustiveDependencies,
209+
effectReportMode,
204210
);
205211
if (diagnostic != null) {
206212
error.pushDiagnostic(diagnostic);
@@ -220,6 +226,7 @@ function validateDependencies(
220226
category:
221227
| ErrorCategory.MemoDependencies
222228
| ErrorCategory.EffectExhaustiveDependencies,
229+
exhaustiveDepsReportMode: 'all' | 'missing-only' | 'extra-only',
223230
): CompilerDiagnostic | null {
224231
// Sort dependencies by name and path, with shorter/non-optional paths first
225232
inferred.sort((a, b) => {
@@ -370,9 +377,20 @@ function validateDependencies(
370377
extra.push(dep);
371378
}
372379

373-
if (missing.length !== 0 || extra.length !== 0) {
380+
// Filter based on report mode
381+
const filteredMissing =
382+
exhaustiveDepsReportMode === 'extra-only' ? [] : missing;
383+
const filteredExtra =
384+
exhaustiveDepsReportMode === 'missing-only' ? [] : extra;
385+
386+
if (filteredMissing.length !== 0 || filteredExtra.length !== 0) {
374387
let suggestion: CompilerSuggestion | null = null;
375-
if (manualMemoLoc != null && typeof manualMemoLoc !== 'symbol') {
388+
if (
389+
manualMemoLoc != null &&
390+
typeof manualMemoLoc !== 'symbol' &&
391+
manualMemoLoc.start.index != null &&
392+
manualMemoLoc.end.index != null
393+
) {
376394
suggestion = {
377395
description: 'Update dependencies',
378396
range: [manualMemoLoc.start.index, manualMemoLoc.end.index],
@@ -388,8 +406,13 @@ function validateDependencies(
388406
.join(', ')}]`,
389407
};
390408
}
391-
const diagnostic = createDiagnostic(category, missing, extra, suggestion);
392-
for (const dep of missing) {
409+
const diagnostic = createDiagnostic(
410+
category,
411+
filteredMissing,
412+
filteredExtra,
413+
suggestion,
414+
);
415+
for (const dep of filteredMissing) {
393416
let reactiveStableValueHint = '';
394417
if (isStableType(dep.identifier)) {
395418
reactiveStableValueHint =
@@ -402,7 +425,7 @@ function validateDependencies(
402425
loc: dep.loc,
403426
});
404427
}
405-
for (const dep of extra) {
428+
for (const dep of filteredExtra) {
406429
if (dep.root.kind === 'Global') {
407430
diagnostic.withDetails({
408431
kind: 'error',

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/exhaustive-deps/error.exhaustive-deps-effect-events.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## Input
33

44
```javascript
5-
// @validateExhaustiveEffectDependencies
5+
// @validateExhaustiveEffectDependencies:"all"
66
import {useEffect, useEffectEvent} from 'react';
77

88
function Component({x, y, z}) {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/exhaustive-deps/error.exhaustive-deps-effect-events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @validateExhaustiveEffectDependencies
1+
// @validateExhaustiveEffectDependencies:"all"
22
import {useEffect, useEffectEvent} from 'react';
33

44
function Component({x, y, z}) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateExhaustiveEffectDependencies:"extra-only"
6+
import {useEffect} from 'react';
7+
8+
function Component({x, y, z}) {
9+
// no error: missing dep not reported in extra-only mode
10+
useEffect(() => {
11+
log(x);
12+
}, []);
13+
14+
// error: extra dep - y
15+
useEffect(() => {
16+
log(x);
17+
}, [x, y]);
18+
19+
// error: extra dep - y (missing dep - z not reported)
20+
useEffect(() => {
21+
log(x, z);
22+
}, [x, y]);
23+
24+
// error: extra dep - x.y
25+
useEffect(() => {
26+
log(x);
27+
}, [x.y]);
28+
}
29+
30+
```
31+
32+
33+
## Error
34+
35+
```
36+
Found 3 errors:
37+
38+
Error: Found extra effect dependencies
39+
40+
Extra dependencies can cause an effect to fire more often than it should, resulting in performance problems such as excessive renders and side effects.
41+
42+
error.invalid-exhaustive-effect-deps-extra-only.ts:13:9
43+
11 | useEffect(() => {
44+
12 | log(x);
45+
> 13 | }, [x, y]);
46+
| ^ Unnecessary dependency `y`
47+
14 |
48+
15 | // error: extra dep - y (missing dep - z not reported)
49+
16 | useEffect(() => {
50+
51+
Inferred dependencies: `[x]`
52+
53+
Error: Found extra effect dependencies
54+
55+
Extra dependencies can cause an effect to fire more often than it should, resulting in performance problems such as excessive renders and side effects.
56+
57+
error.invalid-exhaustive-effect-deps-extra-only.ts:18:9
58+
16 | useEffect(() => {
59+
17 | log(x, z);
60+
> 18 | }, [x, y]);
61+
| ^ Unnecessary dependency `y`
62+
19 |
63+
20 | // error: extra dep - x.y
64+
21 | useEffect(() => {
65+
66+
Inferred dependencies: `[x, z]`
67+
68+
Error: Found extra effect dependencies
69+
70+
Extra dependencies can cause an effect to fire more often than it should, resulting in performance problems such as excessive renders and side effects.
71+
72+
error.invalid-exhaustive-effect-deps-extra-only.ts:23:6
73+
21 | useEffect(() => {
74+
22 | log(x);
75+
> 23 | }, [x.y]);
76+
| ^^^ Overly precise dependency `x.y`, use `x` instead
77+
24 | }
78+
25 |
79+
80+
Inferred dependencies: `[x]`
81+
```
82+
83+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @validateExhaustiveEffectDependencies:"extra-only"
2+
import {useEffect} from 'react';
3+
4+
function Component({x, y, z}) {
5+
// no error: missing dep not reported in extra-only mode
6+
useEffect(() => {
7+
log(x);
8+
}, []);
9+
10+
// error: extra dep - y
11+
useEffect(() => {
12+
log(x);
13+
}, [x, y]);
14+
15+
// error: extra dep - y (missing dep - z not reported)
16+
useEffect(() => {
17+
log(x, z);
18+
}, [x, y]);
19+
20+
// error: extra dep - x.y
21+
useEffect(() => {
22+
log(x);
23+
}, [x.y]);
24+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateExhaustiveEffectDependencies:"missing-only"
6+
import {useEffect} from 'react';
7+
8+
function Component({x, y, z}) {
9+
// error: missing dep - x
10+
useEffect(() => {
11+
log(x);
12+
}, []);
13+
14+
// no error: extra dep not reported in missing-only mode
15+
useEffect(() => {
16+
log(x);
17+
}, [x, y]);
18+
19+
// error: missing dep - z (extra dep - y not reported)
20+
useEffect(() => {
21+
log(x, z);
22+
}, [x, y]);
23+
24+
// error: missing dep x
25+
useEffect(() => {
26+
log(x);
27+
}, [x.y]);
28+
}
29+
30+
```
31+
32+
33+
## Error
34+
35+
```
36+
Found 3 errors:
37+
38+
Error: Found missing effect dependencies
39+
40+
Missing dependencies can cause an effect to fire less often than it should.
41+
42+
error.invalid-exhaustive-effect-deps-missing-only.ts:7:8
43+
5 | // error: missing dep - x
44+
6 | useEffect(() => {
45+
> 7 | log(x);
46+
| ^ Missing dependency `x`
47+
8 | }, []);
48+
9 |
49+
10 | // no error: extra dep not reported in missing-only mode
50+
51+
Inferred dependencies: `[x]`
52+
53+
Error: Found missing effect dependencies
54+
55+
Missing dependencies can cause an effect to fire less often than it should.
56+
57+
error.invalid-exhaustive-effect-deps-missing-only.ts:17:11
58+
15 | // error: missing dep - z (extra dep - y not reported)
59+
16 | useEffect(() => {
60+
> 17 | log(x, z);
61+
| ^ Missing dependency `z`
62+
18 | }, [x, y]);
63+
19 |
64+
20 | // error: missing dep x
65+
66+
Inferred dependencies: `[x, z]`
67+
68+
Error: Found missing effect dependencies
69+
70+
Missing dependencies can cause an effect to fire less often than it should.
71+
72+
error.invalid-exhaustive-effect-deps-missing-only.ts:22:8
73+
20 | // error: missing dep x
74+
21 | useEffect(() => {
75+
> 22 | log(x);
76+
| ^ Missing dependency `x`
77+
23 | }, [x.y]);
78+
24 | }
79+
25 |
80+
81+
Inferred dependencies: `[x]`
82+
```
83+
84+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @validateExhaustiveEffectDependencies:"missing-only"
2+
import {useEffect} from 'react';
3+
4+
function Component({x, y, z}) {
5+
// error: missing dep - x
6+
useEffect(() => {
7+
log(x);
8+
}, []);
9+
10+
// no error: extra dep not reported in missing-only mode
11+
useEffect(() => {
12+
log(x);
13+
}, [x, y]);
14+
15+
// error: missing dep - z (extra dep - y not reported)
16+
useEffect(() => {
17+
log(x, z);
18+
}, [x, y]);
19+
20+
// error: missing dep x
21+
useEffect(() => {
22+
log(x);
23+
}, [x.y]);
24+
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/exhaustive-deps/error.invalid-exhaustive-effect-deps.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## Input
33

44
```javascript
5-
// @validateExhaustiveEffectDependencies
5+
// @validateExhaustiveEffectDependencies:"all"
66
import {useEffect} from 'react';
77

88
function Component({x, y, z}) {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/exhaustive-deps/error.invalid-exhaustive-effect-deps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @validateExhaustiveEffectDependencies
1+
// @validateExhaustiveEffectDependencies:"all"
22
import {useEffect} from 'react';
33

44
function Component({x, y, z}) {

0 commit comments

Comments
 (0)