diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/AlignReactiveScopesToBlockScopesHIR.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/AlignReactiveScopesToBlockScopesHIR.ts index 873dc3ff91..cfcdaa31ed 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/AlignReactiveScopesToBlockScopesHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/AlignReactiveScopesToBlockScopesHIR.ts @@ -241,16 +241,6 @@ export function alignReactiveScopesToBlockScopesHIR(fn: HIRFunction): void { const joinedScopes: DisjointSet = mergeOverlappingScopes(rootNode); - joinedScopes.forEach((scope, groupScope) => { - if (scope !== groupScope) { - groupScope.range.start = makeInstructionId( - Math.min(groupScope.range.start, scope.range.start) - ); - groupScope.range.end = makeInstructionId( - Math.max(groupScope.range.end, scope.range.end) - ); - } - }); /** * Join scopes that begin and end at the same instructions */ @@ -269,6 +259,18 @@ export function alignReactiveScopesToBlockScopesHIR(fn: HIRFunction): void { } } } + + joinedScopes.forEach((scope, groupScope) => { + if (scope !== groupScope) { + groupScope.range.start = makeInstructionId( + Math.min(groupScope.range.start, scope.range.start) + ); + groupScope.range.end = makeInstructionId( + Math.max(groupScope.range.end, scope.range.end) + ); + } + }); + for (const [place, originalScope] of placeScopes) { const nextScope = joinedScopes.find(originalScope); if (nextScope !== null && nextScope !== originalScope) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/value-block-mutates-outer-value.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/value-block-mutates-outer-value.expect.md new file mode 100644 index 0000000000..a3d989ce79 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/value-block-mutates-outer-value.expect.md @@ -0,0 +1,76 @@ + +## Input + +```javascript +import { makeArray, useHook } from "shared-runtime"; + +/** + * Here, the cond ? [...] : defaultList value block produces two + * new values (each with its own scope): + * $0 = ["text"] + * $1 = { text: $0 } + * The same value block also mutates customList, so it must be + * merged with the scope producing customList + */ +function Foo({ defaultList, cond }) { + const comparator = (a, b) => a - b; + useHook(); + const customList = makeArray(1, 5, 2); + useHook(); + const result = cond + ? [...customList.sort(comparator), { text: ["text"] }] + : defaultList; + + return result; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ defaultList: [2, 4], cond: true }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +import { makeArray, useHook } from "shared-runtime"; + +/** + * Here, the cond ? [...] : defaultList value block produces two + * new values (each with its own scope): + * $0 = ["text"] + * $1 = { text: $0 } + * The same value block also mutates customList, so it must be + * merged with the scope producing customList + */ +function Foo(t0) { + const $ = useMemoCache(1); + const { defaultList, cond } = t0; + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = (a, b) => a - b; + $[0] = t1; + } else { + t1 = $[0]; + } + const comparator = t1; + useHook(); + const customList = makeArray(1, 5, 2); + useHook(); + const result = cond + ? [...customList.sort(comparator), { text: ["text"] }] + : defaultList; + return result; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ defaultList: [2, 4], cond: true }], +}; + +``` + +### Eval output +(kind: ok) [1,2,5,{"text":["text"]}] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/value-block-mutates-outer-value.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/value-block-mutates-outer-value.ts new file mode 100644 index 0000000000..c1341214b4 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/value-block-mutates-outer-value.ts @@ -0,0 +1,26 @@ +import { makeArray, useHook } from "shared-runtime"; + +/** + * Here, the cond ? [...] : defaultList value block produces two + * new values (each with its own scope): + * $0 = ["text"] + * $1 = { text: $0 } + * The same value block also mutates customList, so it must be + * merged with the scope producing customList + */ +function Foo({ defaultList, cond }) { + const comparator = (a, b) => a - b; + useHook(); + const customList = makeArray(1, 5, 2); + useHook(); + const result = cond + ? [...customList.sort(comparator), { text: ["text"] }] + : defaultList; + + return result; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ defaultList: [2, 4], cond: true }], +};