diff --git a/.changeset/serious-zebras-scream.md b/.changeset/serious-zebras-scream.md new file mode 100644 index 0000000000..9efd62e5b5 --- /dev/null +++ b/.changeset/serious-zebras-scream.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure snippets have correct scope diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index 95c192d4d4..6c73002d5b 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -557,6 +557,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) { }, SnippetBlock(node, context) { + const state = context.state; // Special-case for root-level snippets: they become part of the instance scope const is_top_level = !context.path.at(-2); let scope = state.scope; diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-scope/Component.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-scope/Component.svelte new file mode 100644 index 0000000000..79a2ed25e9 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-scope/Component.svelte @@ -0,0 +1,7 @@ + + +{#if inner} + {@render inner()} +{/if} diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-scope/_config.js b/packages/svelte/tests/runtime-runes/samples/snippet-scope/_config.js new file mode 100644 index 0000000000..6f04c6270d --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-scope/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + html: `
S
Sv
ve
el
lt
te
e` +}); diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-scope/main.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-scope/main.svelte new file mode 100644 index 0000000000..737d2943e3 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-scope/main.svelte @@ -0,0 +1,13 @@ + + +{#each name.split('') as character} +{character}
+