Skip to content

Commit 382786b

Browse files
fix(lint): remove useExhaustiveDependencies spurious errors on dependency-first custom hooks; improve docs (#8401)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent fc32352 commit 382786b

31 files changed

+1337
-449
lines changed

.changeset/old-seas-drum.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
[`useExhaustiveDependencies`](https://biomejs.dev/linter/rules/use-exhaustive-dependencies) now correctly validate custom hooks whose dependency arrays come before their callbacks.
6+
7+
Previously, a logical error caused the rule to be unable to detect dependency arrays placed before hook callbacks, producing spurious errors and blocking further diagnostics.
8+
```json
9+
{
10+
"linter": {
11+
"rules": {
12+
"correctness": {
13+
"useExhaustiveDependencies": {
14+
"level": "error",
15+
"options": {
16+
"hooks": [
17+
{ "name": "doSomething", "closureIndex": 2, "dependenciesIndex": 0 }
18+
]
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
```
26+
27+
```js
28+
function component() {
29+
let thing = 5;
30+
// The rule will now correctly recognize `thing` as being specified
31+
// instead of erroring due to "missing" dependency arrays
32+
doSomething([thing], "blah", () => {console.log(thing)})
33+
}
34+
```
35+
36+
The rule documentation & diagnostic messages have also been reworked for improved clarity.

crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_react_rules_via_dependencies.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ test.jsx:6:11 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━
121121
```block
122122
test.jsx:6:20 lint/correctness/useExhaustiveDependencies FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
123123
124-
× This hook specifies more dependencies than necessary: local
124+
× This hook specifies more dependencies than necessary: local.
125125
126126
4function Component2() {
127127
5const [local,SetLocal] = useState(0);
@@ -139,6 +139,10 @@ test.jsx:6:20 lint/correctness/useExhaustiveDependencies FIXABLE ━━━━
139139
9 │ }
140140
10
141141
142+
i React relies on hook dependencies to determine when to re-compute Effects.
143+
Specifying more dependencies than required can lead to unnecessary re-rendering
144+
and degraded performance.
145+
142146
i Unsafe fix: Remove the extra dependencies from the list.
143147
144148
8 │ ····},·[locallocal]);

crates/biome_configuration/src/analyzer/linter/rules.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)