diff --git a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts index 7aaf4a980a..0f3e292b7c 100644 --- a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -537,6 +537,15 @@ class PropagationVisitor extends ReactiveFunctionVisitor { id, scope: context.currentScope, }); + } else if (value.kind === "DeclareLocal") { + // Some variables may be declared and never initialized. We need + // to retain (and hoist) these declarations if they are included + // in a reactive scope. One approach is to simply add all `DeclareLocal`s + // as scope declarations. + context.declare(value.lvalue.place.identifier, { + id, + scope: context.currentScope, + }); } else if (value.kind === "Destructure") { context.visitOperand(value.value); for (const place of eachPatternOperand(value.lvalue.pattern)) { diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.uninitialized-declaration-in-reactive-scope.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md similarity index 69% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.uninitialized-declaration-in-reactive-scope.expect.md rename to compiler/forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md index 014e51ac59..de9b7dee92 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.uninitialized-declaration-in-reactive-scope.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md @@ -16,22 +16,25 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(2); + const $ = useMemoCache(3); + let y; let x; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { x = mutate(); - let y; + foo(x); - $[0] = x; + $[0] = y; + $[1] = x; } else { - x = $[0]; + y = $[0]; + x = $[1]; } let t0; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { t0 = [y, x]; - $[1] = t0; + $[2] = t0; } else { - t0 = $[1]; + t0 = $[2]; } return t0; } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.uninitialized-declaration-in-reactive-scope.js b/compiler/forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.js similarity index 100% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.uninitialized-declaration-in-reactive-scope.js rename to compiler/forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.js