Define types for useState/useRef
Defines 4 new types: * Return type of `useState()`, which has properties "0" and "1" to allow us to infer the types when destructuring * Type of useState() set state function * Return type of `useRef()` so we know what is a ref * Type of ref.current, so we know what is a ref *value* Example: <img width="1670" alt="Screenshot 2023-05-24 at 9 59 37 AM" src="https://github.com/facebook/react-forget/assets/6425824/3ee7d04a-fda3-4b7b-89b7-d205d9a6fd0d">
Joe Savona committed
May 24, 2023 at 09:54 UTC
37d044763b45d43fc8e0599cd3926f82125a0360
2 files changed
+36
-4
compiler/forget/src/HIR/Globals.ts
+4
-2
@@ -9,6 +9,8 @@ import { Effect, ValueKind } from "./HIR";
9
import {
10
BUILTIN_SHAPES,
11
BuiltInArrayId,
12
+ BuiltInUseRefId,
13
+ BuiltInUseStateId,
14
ShapeRegistry,
15
addFunction,
16
addHook,
@@ -248,7 +250,7 @@ const BUILTIN_HOOKS: Array<[string, FunctionType]> = [
250
addHook(DEFAULT_SHAPES, [], {
251
positionalParams: [],
252
restParam: Effect.Freeze,
251
- returnType: { kind: "Poly" },
253
+ returnType: { kind: "Object", shapeId: BuiltInUseStateId },
254
calleeEffect: Effect.Read,
255
hookKind: "useState",
256
returnValueKind: ValueKind.Frozen,
@@ -259,7 +261,7 @@ const BUILTIN_HOOKS: Array<[string, FunctionType]> = [
261
addHook(DEFAULT_SHAPES, [], {
262
positionalParams: [],
263
restParam: Effect.Capture,
262
- returnType: { kind: "Poly" },
264
+ returnType: { kind: "Object", shapeId: BuiltInUseRefId },
265
calleeEffect: Effect.Read,
266
hookKind: "useRef",
267
returnValueKind: ValueKind.Mutable,
compiler/forget/src/HIR/ObjectShape.ts
+32
-2
@@ -40,9 +40,10 @@ function createAnonId(): string {
40
export function addFunction(
41
registry: ShapeRegistry,
42
properties: Iterable<[string, BuiltInType | PolyType]>,
43
- fn: Omit<FunctionSignature, "hookKind">
43
+ fn: Omit<FunctionSignature, "hookKind">,
44
+ id: string | null = null
45
): FunctionType {
45
- const shapeId = createAnonId();
46
+ const shapeId = id ?? createAnonId();
47
addShape(registry, shapeId, properties, {
48
...fn,
49
hookKind: null,
@@ -157,6 +158,10 @@ export type ObjectShape = {
158
export type ShapeRegistry = Map<string, ObjectShape>;
159
export const BuiltInArrayId = "BuiltInArray";
160
export const BuiltInObjectId = "BuiltInObject";
161
+export const BuiltInUseStateId = "BuiltInUseState";
162
+export const BuiltInSetStateId = "BuiltInSetState";
163
+export const BuiltInUseRefId = "BuiltInUseRefId";
164
+export const BuiltInRefValueId = "BuiltInRefValue";
165
166
/**
167
* ShapeRegistry with default definitions for built-ins.
@@ -218,6 +223,31 @@ addObject(BUILTIN_SHAPES, BuiltInObjectId, [
223
// hasOwnProperty, isPrototypeOf, propertyIsEnumerable, toLocaleString, valueOf
224
]);
225
226
+addObject(BUILTIN_SHAPES, BuiltInUseStateId, [
227
+ ["0", { kind: "Poly" }],
228
+ [
229
+ "1",
230
+ addFunction(
231
+ BUILTIN_SHAPES,
232
+ [],
233
+ {
234
+ positionalParams: [],
235
+ restParam: Effect.Freeze,
236
+ returnType: PRIMITIVE_TYPE,
237
+ calleeEffect: Effect.Read,
238
+ returnValueKind: ValueKind.Immutable,
239
+ },
240
+ BuiltInSetStateId
241
+ ),
242
+ ],
243
+]);
244
+
245
+addObject(BUILTIN_SHAPES, BuiltInUseRefId, [
246
+ ["current", { kind: "Object", shapeId: BuiltInRefValueId }],
247
+]);
248
+
249
+addObject(BUILTIN_SHAPES, BuiltInRefValueId, []);
250
+
251
export const DefaultMutatingHook = addHook(BUILTIN_SHAPES, [], {
252
positionalParams: [],
253
restParam: Effect.Mutate,