Skip to content

[eslint-plugin-react-hooks] Include cycle-entry segment in cyclic set#36324

Open
thomasbuilds wants to merge 1 commit intofacebook:mainfrom
thomasbuilds:main
Open

[eslint-plugin-react-hooks] Include cycle-entry segment in cyclic set#36324
thomasbuilds wants to merge 1 commit intofacebook:mainfrom
thomasbuilds:main

Conversation

@thomasbuilds
Copy link
Copy Markdown

countPathsFromStart / countPathsToEnd in RulesOfHooks populate a cyclic set of code-path segments so that hooks inside loops can be reported as "may be executed more than once". When a cycle is detected, the code slices the pathList starting at indexOf(segment.id) + 1 — which excludes the repeated segment itself (the cycle-entry / loop-header segment) from the cyclic set.

As a result, a hook that lives directly in the test expression of a while or for loop is not reported, even though it is called on every iteration:

function Component() {
  while (useHook()) { /* ... */ }   // previously not reported
}

function Component() {
  for (; useHook(); ) { /* ... */ } // previously not reported
}

The do { … } while (useHook()) case happened to be reported, but only because of a separate isInsideDoWhileLoop fallback check on the hook node — there was no equivalent for plain while / for, so the bug was invisible there.

The fix slices from the repeated segment itself (indexOf(segment.id) instead of + 1), so every segment participating in the cycle — including the cycle head — is recorded. The change is applied symmetrically to both countPathsFromStart and countPathsToEnd.

How did you test this change?

  • Added two new invalid cases to ESLintRulesOfHooks-test.js asserting loopError('useHookInsideLoop') for:

    • while (useHookInsideLoop()) { foo(); }
    • for (; useHookInsideLoop(); ) { foo(); }

    Both cases fail on main (no error reported) and pass with the fix.

  • Reproduced the underlying off-by-one with a standalone simulation of the cycle-detection logic over the CFG shape ESLint produces for while (useHook()) { … } (segments: start → test → body → test, test → end):

    OLD slice(indexOf + 1):  cyclic = [ 'body' ]
    NEW slice(indexOf):      cyclic = [ 'test', 'body' ]
    

    The old behavior drops the test segment, which is exactly where the hook in the condition lives — matching the observed false negative.

  • Existing tests (including the do { … } while (useHook()) case, and loop-body cases like while (a) { useHook1(); … }) are unaffected: their reported hooks sit in non-header segments that were already captured by the old slice.

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 21, 2026

Hi @thomasbuilds!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Apr 21, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 21, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant