This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
🐛 useExhaustiveDependencies doesn't support function syntax, or React.use* hooks #4330
Open
1 task done
Labels
Comments
Amnesthesia
added
the
S-To triage
Status: user report of a possible bug that needs to be triaged
label
Mar 29, 2023
ematipico
added
S-Bug: confirmed
Status: report has been confirmed as a valid bug
A-Linter
Area: linter
and removed
S-To triage
Status: user report of a possible bug that needs to be triaged
labels
May 8, 2023
cc @rome/core-contributors |
I'll take this issue. |
Adding to this, this rule does not warn when dependencies are defined after the dependency check/array. Does not trigger warning (dependency defined after useEffect(() => {
if (!user) return;
updateUserForm(user);
}, [user]);
const updateUserForm = (updatedUser: any) => {
userForm.reset({ ... });
}; Triggers warning (dependency defined before const updateUserForm = (updatedUser: any) => {
userForm.reset({ ... });
};
useEffect(() => {
if (!user) return;
updateUserForm(user);
}, [user]); |
3 tasks
I think the issues related to the original comment #4330 (comment) were fixed by #4571. Now, I'm looking into #4330 (comment) Note: I seem that the following lines are related to the issuse. tools/crates/rome_js_analyze/src/react/hooks.rs Lines 62 to 63 in 60ee106
tools/crates/rome_js_semantic/src/semantic_model/closure.rs Lines 130 to 164 in 60ee106
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
Environment information
What happened?
1. useExhaustiveDependencies doesn't understand where a hook is coming from and only reads by name
lint/nursery/useExhaustiveDependencies
will not recognize stable hooks if those hooks are not imported asimport { useState } from 'react'
For example,
import * as React from 'react'
and thenReact.useState()
, will not be treated as a hook with a stable callback in index position 1. I have tried to figure out if I can passReact.useState
tooptions.stables
, but this option has been removed in recent versions (and it didn't work when I tried on the versions where it existed)2. useExhaustiveDependencies doesn't understand named functions
Another issue related to this is that if you declare a
useCallback
like this:Then Rome will advise you to remove all dependencies
3. useExhaustiveDependencies doesn't understand multiple hooks with the same name and different parameters
We have a custom
useMemo
hook that allows passing in an optional customisEqual
argument as a third argument, and rome will crash trying to parse this because it doesn't look like what it expects a hook named 'useMemo' should look like✖ processing panicked: index out of bounds: the len is 2 but the index is 2
4. useExhaustiveDependencies is not very good at parsing optional chaining
If a hook does something like this:
Then
selectedArticle.redirectUrl
will receive a complaint with:Because this rule doesn't understand that
selectedArticle?.redirectUrl
is the same asselectedArticle.redirectUrl
, it was just checked for whether or not it was undefineduseEffect is sometimes not checked, or ignored?
I was using a component like this for testing this rule, removing dependencies I know it should complain about and adding extras as well:
Expected result
I would expect Rome to treat any function declaration in a hook as a function regardless of whether its a named or anonymous function and whether or not its an arrow function.
I would also expect rome to understand where a hook is coming from and not just parse the name, e.g
useState
, but understand thatReact.useState
is the same asuseState
andimport { useState as useWhatever } from 'react'
is stilluseState
I'm not sure if this is possible with how rome parses the AST tree, if this is just a bad linting rule, or if rome is really that opinionated on how to write code?
Code of Conduct
The text was updated successfully, but these errors were encountered: