diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mege-consecutive-scopes-dont-merge-with-different-deps.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mege-consecutive-scopes-dont-merge-with-different-deps.expect.md new file mode 100644 index 0000000000000..ffd596e89dbc4 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mege-consecutive-scopes-dont-merge-with-different-deps.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +const { getNumber, identity } = require("shared-runtime"); + +function Component(props) { + // Two scopes: one for `getNumber()`, one for the object literal. + // Neither has dependencies so they should merge + return { a: getNumber(), b: identity(props.id), c: ["static"] }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ id: 42 }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +const { getNumber, identity } = require("shared-runtime"); + +function Component(props) { + const $ = useMemoCache(6); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = getNumber(); + $[0] = t0; + } else { + t0 = $[0]; + } + let t1; + if ($[1] !== props.id) { + t1 = identity(props.id); + $[1] = props.id; + $[2] = t1; + } else { + t1 = $[2]; + } + let t2; + if ($[3] === Symbol.for("react.memo_cache_sentinel")) { + t2 = ["static"]; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] !== t1) { + t3 = { a: t0, b: t1, c: t2 }; + $[4] = t1; + $[5] = t3; + } else { + t3 = $[5]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ id: 42 }], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mege-consecutive-scopes-dont-merge-with-different-deps.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mege-consecutive-scopes-dont-merge-with-different-deps.js new file mode 100644 index 0000000000000..4cdb80419c15a --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mege-consecutive-scopes-dont-merge-with-different-deps.js @@ -0,0 +1,12 @@ +const { getNumber, identity } = require("shared-runtime"); + +function Component(props) { + // Two scopes: one for `getNumber()`, one for the object literal. + // Neither has dependencies so they should merge + return { a: getNumber(), b: identity(props.id), c: ["static"] }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ id: 42 }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-nested-scopes.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-nested-scopes.expect.md new file mode 100644 index 0000000000000..5a831e19d2123 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-nested-scopes.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +const { getNumber } = require("shared-runtime"); + +function Component(props) { + let x; + // Two scopes: one for `getNumber()`, one for the object literal. + // Neither has dependencies so they should merge + if (props.cond) { + x = { session_id: getNumber() }; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +const { getNumber } = require("shared-runtime"); + +function Component(props) { + const $ = useMemoCache(1); + let x; + if (props.cond) { + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { session_id: getNumber() }; + $[0] = t0; + } else { + t0 = $[0]; + } + x = t0; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true }], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-nested-scopes.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-nested-scopes.js new file mode 100644 index 0000000000000..0589d71c5a09c --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-nested-scopes.js @@ -0,0 +1,16 @@ +const { getNumber } = require("shared-runtime"); + +function Component(props) { + let x; + // Two scopes: one for `getNumber()`, one for the object literal. + // Neither has dependencies so they should merge + if (props.cond) { + x = { session_id: getNumber() }; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true }], +};