[compiler] Fix custom hook with use() not being compiled#661
Conversation
Custom hooks whose only invocation is the `use()` function are skipped by the compiler. This fixture demonstrates the current (broken) output where `useMyContext` is not compiled. Ref #35960
The `isHookName` check in Program.ts used the regex `/^use[A-Z0-9]/` which doesn't match `use()`. This caused custom hooks whose only invocation is `use()` to be skipped in infer mode. Fixes #35960
Greptile SummaryFixes a bug where custom hooks whose only hook invocation is React's
Confidence Score: 4/5
Important Files Changed
Last reviewed commit: 2d02a52 |
|
|
||
| function isHookName(s: string): boolean { | ||
| return /^use[A-Z0-9]/.test(s); | ||
| return /^use[A-Z0-9]/.test(s) || s === 'use'; |
There was a problem hiding this comment.
Duplicate isHookName not updated
There is a separate exported isHookName function in Environment.ts:1037 that still uses the old regex /^use[A-Z0-9]/ without the || s === 'use' addition. While use is already registered as a known global in Globals.ts (so the Environment.ts version may not cause issues for standard React imports), the two functions could drift out of sync and cause subtle bugs for edge cases (e.g., module-local bindings named use). Consider applying the same fix there for consistency, or refactoring to share a single isHookName implementation.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
Line: 898
Comment:
**Duplicate `isHookName` not updated**
There is a separate exported `isHookName` function in `Environment.ts:1037` that still uses the old regex `/^use[A-Z0-9]/` without the `|| s === 'use'` addition. While `use` is already registered as a known global in `Globals.ts` (so the `Environment.ts` version may not cause issues for standard React imports), the two functions could drift out of sync and cause subtle bugs for edge cases (e.g., module-local bindings named `use`). Consider applying the same fix there for consistency, or refactoring to share a single `isHookName` implementation.
How can I resolve this? If you propose a fix, please make it concise.
Mirror of facebook/react#35964
Original author: carlbergman
Summary
Fix detection of
use()in custom hooks for infer mode. TheisHookNamecheck used the regex/^use[A-Z0-9]/which doesn't matchuse. This caused custom hooks whose only invocation isuse()to be skipped in infer mode.This is the second PR per the development guide. The first PR is react/react#35963.
Fixes react/react#35960
How did you test this change?
yarn snap