Skip to content

Commit f5f9899

Browse files
committed
[compiler] Add typing for useContext hook
In the future, we can use this to identify useContext calls. ghstack-source-id: 01d7b09 Pull Request resolved: #30546
1 parent ce60785 commit f5f9899

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
BUILTIN_SHAPES,
1111
BuiltInArrayId,
1212
BuiltInUseActionStateId,
13+
BuiltInUseContextHookId,
1314
BuiltInUseEffectHookId,
1415
BuiltInUseInsertionEffectHookId,
1516
BuiltInUseLayoutEffectHookId,
@@ -245,15 +246,19 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
245246
const REACT_APIS: Array<[string, BuiltInType]> = [
246247
[
247248
'useContext',
248-
addHook(DEFAULT_SHAPES, {
249-
positionalParams: [],
250-
restParam: Effect.Read,
251-
returnType: {kind: 'Poly'},
252-
calleeEffect: Effect.Read,
253-
hookKind: 'useContext',
254-
returnValueKind: ValueKind.Frozen,
255-
returnValueReason: ValueReason.Context,
256-
}),
249+
addHook(
250+
DEFAULT_SHAPES,
251+
{
252+
positionalParams: [],
253+
restParam: Effect.Read,
254+
returnType: {kind: 'Poly'},
255+
calleeEffect: Effect.Read,
256+
hookKind: 'useContext',
257+
returnValueKind: ValueKind.Frozen,
258+
returnValueReason: ValueReason.Context,
259+
},
260+
BuiltInUseContextHookId,
261+
),
257262
],
258263
[
259264
'useState',

compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,12 @@ export function isUseInsertionEffectHookType(id: Identifier): boolean {
15991599
);
16001600
}
16011601

1602+
export function isUseContextHookType(id: Identifier): boolean {
1603+
return (
1604+
id.type.kind === 'Function' && id.type.shapeId === 'BuiltInUseContextHook'
1605+
);
1606+
}
1607+
16021608
export function getHookKind(env: Environment, id: Identifier): HookKind | null {
16031609
return getHookKindForType(env, id.type);
16041610
}

compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export const BuiltInUseInsertionEffectHookId = 'BuiltInUseInsertionEffectHook';
208208
export const BuiltInUseOperatorId = 'BuiltInUseOperator';
209209
export const BuiltInUseReducerId = 'BuiltInUseReducer';
210210
export const BuiltInDispatchId = 'BuiltInDispatch';
211+
export const BuiltInUseContextHookId = 'BuiltInUseContextHook';
211212

212213
// ShapeRegistry with default definitions for built-ins.
213214
export const BUILTIN_SHAPES: ShapeRegistry = new Map();

0 commit comments

Comments
 (0)