[compiler] Treat ref-like named objects as refs#29916
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts
Outdated
Show resolved
Hide resolved
...kages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ref-like-name-as-refs-3.js
Show resolved
Hide resolved
josephsavona
left a comment
There was a problem hiding this comment.
Nice! Thoughts on checking that other properties (besides .current) are not accessed? If they are it's a good indication that the value wasn't actually a ref.
If a component uses the `useRef` hook directly then we type it's return value as a ref. But if it's wrapped in a custom hook then we lose out on this type information as the compiler doesn't look at the hook definition. This has resulted in some false positives in our analysis like the ones reported in facebook#29160 and facebook#29196. This PR will treat objects named as `ref` or if their names end with the substring `Ref`, and contain a property named `current`, as React refs. ``` const ref = useMyRef(); const myRef = useMyRef2(); useEffect(() => { ref.current = ...; myRef.current = ...; }) ``` In the above example, `ref` and `myRef` will be treated as React refs.
There's still going to be false positives in either case, I'm not sure if this worth the extra complexity? Lets land this and try it on our codebase to see if there are false positives? |
|
Comparing: 92219ff...4bb18bd Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
josephsavona
left a comment
There was a problem hiding this comment.
That's fair, lets try it out
If a component uses the
useRefhook directly then we type it's return value as a ref. But if it's wrapped in a custom hook then we lose out on this type information as the compiler doesn't look at the hook definition. This has resulted in some false positives in our analysis like the ones reported in #29160 and #29196.This PR will treat objects named as
refor if their names end with the substringRef, and contain a property namedcurrent, as React refs.In the above example,
refandmyRefwill be treated as React refs.