Skip to content

chore: initialize snippet binding correctly #14430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export function analyze_component(root, source, options) {

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

// If we're not in legacy mode through the compiler option, assume the user
// is referencing a rune and not a global store.
Expand All @@ -295,9 +296,9 @@ export function analyze_component(root, source, options) {
!is_rune(name) ||
(declaration !== null &&
// const state = $state(0) is valid
(get_rune(declaration.initial, instance.scope) === null ||
(get_rune(init, instance.scope) === null ||
// rune-line names received as props are valid too (but we have to protect against $props as store)
(store_name !== 'props' && get_rune(declaration.initial, instance.scope) === '$props')) &&
(store_name !== 'props' && get_rune(init, instance.scope) === '$props')) &&
// allow `import { derived } from 'svelte/store'` in the same file as `const x = $derived(..)` because one is not a subscription to the other
!(
name === '$derived' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ export function should_proxy(node, scope) {
binding.initial.type !== 'FunctionDeclaration' &&
binding.initial.type !== 'ClassDeclaration' &&
binding.initial.type !== 'ImportDeclaration' &&
binding.initial.type !== 'EachBlock'
binding.initial.type !== 'EachBlock' &&
binding.initial.type !== 'SnippetBlock'
) {
return should_proxy(binding.initial, null);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Scope {
* @param {Identifier} node
* @param {Binding['kind']} kind
* @param {DeclarationKind} declaration_kind
* @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock} initial
* @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock | AST.SnippetBlock} initial
* @returns {Binding}
*/
declare(node, kind, declaration_kind, initial = null) {
Expand Down Expand Up @@ -632,7 +632,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
if (is_top_level) {
scope = /** @type {Scope} */ (parent);
}
scope.declare(node.expression, 'normal', 'function', node.expression);
scope.declare(node.expression, 'normal', 'function', node);

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

/**
* Returns the name of the rune if the given expression is a `CallExpression` using a rune.
* @param {Node | AST.EachBlock | null | undefined} node
* @param {Node | null | undefined} node
* @param {Scope} scope
*/
export function get_rune(node, scope) {
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ export interface Binding {
| FunctionDeclaration
| ClassDeclaration
| ImportDeclaration
| AST.EachBlock;
| AST.EachBlock
| AST.SnippetBlock;
is_called: boolean;
references: { node: Identifier; path: SvelteNode[] }[];
mutated: boolean;
Expand Down