From 337c17a66fc326ded8f1614783712fbdf05b8d25 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Mon, 6 Nov 2023 16:27:11 -0800 Subject: [PATCH] More React API coverage Redo of #2248 in its own stack --- .../src/HIR/Globals.ts | 48 +++++++++++++- .../compiler/createElement-freeze.expect.md | 66 +++++++++++++++++++ .../fixtures/compiler/createElement-freeze.js | 14 ++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.js diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts index bcf79f6857..7eebd98b69 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts @@ -312,9 +312,55 @@ const BUILTIN_HOOKS: Array<[string, BuiltInType]> = [ returnValueKind: ValueKind.Frozen, }), ], + [ + "useInsertionEffect", + addHook(DEFAULT_SHAPES, [], { + positionalParams: [], + restParam: Effect.Freeze, + returnType: { kind: "Poly" }, + calleeEffect: Effect.Read, + hookKind: "useLayoutEffect", + returnValueKind: ValueKind.Frozen, + }), + ], ]; -TYPED_GLOBALS.push(["React", addObject(DEFAULT_SHAPES, null, BUILTIN_HOOKS)]); +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, + }), + ], + ]), +]); export type Global = BuiltInType | PolyType; export type GlobalRegistry = Map; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.expect.md new file mode 100644 index 0000000000..62c09d96b0 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +import React from "react"; +import { shallowCopy } from "shared-runtime"; + +function Component(props) { + const childProps = { style: { width: props.width } }; + const element = React.createElement("div", childProps, ["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 React from "react"; +import { shallowCopy } from "shared-runtime"; + +function Component(props) { + const $ = useMemoCache(5); + let t0; + if ($[0] !== props.width) { + t0 = { style: { width: props.width } }; + $[0] = props.width; + $[1] = t0; + } else { + t0 = $[1]; + } + const childProps = t0; + let t2; + if ($[2] !== childProps) { + let t1; + if ($[4] === Symbol.for("react.memo_cache_sentinel")) { + t1 = ["hello world"]; + $[4] = t1; + } else { + t1 = $[4]; + } + t2 = React.createElement("div", childProps, t1); + $[2] = childProps; + $[3] = t2; + } else { + t2 = $[3]; + } + const element = t2; + shallowCopy(childProps); + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.js new file mode 100644 index 0000000000..40d9fafeb0 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/createElement-freeze.js @@ -0,0 +1,14 @@ +import React from "react"; +import { shallowCopy } from "shared-runtime"; + +function Component(props) { + const childProps = { style: { width: props.width } }; + const element = React.createElement("div", childProps, ["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: [{}], +};