Skip to content

[eslint-plugin-react-hooks] Ignore hooks in unreachable code in rules-of-hooks - #38450

Open
Krrishkumar125 wants to merge 1 commit into
react:mainfrom
Krrishkumar125:fix/rules-of-hooks-unreachable
Open

Krrishkumar125 wants to merge 1 commit into
react:mainfrom
Krrishkumar125:fix/rules-of-hooks-unreachable

Conversation

@Krrishkumar125

Copy link
Copy Markdown

Summary

Fixes #38438.

rules-of-hooks kept a single codePathSegmentStack shared by every code path. The code path analyzer only emits onCodePathSegmentStart for reachable segments, so a hook call after a throw or return has no active segment of its own and was recorded against whatever was left on the stack:

  • At the top level the stack was empty, so the hook was stored under undefined and onCodePathEnd crashed ESLint with Cannot read properties of undefined (reading 'reachable').
  • Inside a component, the hook picked up the enclosing Program segment, which has a single path, so the rule reported a false "called conditionally" error.

This change keeps one segment stack per code path, pushed in onCodePathStart and popped in onCodePathEnd, and skips a hook call when its code path has no active segment. Unreachable hooks are left to other rules, which is what the existing !segment.reachable check already intends.

How did you test this change?

Added two valid cases to ESLintRulesOfHooks-test.js, the two repros from the issue:

throw new Error('fail');
useState(0);
function ComponentWithUnreachableHook({cond}) {
  if (cond) return null;
  return <div />;
  useState(0);
}

Without the fix the new cases fail (24 failures across the parser and ESLint version matrix, the first one being the crash). With the fix:

  • jest in packages/eslint-plugin-react-hooks after build-compiler.sh and link-compiler.sh, as CI does: 4 suites, 5112 tests passed
  • tsc --noEmit in the package: clean
  • prettier --check and eslint on the changed files: clean

Note: #38449 covers the same issue. The branch for this change (commit b195ea4) was on my fork before that PR was opened, so I am opening this too. Happy to close this one if the maintainers prefer to go with #38449.

…-of-hooks

rules-of-hooks kept one segment stack for every code path. A hook call
after a throw or return has no active segment of its own, so it was
recorded against whatever segment was left on the stack. At the top
level that was undefined, which crashed ESLint in onCodePathEnd. Inside
a component it was the enclosing Program segment, which produced a
false conditional hook error.

Keep a segment stack per code path, and skip a hook call when its code
path has no active segment. Unreachable code is left to other rules,
as the existing reachability check already intends.

Fixes react#38438
Copilot AI balanced review requested due to automatic review settings November 29, 2026 11:41
@meta-cla meta-cla Bot added the CLA Signed label Nov 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

Copy link
Copy Markdown

A size report will appear here when the build finishes.

Generated by sizebot against b195ea4

This branch has not been deployed

No deployments
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.

[eslint-plugin-react-hooks] rules-of-hooks crashes on top-level unreachable hooks and misreports unreachable hooks inside components

2 participants