[compiler] Fix set-state-in-effect validation for React.useEffect
#35280
+27
−7
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
I noticed that
React.useEffectbypasses theset-state-in-effectvalidation, as shown below (or see this CodeSandbox).Initially I thought this was an
eslint-plugin-react-hooksissue, but later realized it's a Compiler-level bug.The bug was in the callee extraction for
MethodCall: it checkedreceiver(theReactnamespace) instead ofproperty(theuseEffectmethod). SinceReactis not an effect hook, the validation was skipped.The fix changes
instr.value.receivertoinstr.value.propertyso thatuseEffectis correctly identified as an effect hook.How did you test this change?
Added a
React.useEffectcase toinvalid-setState-in-useEffect.js.Updated
invalid-setState-in-useEffect.expect.mdand checked the Logs: only oneCompileErrorat line 7, missingReact.useEffectat line 10. (See Commit 1)Fixed
ValidateNoSetStateInEffects.ts.Updated
invalid-setState-in-useEffect.expect.mdagain and checked the Logs: now twoCompileErrors are reported (line 7 and line 10). (See Commit 2)Ran and passed all tests in
compiler/packages/babel-plugin-react-compiler(yarn snap:ci).