@samitouri / QOS-React / commits / 9daabc0bf9

`react-hooks/rules-of-hooks`: Add support for `do/while` loops (#28714)

<!-- 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 https://github.com/facebook/react/issues/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 ```

Marin Atanasov committed Oct 22, 2024 at 23:07 UTC 9daabc0bf97805be23f6131be4d84d063a3ff446
2 files changed +64 -1
packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js
+63
@@ -755,6 +755,30 @@ const tests = {
755 `,
756 errors: [loopError('useHookInsideLoop')],
757 },
758 + {
759 + code: normalizeIndent`
760 + // Invalid because it's dangerous and might not warn otherwise.
761 + // This *must* be invalid.
762 + function ComponentWithHookInsideLoop() {
763 + do {
764 + useHookInsideLoop();
765 + } while (cond);
766 + }
767 + `,
768 + errors: [loopError('useHookInsideLoop')],
769 + },
770 + {
771 + code: normalizeIndent`
772 + // Invalid because it's dangerous and might not warn otherwise.
773 + // This *must* be invalid.
774 + function ComponentWithHookInsideLoop() {
775 + do {
776 + foo();
777 + } while (useHookInsideLoop());
778 + }
779 + `,
780 + errors: [loopError('useHookInsideLoop')],
781 + },
782 {
783 code: normalizeIndent`
784 // Invalid because it's dangerous and might not warn otherwise.
@@ -853,6 +877,45 @@ const tests = {
877 `,
878 errors: [loopError('useHook1'), loopError('useHook2', true)],
879 },
880 + {
881 + code: normalizeIndent`
882 + // Invalid because it's dangerous and might not warn otherwise.
883 + // This *must* be invalid.
884 + function useHookInLoops() {
885 + do {
886 + useHook1();
887 + if (a) return;
888 + useHook2();
889 + } while (b);
890 +
891 + do {
892 + useHook3();
893 + if (c) return;
894 + useHook4();
895 + } while (d)
896 + }
897 + `,
898 + errors: [
899 + loopError('useHook1'),
900 + loopError('useHook2'),
901 + loopError('useHook3'),
902 + loopError('useHook4'),
903 + ],
904 + },
905 + {
906 + code: normalizeIndent`
907 + // Invalid because it's dangerous and might not warn otherwise.
908 + // This *must* be invalid.
909 + function useHookInLoops() {
910 + do {
911 + useHook1();
912 + if (a) continue;
913 + useHook2();
914 + } while (b);
915 + }
916 + `,
917 + errors: [loopError('useHook1'), loopError('useHook2', true)],
918 + },
919 {
920 code: normalizeIndent`
921 // Invalid because it's dangerous and might not warn otherwise.
packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
+1 -1
@@ -295,7 +295,7 @@ export default {
295 if (pathList.has(segment.id)) {
296 const pathArray = Array.from(pathList);
297 const cyclicSegments = pathArray.slice(
298 - pathArray.indexOf(segment.id) + 1,
298 + pathArray.indexOf(segment.id) - 1,
299 );
300 for (const cyclicSegment of cyclicSegments) {
301 cyclic.add(cyclicSegment);