fix: ensure snippet hoisting works in the correct scope (#14642)

* fix: ensure snippet hoisting works in the correct scope

* fix bug

* revert

* revert
pull/14656/head
Dominic Gannaway 2 weeks ago committed by GitHub
parent 9cfd2e20ab
commit 66e30d3288
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure snippet hoisting works in the correct scope

@ -99,8 +99,9 @@ function can_hoist_snippet(scope, scopes, visited = new Set()) {
if (binding.initial?.type === 'SnippetBlock') {
if (visited.has(binding)) continue;
visited.add(binding);
const snippet_scope = /** @type {Scope} */ (scopes.get(binding.initial));
if (can_hoist_snippet(binding.scope, scopes, visited)) {
if (can_hoist_snippet(snippet_scope, scopes, visited)) {
continue;
}
}

@ -628,12 +628,8 @@ 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;
if (is_top_level) {
scope = /** @type {Scope} */ (parent);
}
scope.declare(node.expression, 'normal', 'function', node);
const child_scope = state.scope.child();

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: 'a'
});

@ -0,0 +1,13 @@
<script>
let abc = 'a'
</script>
{@render b()}
{#snippet a()}
{abc}
{/snippet}
{#snippet b()}
{@render a()}
{/snippet}

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: '<h1>Hello world!</h1>'
});

@ -0,0 +1,13 @@
<script module>
export { foo }
</script>
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
{#snippet foo()}
oo
{/snippet}
Loading…
Cancel
Save