-
-
Notifications
You must be signed in to change notification settings - Fork 918
fix(linter): recognize stable hook results declared with let
#8927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mdevils
merged 4 commits into
biomejs:main
from
littleKitchen:fix/8907-let-useState-stable
Feb 2, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
647b957
fix(linter): recognize stable hook results declared with `let`
littleKitchen ccaa096
check for reassignments before treating let binding as stable
littleKitchen 5211b7a
add changeset for #8907 fix
littleKitchen caa1010
chore: replace manual changeset with generated one
littleKitchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Fixed #8907: `useExhaustiveDependencies` now correctly recognizes stable hook results (like `useState` setters and `useRef` values) when declared with `let`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue8907.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* should not generate diagnostics */ | ||
|
|
||
| import { useEffect, useState, useRef } from "react"; | ||
|
|
||
| // Issue #8907: useState setters and refs declared with `let` should be stable | ||
| export const Component = () => { | ||
| let [a, setA] = useState(0); | ||
| let b = useRef(""); | ||
|
|
||
| useEffect(() => { | ||
| setA(1); | ||
| b.current = "test"; | ||
| }, []); | ||
| }; | ||
|
|
||
| // Also test with const for comparison (should still work) | ||
| export const Component2 = () => { | ||
| const [a, setA] = useState(0); | ||
| const b = useRef(""); | ||
|
|
||
| useEffect(() => { | ||
| setA(1); | ||
| b.current = "test"; | ||
| }, []); | ||
| }; | ||
33 changes: 33 additions & 0 deletions
33
crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue8907.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: issue8907.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should not generate diagnostics */ | ||
|
|
||
| import { useEffect, useState, useRef } from "react"; | ||
|
|
||
| // Issue #8907: useState setters and refs declared with `let` should be stable | ||
| export const Component = () => { | ||
| let [a, setA] = useState(0); | ||
| let b = useRef(""); | ||
|
|
||
| useEffect(() => { | ||
| setA(1); | ||
| b.current = "test"; | ||
| }, []); | ||
| }; | ||
|
|
||
| // Also test with const for comparison (should still work) | ||
| export const Component2 = () => { | ||
| const [a, setA] = useState(0); | ||
| const b = useRef(""); | ||
|
|
||
| useEffect(() => { | ||
| setA(1); | ||
| b.current = "test"; | ||
| }, []); | ||
| }; | ||
|
|
||
| ``` |
21 changes: 21 additions & 0 deletions
21
...biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue8907Reassigned.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { useEffect, useState, useRef } from "react"; | ||
|
|
||
| // Issue #8907: If a let binding is reassigned, it's no longer stable | ||
| export const Component3 = () => { | ||
| let [a, setA] = useState(0); | ||
| setA = () => {}; // reassigned! | ||
|
|
||
| useEffect(() => { | ||
| setA(1); // should now require setA in deps | ||
| }, []); | ||
| }; | ||
|
|
||
| // Same for useRef | ||
| export const Component4 = () => { | ||
| let b = useRef(""); | ||
| b = { current: "other" }; // reassigned! | ||
|
|
||
| useEffect(() => { | ||
| console.log(b.current); // should now require b in deps | ||
| }, []); | ||
| }; |
96 changes: 96 additions & 0 deletions
96
..._js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue8907Reassigned.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: issue8907Reassigned.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| import { useEffect, useState, useRef } from "react"; | ||
|
|
||
| // Issue #8907: If a let binding is reassigned, it's no longer stable | ||
| export const Component3 = () => { | ||
| let [a, setA] = useState(0); | ||
| setA = () => {}; // reassigned! | ||
|
|
||
| useEffect(() => { | ||
| setA(1); // should now require setA in deps | ||
| }, []); | ||
| }; | ||
|
|
||
| // Same for useRef | ||
| export const Component4 = () => { | ||
| let b = useRef(""); | ||
| b = { current: "other" }; // reassigned! | ||
|
|
||
| useEffect(() => { | ||
| console.log(b.current); // should now require b in deps | ||
| }, []); | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| issue8907Reassigned.js:8:3 lint/correctness/useExhaustiveDependencies FIXABLE ━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × This hook does not specify its dependency on setA. | ||
|
|
||
| 6 │ setA = () => {}; // reassigned! | ||
| 7 │ | ||
| > 8 │ useEffect(() => { | ||
| │ ^^^^^^^^^ | ||
| 9 │ setA(1); // should now require setA in deps | ||
| 10 │ }, []); | ||
|
|
||
| i This dependency is being used here, but is not specified in the hook dependency list. | ||
|
|
||
| 8 │ useEffect(() => { | ||
| > 9 │ setA(1); // should now require setA in deps | ||
| │ ^^^^ | ||
| 10 │ }, []); | ||
| 11 │ }; | ||
|
|
||
| i React relies on hook dependencies to determine when to re-compute Effects. | ||
| Failing to specify dependencies can result in Effects not updating correctly when state changes. | ||
| These "stale closures" are a common source of surprising bugs. | ||
|
|
||
| i Either include it or remove the dependency array. | ||
|
|
||
| i Unsafe fix: Add the missing dependency to the list. | ||
|
|
||
| 10 │ ··},·[setA]); | ||
| │ ++++ | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| issue8907Reassigned.js:18:3 lint/correctness/useExhaustiveDependencies FIXABLE ━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × This hook does not specify its dependency on b.current. | ||
|
|
||
| 16 │ b = { current: "other" }; // reassigned! | ||
| 17 │ | ||
| > 18 │ useEffect(() => { | ||
| │ ^^^^^^^^^ | ||
| 19 │ console.log(b.current); // should now require b in deps | ||
| 20 │ }, []); | ||
|
|
||
| i This dependency is being used here, but is not specified in the hook dependency list. | ||
|
|
||
| 18 │ useEffect(() => { | ||
| > 19 │ console.log(b.current); // should now require b in deps | ||
| │ ^^^^^^^^^ | ||
| 20 │ }, []); | ||
| 21 │ }; | ||
|
|
||
| i React relies on hook dependencies to determine when to re-compute Effects. | ||
| Failing to specify dependencies can result in Effects not updating correctly when state changes. | ||
| These "stale closures" are a common source of surprising bugs. | ||
|
|
||
| i Either include it or remove the dependency array. | ||
|
|
||
| i Unsafe fix: Add the missing dependency to the list. | ||
|
|
||
| 20 │ ··},·[b.current]); | ||
| │ +++++++++ | ||
|
|
||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.