More React API coverage
Redo of #2248 in its own stack
Joe Savona committed
Nov 6, 2023 at 16:27 UTC
337c17a66fc326ded8f1614783712fbdf05b8d25
3 files changed
+127
-1
compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts
+47
-1
@@ -312,9 +312,55 @@ const BUILTIN_HOOKS: Array<[string, BuiltInType]> = [
312
returnValueKind: ValueKind.Frozen,
313
}),
314
],
315
+ [
316
+ "useInsertionEffect",
317
+ addHook(DEFAULT_SHAPES, [], {
318
+ positionalParams: [],
319
+ restParam: Effect.Freeze,
320
+ returnType: { kind: "Poly" },
321
+ calleeEffect: Effect.Read,
322
+ hookKind: "useLayoutEffect",
323
+ returnValueKind: ValueKind.Frozen,
324
+ }),
325
+ ],
326
];
327
317
-TYPED_GLOBALS.push(["React", addObject(DEFAULT_SHAPES, null, BUILTIN_HOOKS)]);
328
+TYPED_GLOBALS.push([
329
+ "React",
330
+ addObject(DEFAULT_SHAPES, null, [
331
+ ...BUILTIN_HOOKS,
332
+ [
333
+ "createElement",
334
+ addFunction(DEFAULT_SHAPES, [], {
335
+ positionalParams: [],
336
+ restParam: Effect.Freeze,
337
+ returnType: { kind: "Poly" },
338
+ calleeEffect: Effect.Read,
339
+ returnValueKind: ValueKind.Frozen,
340
+ }),
341
+ ],
342
+ [
343
+ "cloneElement",
344
+ addFunction(DEFAULT_SHAPES, [], {
345
+ positionalParams: [],
346
+ restParam: Effect.Freeze,
347
+ returnType: { kind: "Poly" },
348
+ calleeEffect: Effect.Read,
349
+ returnValueKind: ValueKind.Frozen,
350
+ }),
351
+ ],
352
+ [
353
+ "createRef",
354
+ addFunction(DEFAULT_SHAPES, [], {
355
+ positionalParams: [],
356
+ restParam: Effect.Capture, // createRef takes no paramters
357
+ returnType: { kind: "Object", shapeId: BuiltInUseRefId },
358
+ calleeEffect: Effect.Read,
359
+ returnValueKind: ValueKind.Mutable,
360
+ }),
361
+ ],
362
+ ]),
363
+]);
364
365
export type Global = BuiltInType | PolyType;
366
export type GlobalRegistry = Map<string, Global>;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.expect.md
new
+66
@@ -0,0 +1,66 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import React from "react";
6
+import { shallowCopy } from "shared-runtime";
7
+
8
+function Component(props) {
9
+ const childProps = { style: { width: props.width } };
10
+ const element = React.createElement("div", childProps, ["hello world"]);
11
+ shallowCopy(childProps); // function that in theory could mutate, we assume not bc createElement freezes
12
+ return element;
13
+}
14
+
15
+export const FIXTURE_ENTRYPOINT = {
16
+ fn: Component,
17
+ params: [{}],
18
+};
19
+
20
+```
21
+
22
+## Code
23
+
24
+```javascript
25
+import { unstable_useMemoCache as useMemoCache } from "react";
26
+import React from "react";
27
+import { shallowCopy } from "shared-runtime";
28
+
29
+function Component(props) {
30
+ const $ = useMemoCache(5);
31
+ let t0;
32
+ if ($[0] !== props.width) {
33
+ t0 = { style: { width: props.width } };
34
+ $[0] = props.width;
35
+ $[1] = t0;
36
+ } else {
37
+ t0 = $[1];
38
+ }
39
+ const childProps = t0;
40
+ let t2;
41
+ if ($[2] !== childProps) {
42
+ let t1;
43
+ if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
44
+ t1 = ["hello world"];
45
+ $[4] = t1;
46
+ } else {
47
+ t1 = $[4];
48
+ }
49
+ t2 = React.createElement("div", childProps, t1);
50
+ $[2] = childProps;
51
+ $[3] = t2;
52
+ } else {
53
+ t2 = $[3];
54
+ }
55
+ const element = t2;
56
+ shallowCopy(childProps);
57
+ return element;
58
+}
59
+
60
+export const FIXTURE_ENTRYPOINT = {
61
+ fn: Component,
62
+ params: [{}],
63
+};
64
+
65
+```
66
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.js
new
+14
@@ -0,0 +1,14 @@
1
+import React from "react";
2
+import { shallowCopy } from "shared-runtime";
3
+
4
+function Component(props) {
5
+ const childProps = { style: { width: props.width } };
6
+ const element = React.createElement("div", childProps, ["hello world"]);
7
+ shallowCopy(childProps); // function that in theory could mutate, we assume not bc createElement freezes
8
+ return element;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [{}],
14
+};