Skip to content
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

feat: add rules_of_hooks rule #1346

Merged
merged 8 commits into from
Dec 10, 2024
Merged

feat: add rules_of_hooks rule #1346

merged 8 commits into from
Dec 10, 2024

Conversation

marvinhagemeister
Copy link
Contributor

This PR implements the rules-of-hooks eslint rule, see https://react.dev/reference/rules/rules-of-hooks . It ensures that hooks are only used inside components and that they are called unconditionally.

docs/rules/rules_of_hooks.md Outdated Show resolved Hide resolved
docs/rules/rules_of_hooks.md Outdated Show resolved Hide resolved
Comment on lines +65 to +73
function Component() {
const [count, setCount] = useState(0);
// ...
}

function useCustomHook() {
const [count, setCount] = useState(0);
// ...
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this one to show all "bad" examples written in a correct form?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the only correct ones for all bad examples.

src/rules/rules_of_hooks.rs Outdated Show resolved Hide resolved
Comment on lines +274 to +277
if let Some(ch) = name.chars().next() {
return ch.is_uppercase();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this hit? Could you add a small unit test for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hit in pretty much all bad variants. It tests if the enclosing function is a component.

@marvinhagemeister marvinhagemeister merged commit 8b3e5ce into main Dec 10, 2024
7 checks passed
@marvinhagemeister marvinhagemeister deleted the rules-of-hooks branch December 10, 2024 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants