fix: keep `$state.eager` when used as a variable initializer (#18809)

fixes #18808

If you write `let x = $state.eager(...)`, the client compiler used to
delete that line and then still read `x`. The page crashed with `x is
not defined`. Server rendering kept the binding.

Using `$state.eager(...)` directly in the markup already worked. This
puts the `let` / `const` form on the same path as `$state.snapshot`.

Added a runtime-runes sample that fails without this change.

Co-authored-by: Xia Chao <236466140+bun-unsafe@users.noreply.github.com>
pull/17600/merge
Xia Chao 2 days ago committed by GitHub
parent ce89035ecb
commit 34b13ac3e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: keep `$state.eager` when used as a variable initializer

@ -35,6 +35,7 @@ export function VariableDeclaration(node, context) {
rune === '$inspect' ||
rune === '$inspect.trace' ||
rune === '$state.snapshot' ||
rune === '$state.eager' ||
rune === '$host'
) {
declarations.push(/** @type {VariableDeclarator} */ (context.visit(declarator)));

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `<p>20</p>`
});

@ -0,0 +1,7 @@
<script>
let n = $state(2);
let d = $derived(n * 10);
let x = $state.eager(d);
</script>
<p>{x}</p>
Loading…
Cancel
Save