-
Notifications
You must be signed in to change notification settings - Fork 47k
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
fix[eslint-plugin-react-hooks]: Fix error when callback argument is an identifier with an as
expression
#31119
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It no longer will warn in this case about missing dependencies, but not sure if that matters for this kind of code example.
It does. Let's add the code you shared to tests and see why it isn't working.
The proposed fixed is common but very often not the correct one.
@eps1lon I tried adding my code example as a test, and with the versions of eslint, typescript, and typescript-eslint in this repo, it worked fine. It's only with the versions I specified that this is an issue, and not sure exactly the best way to test that. I noticed there is configuration for testing various versions of eslint and typescript-eslint, but not typescript. I'll do some more investigation on my end and see what I can come up with. |
d4e5b1f
to
308f62a
Compare
while ( | ||
callback.type === 'TSAsExpression' || | ||
callback.type === 'AsExpression' | ||
) { | ||
callback = callback.expression; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Digging through the as
expression to find the actual function or identifier then allows the switch statement that follows to analyze properly for edge cases where the callback is an identifier as part of an as expression.
@eps1lon I was able to build a test case that did error with the current version of the plugin which I added to this PR, and also updated the fix to properly handle this case so it does error with this code example. |
as
expression
as
expressionas
expression
errors: [ | ||
{ | ||
message: | ||
"React Hook useCallback has a missing dependency: 'callback'. Either include it or remove the dependency array.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should rather warn that we can't analyze the dependencies of callback
. Using [callback]
in the dependencies would be equivalent to not using useCallback
at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I updated the PR to use the unknown dependencies message when argument is an identifier passed as a function parameter.
Add test case
308f62a
to
f673142
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Summary
Using eslint-plugin-react-hooks with ESLint v9 and TypeScript ESLint v8 causes the following issue:
I can reproduce this with the above mentioned versions and the following file:
How did you test this change?
Added a test case.