fix: detect setState in useEffect for anonymous component callbacks passed to HOFs#35934
Open
fresh3nough wants to merge 1 commit intofacebook:mainfrom
Open
fix: detect setState in useEffect for anonymous component callbacks passed to HOFs#35934fresh3nough wants to merge 1 commit intofacebook:mainfrom
fresh3nough wants to merge 1 commit intofacebook:mainfrom
Conversation
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 #35910
The
react-hooks/set-state-in-effectESLint rule failed to detectsetStatecalls insideuseEffectwhen the component was an anonymous callback passed to a higher-order function (e.g.const WrappedComponent = wrap(() => { ... })).Root Cause
Two issues prevented detection:
getFunctionNameinProgram.tsdid not look throughCallExpressionparents. Forconst Foo = wrap(() => { ... }), the arrow function's parent is theCallExpression, not theVariableDeclarator, sogetFunctionNamereturnednulland the compiler never recognized the anonymous function as a component.mayContainReactCodeheuristic inRunReactCompiler.tsonly matchedVariableDeclarationinits that were directlyArrowFunctionExpressionorFunctionExpression. Files containing only HOF-wrapped components (e.g.const Foo = wrap(() => { ... })) would be skipped entirely.Changes
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts: ExtendedgetFunctionNameto look throughCallExpressionparents to theVariableDeclaratorgrandparent, soconst Foo = wrap(() => { ... })correctly gives the inner arrow function the nameFoo.packages/eslint-plugin-react-hooks/src/shared/RunReactCompiler.ts: Extended themayContainReactCodeheuristic to also matchCallExpressioninits in variable declarations with component/hook-like names.packages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.ts: Added test cases for both theset-state-in-effectandimmutabilityrules to cover HOF-wrapped component detection.Steps to Reproduce
Test Plan
All 5086 eslint-plugin-react-hooks tests pass, including 2 new test cases.