[compiler] Fix false positive in refs rule for refs stored in objects#35819
Open
apoorvdarshan wants to merge 1 commit intofacebook:mainfrom
Open
[compiler] Fix false positive in refs rule for refs stored in objects#35819apoorvdarshan wants to merge 1 commit intofacebook:mainfrom
apoorvdarshan wants to merge 1 commit intofacebook:mainfrom
Conversation
When multiple refs are stored in an object (e.g. `{group1: useRef(), group2: useRef()}`),
the `react-hooks/refs` validation incorrectly reported "Cannot access ref value during
render" on the second property access. This happened because `collectTemporariesSidemap`
aliased PropertyLoad results to the source object, causing `env.set` on the property load
lvalue to corrupt the object's type through the temporary chain.
The fix removes PropertyLoad aliasing from `collectTemporariesSidemap` (the main
validation loop already tracks types correctly through `env.set`), and adjusts the
PropertyLoad case in the main validation to propagate function type info from Structure
types when the value is null.
Fixes facebook#35813
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
react-hooks/refsESLint rule where storing multiple refs in an object (const groupRefs = {group1: useRef(), group2: useRef()}) and passing them as JSX props (ref={groupRefs.group2}) incorrectly reported "Cannot access ref value during render"collectTemporariesSidemapaliased PropertyLoad results to the source object, soenv.seton a property load lvalue would resolve through the temporary chain and corrupt the object's type (fromStructure{Ref}toRef), causing subsequent property loads to produceRefValueinstead ofRefcollectTemporariesSidemap(the main validation loop already tracks types correctly) and adjusts PropertyLoad in the main validation to propagate function type info from Structure types when value is nullFixes #35813
Test plan
allow-passing-refs-in-object-as-propsthat reproduces the false positiveref.currentduring render, calling ref-accessing functions during render)