diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts index 9b3cfebb9af9d..5e90401c6882a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts @@ -10,6 +10,7 @@ import { BUILTIN_SHAPES, BuiltInArrayId, BuiltInUseActionStateId, + BuiltInUseContextHookId, BuiltInUseEffectHookId, BuiltInUseInsertionEffectHookId, BuiltInUseLayoutEffectHookId, @@ -245,15 +246,19 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [ const REACT_APIS: Array<[string, BuiltInType]> = [ [ 'useContext', - addHook(DEFAULT_SHAPES, { - positionalParams: [], - restParam: Effect.Read, - returnType: {kind: 'Poly'}, - calleeEffect: Effect.Read, - hookKind: 'useContext', - returnValueKind: ValueKind.Frozen, - returnValueReason: ValueReason.Context, - }), + addHook( + DEFAULT_SHAPES, + { + positionalParams: [], + restParam: Effect.Read, + returnType: {kind: 'Poly'}, + calleeEffect: Effect.Read, + hookKind: 'useContext', + returnValueKind: ValueKind.Frozen, + returnValueReason: ValueReason.Context, + }, + BuiltInUseContextHookId, + ), ], [ 'useState', diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts index 4578a9b874aef..f0ada2d3708fb 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts @@ -1599,6 +1599,12 @@ export function isUseInsertionEffectHookType(id: Identifier): boolean { ); } +export function isUseContextHookType(id: Identifier): boolean { + return ( + id.type.kind === 'Function' && id.type.shapeId === 'BuiltInUseContextHook' + ); +} + export function getHookKind(env: Environment, id: Identifier): HookKind | null { return getHookKindForType(env, id.type); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts index 63c4d7f6f9ece..3d377dba59dc9 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts @@ -208,6 +208,7 @@ export const BuiltInUseInsertionEffectHookId = 'BuiltInUseInsertionEffectHook'; export const BuiltInUseOperatorId = 'BuiltInUseOperator'; export const BuiltInUseReducerId = 'BuiltInUseReducer'; export const BuiltInDispatchId = 'BuiltInDispatch'; +export const BuiltInUseContextHookId = 'BuiltInUseContextHook'; // ShapeRegistry with default definitions for built-ins. export const BUILTIN_SHAPES: ShapeRegistry = new Map();