Repository navigation
[eslint-plugin-react-hooks] Ignore hooks in unreachable code in rules-of-hooks - #38450
Open
Krrishkumar125 wants to merge 1 commit into
Open
Krrishkumar125 wants to merge 1 commit into
Krrishkumar125 wants to merge 1 commit into
Conversation
…-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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #38438.
rules-of-hookskept a singlecodePathSegmentStackshared by every code path. The code path analyzer only emitsonCodePathSegmentStartfor reachable segments, so a hook call after athroworreturnhas no active segment of its own and was recorded against whatever was left on the stack:undefinedandonCodePathEndcrashed ESLint withCannot read properties of undefined (reading 'reachable').Programsegment, 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
onCodePathStartand popped inonCodePathEnd, 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.reachablecheck already intends.How did you test this change?
Added two valid cases to
ESLintRulesOfHooks-test.js, the two repros from the issue: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:
jestinpackages/eslint-plugin-react-hooksafterbuild-compiler.shandlink-compiler.sh, as CI does: 4 suites, 5112 tests passedtsc --noEmitin the package: cleanprettier --checkandeslinton the changed files: cleanNote: #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.