Skip to content

Commit 1912459

Browse files
authored
chore: initialize snippet binding correctly (#14430)
1 parent d2ce0a7 commit 1912459

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ export function analyze_component(root, source, options) {
287287

288288
const store_name = name.slice(1);
289289
const declaration = instance.scope.get(store_name);
290+
const init = /** @type {Node | undefined} */ (declaration?.initial);
290291

291292
// If we're not in legacy mode through the compiler option, assume the user
292293
// is referencing a rune and not a global store.
@@ -295,9 +296,9 @@ export function analyze_component(root, source, options) {
295296
!is_rune(name) ||
296297
(declaration !== null &&
297298
// const state = $state(0) is valid
298-
(get_rune(declaration.initial, instance.scope) === null ||
299+
(get_rune(init, instance.scope) === null ||
299300
// rune-line names received as props are valid too (but we have to protect against $props as store)
300-
(store_name !== 'props' && get_rune(declaration.initial, instance.scope) === '$props')) &&
301+
(store_name !== 'props' && get_rune(init, instance.scope) === '$props')) &&
301302
// allow `import { derived } from 'svelte/store'` in the same file as `const x = $derived(..)` because one is not a subscription to the other
302303
!(
303304
name === '$derived' &&

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ export function should_proxy(node, scope) {
259259
binding.initial.type !== 'FunctionDeclaration' &&
260260
binding.initial.type !== 'ClassDeclaration' &&
261261
binding.initial.type !== 'ImportDeclaration' &&
262-
binding.initial.type !== 'EachBlock'
262+
binding.initial.type !== 'EachBlock' &&
263+
binding.initial.type !== 'SnippetBlock'
263264
) {
264265
return should_proxy(binding.initial, null);
265266
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Scope {
7575
* @param {Identifier} node
7676
* @param {Binding['kind']} kind
7777
* @param {DeclarationKind} declaration_kind
78-
* @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock} initial
78+
* @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock | AST.SnippetBlock} initial
7979
* @returns {Binding}
8080
*/
8181
declare(node, kind, declaration_kind, initial = null) {
@@ -632,7 +632,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
632632
if (is_top_level) {
633633
scope = /** @type {Scope} */ (parent);
634634
}
635-
scope.declare(node.expression, 'normal', 'function', node.expression);
635+
scope.declare(node.expression, 'normal', 'function', node);
636636

637637
const child_scope = state.scope.child();
638638
scopes.set(node, child_scope);
@@ -726,7 +726,7 @@ export function set_scope(node, { next, state }) {
726726

727727
/**
728728
* Returns the name of the rune if the given expression is a `CallExpression` using a rune.
729-
* @param {Node | AST.EachBlock | null | undefined} node
729+
* @param {Node | null | undefined} node
730730
* @param {Scope} scope
731731
*/
732732
export function get_rune(node, scope) {

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ export interface Binding {
291291
| FunctionDeclaration
292292
| ClassDeclaration
293293
| ImportDeclaration
294-
| AST.EachBlock;
294+
| AST.EachBlock
295+
| AST.SnippetBlock;
295296
is_called: boolean;
296297
references: { node: Identifier; path: SvelteNode[] }[];
297298
mutated: boolean;

0 commit comments

Comments
 (0)