Skip to content
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

fix: disallow bind:group to snippet parameters #15401

Merged
merged 1 commit into from
Feb 27, 2025
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: 5 additions & 0 deletions .changeset/hip-oranges-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: disallow `bind:group` to snippet parameters
6 changes: 6 additions & 0 deletions documentation/docs/98-reference/.generated/compile-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ Attribute values containing `{...}` must be enclosed in quote marks, unless the
`bind:group` can only bind to an Identifier or MemberExpression
```

### bind_group_invalid_snippet_parameter

```
Cannot `bind:group` to a snippet parameter
```

### bind_invalid_expression

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@

> `bind:group` can only bind to an Identifier or MemberExpression

## bind_group_invalid_snippet_parameter

> Cannot `bind:group` to a snippet parameter

## bind_invalid_expression

> Can only bind to an Identifier or MemberExpression or a `{get, set}` pair
Expand Down
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,15 @@ export function bind_group_invalid_expression(node) {
e(node, 'bind_group_invalid_expression', `\`bind:group\` can only bind to an Identifier or MemberExpression\nhttps://svelte.dev/e/bind_group_invalid_expression`);
}

/**
* Cannot `bind:group` to a snippet parameter
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function bind_group_invalid_snippet_parameter(node) {
e(node, 'bind_group_invalid_snippet_parameter', `Cannot \`bind:group\` to a snippet parameter\nhttps://svelte.dev/e/bind_group_invalid_snippet_parameter`);
}

/**
* Can only bind to an Identifier or MemberExpression or a `{get, set}` pair
* @param {null | number | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export function BindDirective(node, context) {
throw new Error('Cannot find declaration for bind:group');
}

if (binding.kind === 'snippet') {
e.bind_group_invalid_snippet_parameter(node);
}

// Traverse the path upwards and find all EachBlocks who are (indirectly) contributing to bind:group,
// i.e. one of their declarations is referenced in the binding. This allows group bindings to work
// correctly when referencing a variable declared in an EachBlock by using the index of the each block
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "bind_group_invalid_snippet_parameter",
"end": {
"column": 44,
"line": 2
},
"message": "Cannot `bind:group` to a snippet parameter",
"start": {
"column": 21,
"line": 2
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#snippet test(group)}
<input type="radio" bind:group={group.name} />
{/snippet}