Skip to content

Commit a758eac

Browse files
committed
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 #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)
1 parent 48dd4dd commit a758eac

35 files changed

+87
-87
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b8ae38f88b70f8a0ea96421a4355266aafefee7f
1+
9daabc0bf97805be23f6131be4d84d063a3ff446
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b8ae38f88b70f8a0ea96421a4355266aafefee7f
1+
9daabc0bf97805be23f6131be4d84d063a3ff446

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ __DEV__ &&
20212021
exports.useTransition = function () {
20222022
return resolveDispatcher().useTransition();
20232023
};
2024-
exports.version = "19.0.0-www-classic-b8ae38f8-20241018";
2024+
exports.version = "19.0.0-www-classic-9daabc0b-20241022";
20252025
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
20262026
"function" ===
20272027
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ __DEV__ &&
20012001
exports.useTransition = function () {
20022002
return resolveDispatcher().useTransition();
20032003
};
2004-
exports.version = "19.0.0-www-modern-b8ae38f8-20241018";
2004+
exports.version = "19.0.0-www-modern-9daabc0b-20241022";
20052005
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
20062006
"function" ===
20072007
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,4 +824,4 @@ exports.useSyncExternalStore = function (
824824
exports.useTransition = function () {
825825
return ReactSharedInternals.H.useTransition();
826826
};
827-
exports.version = "19.0.0-www-classic-b8ae38f8-20241018";
827+
exports.version = "19.0.0-www-classic-9daabc0b-20241022";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,4 +820,4 @@ exports.useSyncExternalStore = function (
820820
exports.useTransition = function () {
821821
return ReactSharedInternals.H.useTransition();
822822
};
823-
exports.version = "19.0.0-www-modern-b8ae38f8-20241018";
823+
exports.version = "19.0.0-www-modern-9daabc0b-20241022";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ exports.useSyncExternalStore = function (
828828
exports.useTransition = function () {
829829
return ReactSharedInternals.H.useTransition();
830830
};
831-
exports.version = "19.0.0-www-classic-b8ae38f8-20241018";
831+
exports.version = "19.0.0-www-classic-9daabc0b-20241022";
832832
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
833833
"function" ===
834834
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ exports.useSyncExternalStore = function (
824824
exports.useTransition = function () {
825825
return ReactSharedInternals.H.useTransition();
826826
};
827-
exports.version = "19.0.0-www-modern-b8ae38f8-20241018";
827+
exports.version = "19.0.0-www-modern-9daabc0b-20241022";
828828
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
829829
"function" ===
830830
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.classic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17059,11 +17059,11 @@ __DEV__ &&
1705917059
(function () {
1706017060
var internals = {
1706117061
bundleType: 1,
17062-
version: "19.0.0-www-classic-b8ae38f8-20241018",
17062+
version: "19.0.0-www-classic-9daabc0b-20241022",
1706317063
rendererPackageName: "react-art",
1706417064
currentDispatcherRef: ReactSharedInternals,
1706517065
findFiberByHostInstance: getInstanceFromNode,
17066-
reconcilerVersion: "19.0.0-www-classic-b8ae38f8-20241018"
17066+
reconcilerVersion: "19.0.0-www-classic-9daabc0b-20241022"
1706717067
};
1706817068
internals.overrideHookState = overrideHookState;
1706917069
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -17097,7 +17097,7 @@ __DEV__ &&
1709717097
exports.Shape = Shape;
1709817098
exports.Surface = Surface;
1709917099
exports.Text = Text;
17100-
exports.version = "19.0.0-www-classic-b8ae38f8-20241018";
17100+
exports.version = "19.0.0-www-classic-9daabc0b-20241022";
1710117101
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1710217102
"function" ===
1710317103
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.modern.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16502,11 +16502,11 @@ __DEV__ &&
1650216502
(function () {
1650316503
var internals = {
1650416504
bundleType: 1,
16505-
version: "19.0.0-www-modern-b8ae38f8-20241018",
16505+
version: "19.0.0-www-modern-9daabc0b-20241022",
1650616506
rendererPackageName: "react-art",
1650716507
currentDispatcherRef: ReactSharedInternals,
1650816508
findFiberByHostInstance: getInstanceFromNode,
16509-
reconcilerVersion: "19.0.0-www-modern-b8ae38f8-20241018"
16509+
reconcilerVersion: "19.0.0-www-modern-9daabc0b-20241022"
1651016510
};
1651116511
internals.overrideHookState = overrideHookState;
1651216512
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -16540,7 +16540,7 @@ __DEV__ &&
1654016540
exports.Shape = Shape;
1654116541
exports.Surface = Surface;
1654216542
exports.Text = Text;
16543-
exports.version = "19.0.0-www-modern-b8ae38f8-20241018";
16543+
exports.version = "19.0.0-www-modern-9daabc0b-20241022";
1654416544
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1654516545
"function" ===
1654616546
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)