-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
react-hooks/rules-of-hooks
: Add support for do/while
loops
#28714
Conversation
Hi @tyxla! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
react-hooks/rules-of-hooks
: Add support for do/while
loops
Comparing: ae90522...ff8ac24 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: (No significant changes) |
@eps1lon I noticed you've been working on the ESLint rules, will you have a chance to take a look? Thanks 🙌 |
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you! |
Bump! This is still a relevant addition to the existing ESLint plugin IMHO. |
cc @mofeiZ perhaps? Sorry for the direct ping, but no one ever replied to this one, and I still think it's useful. |
We need to figure out our strategy for the ESLint plugin. React Compiler also implements the conditional hook call check for all loop types, using a robust general-purpose approach based on the control flow graph. Long-term we expect to replace the existing plugin w the compiler-powered version. But we can take a look and see if it makes sense to proceed here in the meantime. I haven’t had a chance to look at the code yet though. |
@tyxla is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
Thank you @josephsavona 🙌 FWIW, we're also interested in leveraging the React Compiler eslint plugin (we've been experimenting with it), and I'd be happy to come up with a fix for it if that same bug is present. Sounds like a good opportunity to learn more about its codebase. |
I appreciate the offer! But what I meant is that React Compiler already correctly handles do..while and all other forms of loops when detecting conditional hooks. But definitely try it out and let us know if you see any issues! In the meantime we’ll review the code here. |
That's even better, thanks for clarifying! |
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
Not stale IMHO. |
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.
@tyxla left a few questions. I agree with @josephsavona, react compiler-based eslint does correctly detect and fix this, but we know that not everyone has upgraded yet
33a8918
to
33cb28f
Compare
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@mofeiZ, thanks for the review and the valuable feedback! I've addressed it all, and this PR should be ready for one last look. 👀 |
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 for the fix and new test fixtures @tyxla!
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> Currently, `react-hooks/rules-of-hooks` does not support `do/while` loops - I've also reported this in #28713. This PR takes a stab at adding support for `do/while` by following the same logic we already have for detecting `while` loops. After this PR, any hooks called inside a `do/while` loop will be considered invalid. We're also adding some unit tests to confirm that the behavior is working as expected. Fixes #28713. ## How did you test this change? <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. --> I've added unit tests that cover the case and verified that they pass by running: ``` yarn test packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js --watch ``` I've also verified that the rest of the tests continue to pass by running: ``` yarn test ``` and ``` yarn test --prod ``` DiffTrain build for [9daabc0](9daabc0)
FYI, this change causes false positives when Example of code that now gets (incorrectly) flagged by this rule: const MyComponent = () => {
const [state, setState] = useState(0);
for (let i = 0; i < 10; i++) {
console.log(i);
}
return <div></div>;
}; |
@Mathias-S thanks for the report! We might need to take this a different way. I think I might have a quick solution as I played with a few alternative solutions before. Stay tuned. |
Follow up fix in #31720, @Mathias-S please take a look when you get a chance. |
The latest release of eslint-plugin-react-hooks incorrectly flags uses of hooks as occurring in a loop if a component function body contains a loop. Downgrading to version 5.0.0 is a workaround until the React team publishes a release with the fix in facebook/react#28714 (merged but not yet released). See facebook/react#31687
Summary
Currently,
react-hooks/rules-of-hooks
does not supportdo/while
loops - I've also reported this in #28713.This PR takes a stab at adding support for
do/while
by following the same logic we already have for detectingwhile
loops.After this PR, any hooks called inside a
do/while
loop will be considered invalid.We're also adding some unit tests to confirm that the behavior is working as expected.
Fixes #28713.
How did you test this change?
I've added unit tests that cover the case and verified that they pass by running:
I've also verified that the rest of the tests continue to pass by running:
and