diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index d16ac9cc8c..9425030dac 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -2916,8 +2916,9 @@ function lowerJsxElementName( if (exprPath.isJSXIdentifier()) { const tag: string = exprPath.node.name; if (tag.match(/^[A-Z]/)) { + const kind = getLoadKind(builder, exprPath); return lowerValueToTemporary(builder, { - kind: "LoadLocal", + kind: kind, place: lowerIdentifier(builder, exprPath), loc: exprLoc, }); @@ -3245,7 +3246,7 @@ function getStoreKind( function getLoadKind( builder: HIRBuilder, - identifier: NodePath + identifier: NodePath ): "LoadLocal" | "LoadContext" { const isContext = builder.isContextIdentifier(identifier); return isContext ? "LoadContext" : "LoadLocal"; diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts index ce12a46a4a..1246a68c82 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts @@ -267,7 +267,7 @@ export default class HIRBuilder { return resolvedBinding; } - isContextIdentifier(path: NodePath): boolean { + isContextIdentifier(path: NodePath): boolean { const binding = this.#resolveBabelBinding(path); if (binding) { return this.#env.isContextIdentifier(binding.identifier); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md new file mode 100644 index 0000000000..82c48f2108 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import { useMemo } from "react"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + let Component = Stringify; + + Component = useMemo(() => { + return Component; + }); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Sathya" }], +}; + +``` + +## Code + +```javascript +import { useMemo, unstable_useMemoCache as useMemoCache } from "react"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = useMemoCache(3); + let Component; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + Component = Stringify; + + Component; + let t0; + t0 = Component; + Component = t0; + $[0] = Component; + } else { + Component = $[0]; + } + let t0; + if ($[1] !== props) { + t0 = ; + $[1] = props; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Sathya" }], +}; + +``` + +### Eval output +(kind: ok)
{"name":"Sathya"}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.js new file mode 100644 index 0000000000..7abf87ea0f --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.js @@ -0,0 +1,17 @@ +import { useMemo } from "react"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + let Component = Stringify; + + Component = useMemo(() => { + return Component; + }); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Sathya" }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-context-variable-as-jsx-element-tag.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-context-variable-as-jsx-element-tag.expect.md deleted file mode 100644 index 69a7bf2c1e..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-context-variable-as-jsx-element-tag.expect.md +++ /dev/null @@ -1,29 +0,0 @@ - -## Input - -```javascript -function Component(props) { - let Component = Foo; - - Component = useMemo(() => { - return Component; - }); - - return ; -} - -``` - - -## Error - -``` - 6 | }); - 7 | -> 8 | return ; - | ^^^^^^^^^ [ReactForget] Invariant: Expected all references to a variable to be consistently local or context references. Identifier Component$2 is referenced as a local variable, but was previously referenced as a context variable (8:8) - 9 | } - 10 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-context-variable-as-jsx-element-tag.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-context-variable-as-jsx-element-tag.js deleted file mode 100644 index 355d85f235..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-context-variable-as-jsx-element-tag.js +++ /dev/null @@ -1,9 +0,0 @@ -function Component(props) { - let Component = Foo; - - Component = useMemo(() => { - return Component; - }); - - return ; -}