Skip to content

Commit 67c3f69

Browse files
committed
fix: add top level snippets to instance scope
fixes #9460
1 parent 640dd48 commit 67c3f69

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

.changeset/rich-sheep-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: add top level snippets to instance scope

packages/svelte/src/compiler/phases/scope.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,6 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
273273
next({ scope });
274274
};
275275

276-
/**
277-
* @type {import('zimmerframe').Visitor<import('#compiler').SvelteNode, State, import('#compiler').SvelteNode>}
278-
*/
279-
const CreateBlock = (node, { state, next }) => {
280-
const scope = state.scope.child();
281-
scopes.set(node, scope);
282-
next({ scope });
283-
};
284-
285276
/**
286277
* @param {import('#compiler').ElementLike} node
287278
* @param {Scope} parent
@@ -566,18 +557,24 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
566557
},
567558

568559
SnippetBlock(node, context) {
569-
state.scope.declare(node.expression, 'normal', 'function', node.expression);
560+
// Special-case for root-level snippets: they become part of the instance scope
561+
const is_top_level = !context.path.at(-2);
562+
let scope = state.scope;
563+
if (is_top_level) {
564+
scope = /** @type {Scope} */ (parent);
565+
}
566+
scope.declare(node.expression, 'normal', 'function', node.expression);
570567

571-
const scope = state.scope.child();
572-
scopes.set(node, scope);
568+
const child_scope = state.scope.child();
569+
scopes.set(node, child_scope);
573570

574571
if (node.context) {
575572
for (const id of extract_identifiers(node.context)) {
576-
scope.declare(id, 'each', 'let');
573+
child_scope.declare(id, 'each', 'let');
577574
}
578575
}
579576

580-
context.next({ scope });
577+
context.next({ scope: child_scope });
581578
},
582579

583580
Fragment: (node, context) => {
@@ -586,9 +583,6 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
586583
context.next({ scope });
587584
},
588585

589-
// TODO this will be unnecessary when we switch to fragments
590-
IfBlock: CreateBlock,
591-
592586
BindDirective(node, context) {
593587
updates.push([
594588
context.state.scope,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// input.svelte (Svelte VERSION)
2+
// Note: compiler output will change before 5.0 is released!
3+
import "svelte/internal/disclose-version";
4+
import * as $ from "svelte/internal";
5+
6+
function log(_, snippet) {
7+
console.log(snippet);
8+
}
9+
10+
var frag_1 = $.template(`Hello`, true);
11+
var frag = $.template(`<button>log snippet</button>`);
12+
13+
export default function Input($$anchor, $$props) {
14+
$.push($$props, false);
15+
16+
/* Init */
17+
var button = $.open($$anchor, true, frag);
18+
19+
function snippet($$anchor) {
20+
/* Init */
21+
var fragment = $.open_frag($$anchor, true, frag_1);
22+
23+
$.close_frag($$anchor, fragment);
24+
}
25+
26+
button.__click = [log, snippet];
27+
$.close($$anchor, button);
28+
$.pop();
29+
}
30+
31+
$.delegate(["click"]);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// input.svelte (Svelte VERSION)
2+
// Note: compiler output will change before 5.0 is released!
3+
import * as $ from "svelte/internal/server";
4+
5+
export default function Input($$payload, $$props) {
6+
$.push(false);
7+
8+
function log() {
9+
console.log(snippet);
10+
}
11+
12+
function snippet($$payload) {
13+
const anchor = $.create_anchor($$payload);
14+
15+
$$payload.out += anchor;
16+
$$payload.out += `Hello`;
17+
$$payload.out += anchor;
18+
}
19+
20+
$$payload.out += `<button>log snippet</button>`;
21+
$.pop();
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
function log() {
3+
console.log(snippet)
4+
}
5+
</script>
6+
7+
{#snippet snippet()}Hello{/snippet}
8+
9+
<button on:click={log}>
10+
log snippet
11+
</button>

0 commit comments

Comments
 (0)