chore: initialize snippet binding correctly (#14430)

pull/14432/head
Rich Harris 2 months ago committed by GitHub
parent d2ce0a79ae
commit 1912459308
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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.
@ -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' &&

@ -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);
}

@ -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) {
@ -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);
@ -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) {

@ -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;

Loading…
Cancel
Save