@samitouri / QOS-React-1 / commits / 29cb62ad6c

Add type information for jsx/runtime

Sathya Gunasekaran committed Feb 2, 2024 at 10:56 UTC 29cb62ad6ca2e3d491df038e1cc8cbe882fa31a4
3 files changed +120 -36
compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts
+48 -36
@@ -346,42 +346,54 @@ const BUILTIN_HOOKS: Array<[string, BuiltInType]> = [
346 ],
347 ];
348
349 -TYPED_GLOBALS.push([
350 - "React",
351 - addObject(DEFAULT_SHAPES, null, [
352 - ...BUILTIN_HOOKS,
353 - [
354 - "createElement",
355 - addFunction(DEFAULT_SHAPES, [], {
356 - positionalParams: [],
357 - restParam: Effect.Freeze,
358 - returnType: { kind: "Poly" },
359 - calleeEffect: Effect.Read,
360 - returnValueKind: ValueKind.Frozen,
361 - }),
362 - ],
363 - [
364 - "cloneElement",
365 - addFunction(DEFAULT_SHAPES, [], {
366 - positionalParams: [],
367 - restParam: Effect.Freeze,
368 - returnType: { kind: "Poly" },
369 - calleeEffect: Effect.Read,
370 - returnValueKind: ValueKind.Frozen,
371 - }),
372 - ],
373 - [
374 - "createRef",
375 - addFunction(DEFAULT_SHAPES, [], {
376 - positionalParams: [],
377 - restParam: Effect.Capture, // createRef takes no paramters
378 - returnType: { kind: "Object", shapeId: BuiltInUseRefId },
379 - calleeEffect: Effect.Read,
380 - returnValueKind: ValueKind.Mutable,
381 - }),
382 - ],
383 - ]),
384 -]);
349 +TYPED_GLOBALS.push(
350 + [
351 + "React",
352 + addObject(DEFAULT_SHAPES, null, [
353 + ...BUILTIN_HOOKS,
354 + [
355 + "createElement",
356 + addFunction(DEFAULT_SHAPES, [], {
357 + positionalParams: [],
358 + restParam: Effect.Freeze,
359 + returnType: { kind: "Poly" },
360 + calleeEffect: Effect.Read,
361 + returnValueKind: ValueKind.Frozen,
362 + }),
363 + ],
364 + [
365 + "cloneElement",
366 + addFunction(DEFAULT_SHAPES, [], {
367 + positionalParams: [],
368 + restParam: Effect.Freeze,
369 + returnType: { kind: "Poly" },
370 + calleeEffect: Effect.Read,
371 + returnValueKind: ValueKind.Frozen,
372 + }),
373 + ],
374 + [
375 + "createRef",
376 + addFunction(DEFAULT_SHAPES, [], {
377 + positionalParams: [],
378 + restParam: Effect.Capture, // createRef takes no paramters
379 + returnType: { kind: "Object", shapeId: BuiltInUseRefId },
380 + calleeEffect: Effect.Read,
381 + returnValueKind: ValueKind.Mutable,
382 + }),
383 + ],
384 + ]),
385 + ],
386 + [
387 + "_jsx",
388 + addFunction(DEFAULT_SHAPES, [], {
389 + positionalParams: [],
390 + restParam: Effect.Freeze,
391 + returnType: { kind: "Poly" },
392 + calleeEffect: Effect.Read,
393 + returnValueKind: ValueKind.Frozen,
394 + }),
395 + ]
396 +);
397
398 export type Global = BuiltInType | PolyType;
399 export type GlobalRegistry = Map<string, Global>;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-freeze.expect.md new
+55
@@ -0,0 +1,55 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { jsx as _jsx } from "react/jsx-runtime";
6 +import { shallowCopy } from "shared-runtime";
7 +
8 +function Component(props) {
9 + const childprops = { style: { width: props.width } };
10 + const element = _jsx("div", {
11 + childprops: childprops,
12 + children: '"hello world"',
13 + });
14 + shallowCopy(childprops); // function that in theory could mutate, we assume not bc createElement freezes
15 + return element;
16 +}
17 +
18 +export const FIXTURE_ENTRYPOINT = {
19 + fn: Component,
20 + params: [{}],
21 +};
22 +
23 +```
24 +
25 +## Code
26 +
27 +```javascript
28 +import { unstable_useMemoCache as useMemoCache } from "react";
29 +import { jsx as _jsx } from "react/jsx-runtime";
30 +import { shallowCopy } from "shared-runtime";
31 +
32 +function Component(props) {
33 + const $ = useMemoCache(2);
34 + let element;
35 + if ($[0] !== props.width) {
36 + const childprops = { style: { width: props.width } };
37 + element = _jsx("div", { childprops, children: '"hello world"' });
38 + shallowCopy(childprops);
39 + $[0] = props.width;
40 + $[1] = element;
41 + } else {
42 + element = $[1];
43 + }
44 + return element;
45 +}
46 +
47 +export const FIXTURE_ENTRYPOINT = {
48 + fn: Component,
49 + params: [{}],
50 +};
51 +
52 +```
53 +
54 +### Eval output
55 +(kind: ok) <div childprops="[object Object]">"hello world"</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-freeze.js new
+17
@@ -0,0 +1,17 @@
1 +import { jsx as _jsx } from "react/jsx-runtime";
2 +import { shallowCopy } from "shared-runtime";
3 +
4 +function Component(props) {
5 + const childprops = { style: { width: props.width } };
6 + const element = _jsx("div", {
7 + childprops: childprops,
8 + children: '"hello world"',
9 + });
10 + shallowCopy(childprops); // function that in theory could mutate, we assume not bc createElement freezes
11 + return element;
12 +}
13 +
14 +export const FIXTURE_ENTRYPOINT = {
15 + fn: Component,
16 + params: [{}],
17 +};