Add type information for jsx/runtime

This commit is contained in:
Sathya Gunasekaran
2024-02-02 10:56:44 -05:00
parent 7bce302421
commit 29cb62ad6c
3 changed files with 120 additions and 36 deletions
@@ -346,42 +346,54 @@ const BUILTIN_HOOKS: Array<[string, BuiltInType]> = [
],
];
TYPED_GLOBALS.push([
"React",
addObject(DEFAULT_SHAPES, null, [
...BUILTIN_HOOKS,
[
"createElement",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
],
[
"cloneElement",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
],
[
"createRef",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Capture, // createRef takes no paramters
returnType: { kind: "Object", shapeId: BuiltInUseRefId },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Mutable,
}),
],
]),
]);
TYPED_GLOBALS.push(
[
"React",
addObject(DEFAULT_SHAPES, null, [
...BUILTIN_HOOKS,
[
"createElement",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
],
[
"cloneElement",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
],
[
"createRef",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Capture, // createRef takes no paramters
returnType: { kind: "Object", shapeId: BuiltInUseRefId },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Mutable,
}),
],
]),
],
[
"_jsx",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
]
);
export type Global = BuiltInType | PolyType;
export type GlobalRegistry = Map<string, Global>;
@@ -0,0 +1,55 @@
## Input
```javascript
import { jsx as _jsx } from "react/jsx-runtime";
import { shallowCopy } from "shared-runtime";
function Component(props) {
const childprops = { style: { width: props.width } };
const element = _jsx("div", {
childprops: childprops,
children: '"hello world"',
});
shallowCopy(childprops); // function that in theory could mutate, we assume not bc createElement freezes
return element;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { jsx as _jsx } from "react/jsx-runtime";
import { shallowCopy } from "shared-runtime";
function Component(props) {
const $ = useMemoCache(2);
let element;
if ($[0] !== props.width) {
const childprops = { style: { width: props.width } };
element = _jsx("div", { childprops, children: '"hello world"' });
shallowCopy(childprops);
$[0] = props.width;
$[1] = element;
} else {
element = $[1];
}
return element;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
### Eval output
(kind: ok) <div childprops="[object Object]">"hello world"</div>
@@ -0,0 +1,17 @@
import { jsx as _jsx } from "react/jsx-runtime";
import { shallowCopy } from "shared-runtime";
function Component(props) {
const childprops = { style: { width: props.width } };
const element = _jsx("div", {
childprops: childprops,
children: '"hello world"',
});
shallowCopy(childprops); // function that in theory could mutate, we assume not bc createElement freezes
return element;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};