diff --git a/.changeset/hot-grapes-destroy.md b/.changeset/hot-grapes-destroy.md new file mode 100644 index 000000000000..f80e2aa01b52 --- /dev/null +++ b/.changeset/hot-grapes-destroy.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: delete transformers shadowed by unreassigned state diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index 04815dbc285a..d0e6efc6d8f8 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -4,7 +4,7 @@ /** @import { Visitors, ComponentClientTransformState, ClientTransformState } from './types' */ import { walk } from 'zimmerframe'; import * as b from '../../../utils/builders.js'; -import { build_getter } from './utils.js'; +import { build_getter, is_state_source } from './utils.js'; import { render_stylesheet } from '../css/index.js'; import { dev, filename } from '../../../state.js'; import { AnimateDirective } from './visitors/AnimateDirective.js'; @@ -66,7 +66,12 @@ const visitors = { const transform = { ...state.transform }; for (const [name, binding] of scope.declarations) { - if (binding.kind === 'normal') { + if ( + binding.kind === 'normal' || + // Reads of `$state(...)` declarations are not + // transformed if they are never reassigned + (binding.kind === 'state' && !is_state_source(binding, state.analysis)) + ) { delete transform[name]; } } diff --git a/packages/svelte/tests/runtime-runes/samples/derived-shadowed/_config.js b/packages/svelte/tests/runtime-runes/samples/derived-shadowed/_config.js new file mode 100644 index 000000000000..a9ff1fbabea6 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/derived-shadowed/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + html: `
0
` +}); diff --git a/packages/svelte/tests/runtime-runes/samples/derived-shadowed/main.svelte b/packages/svelte/tests/runtime-runes/samples/derived-shadowed/main.svelte new file mode 100644 index 000000000000..5ae3d3b494f2 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/derived-shadowed/main.svelte @@ -0,0 +1,8 @@ + + +
{value}