-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[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
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair, lets try it out
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 #29160 and #29196.This PR will treat objects named as
ref
or if their names end with the substringRef
, and contain a property namedcurrent
, as React refs.In the above example,
ref
andmyRef
will be treated as React refs.