-
-
Notifications
You must be signed in to change notification settings - Fork 465
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
🐛 useHookAtTopLevel
doesn't flag hook usages after an early return
#696
Comments
Thank you @arendjr for reporting. We would also like a reproduction for the eslint hook rule, so we can confirm the incorrect behaviour |
I don’t know how to make an ESLint reproduction, but I can link you the source that confirms this should be flagged: https://legacy.reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
(emphasis mine) |
I saw how to make a reproduction in another issue, so you can use this link: https://stackblitz.com/edit/node-jfks6q?file=src%2Findex.js
|
Environment information
What happened?
When a React component contains an early return, calls to hooks after that point are not correctly identified as being conditional.
For instance, see the following playground example: https://www.biomejs.dev/playground?lintRules=all&code=aQBtAHAAbwByAHQAIAB7ACAAdQBzAGUAUwB0AGEAdABlACAAfQAgAGYAcgBvAG0AIAAiAHIAZQBhAGMAdAAiADsACgAKAGYAdQBuAGMAdABpAG8AbgAgAE0AeQBDAG8AbQBwAG8AbgBlAG4AdAAoAHsAIABiAGEAcgAgAH0AKQAgAHsACgAgACAAaQBmACAAKAAhAGIAYQByACkAIAB7AAoAIAAgACAAIAByAGUAdAB1AHIAbgAgAG4AdQBsAGwAOwAKACAAIAB9AAoACgAgACAAYwBvAG4AcwB0ACAAWwBmAG8AbwAsACAAcwBlAHQARgBvAG8AXQAgAD0AIAB1AHMAZQBTAHQAYQB0AGUAKAAxACkAOwAKAAoAIAAgAHIAZQB0AHUAcgBuACAAPABkAGkAdgA%2BAHsAZgBvAG8AfQA8AC8AZABpAHYAPgA7AAoAfQA%3D
Expected result
The call to
useState()
should be flagged as conditional.As a sidenote, I think it would be better if the
useHookAtTopLevel
rule were namednoConditionalHookCalls
instead, since that's its real purpose. As this example shows, being at the top-level isn't a guarantee something is not conditional.Code of Conduct
The text was updated successfully, but these errors were encountered: