[compiler] Add typing for useContext hook
In the future, we can use this to identify useContext calls. ghstack-source-id: 01d7b0941ccd09f65346eb5431aa53fe361ce5ed Pull Request resolved: https://github.com/facebook/react/pull/30546
Sathya Gunsasekaran committed
Jul 31, 2024 at 14:35 UTC
f5f9899bee85097203c18866a8bd5208b771cc22
3 files changed
+21
-9
compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts
+14
-9
@@ -10,6 +10,7 @@ import {
10
BUILTIN_SHAPES,
11
BuiltInArrayId,
12
BuiltInUseActionStateId,
13
+ BuiltInUseContextHookId,
14
BuiltInUseEffectHookId,
15
BuiltInUseInsertionEffectHookId,
16
BuiltInUseLayoutEffectHookId,
@@ -245,15 +246,19 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
246
const REACT_APIS: Array<[string, BuiltInType]> = [
247
[
248
'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
+ ),
262
],
263
[
264
'useState',
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
+6
@@ -1599,6 +1599,12 @@ export function isUseInsertionEffectHookType(id: Identifier): boolean {
1599
);
1600
}
1601
1602
+export function isUseContextHookType(id: Identifier): boolean {
1603
+ return (
1604
+ id.type.kind === 'Function' && id.type.shapeId === 'BuiltInUseContextHook'
1605
+ );
1606
+}
1607
+
1608
export function getHookKind(env: Environment, id: Identifier): HookKind | null {
1609
return getHookKindForType(env, id.type);
1610
}
compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts
+1
@@ -208,6 +208,7 @@ export const BuiltInUseInsertionEffectHookId = 'BuiltInUseInsertionEffectHook';
208
export const BuiltInUseOperatorId = 'BuiltInUseOperator';
209
export const BuiltInUseReducerId = 'BuiltInUseReducer';
210
export const BuiltInDispatchId = 'BuiltInDispatch';
211
+export const BuiltInUseContextHookId = 'BuiltInUseContextHook';
212
213
// ShapeRegistry with default definitions for built-ins.
214
export const BUILTIN_SHAPES: ShapeRegistry = new Map();