-
Notifications
You must be signed in to change notification settings - Fork 45.8k
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
Bug: eslint-plugin-react-hooks rules-of-hooks misses hook usage in class properties with typescript-eslint v5+ parser #27431
Comments
Thank you for raising this issue with such a detailed analysis. I don't think the code you posted should be flagged. Since the class property is detected as a hook, you can call hooks inside it. The code would only be problematic if So this seems to me like a bug that was fixed by switching to newer TypeScript ESLint parsers. Could you add your snippet to the changes you proposed in #27435 to confirm it would still be allowed. |
I've run into this in our code where the ESLint plugin misses that a hook is used within a property of a class component. Here's an example based on our code: export class List extends React.PureComponent {
renderRow = () => {
const [value] = useState('value');
return <li>{value}</li>;
};
render() {
return (
<ul>
{this.renderRow()}
</ul>
);
}
} This isn't caught by ESLint because |
This comment was marked as off-topic.
This comment was marked as off-topic.
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Sure. It's likely unnecessary since a class method implies the method is stateful which would likely not behave as expected at runtime. So any hook as a class method should be able to be rewritten as a normal function. I guess we previously assume a hook in a class implies this class is a React component. We should probably continue with that logic. |
React version: any version with hooks
eslint-plugin-react-hooks
version: 4.6.0 / all (still repros in 5.0.0-canary-f81c0f1ed-20230927)Steps To Reproduce
Link to code example: https://github.com/ecraig12345/repro-react-hooks-eslint
eslint-plugin-react-hooks
(any version)@typescript-eslint/parser
v5 or greatereslint
v6 or greatertypescript
(any version)@typescript-eslint/parser
and enable thereact-hooks/rules-of-hooks
ruleThe current behavior
No errors are flagged.
The expected behavior
One error is flagged.
The behavior was correct with
@typescript-eslint/parser
v4 (as demonstrated in the full repro).Root cause
In
@typescript-eslint/parser
v5, the AST node typeClassProperty
was renamed toPropertyDefinition
. Therules-of-hooks
rule needs to be updated to check for this additional type.The text was updated successfully, but these errors were encountered: