Skip to content

Commit

Permalink
fix: disallow bind:group to snippet parameters (#15401)
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti authored Feb 27, 2025
1 parent e4987d2 commit 474c588
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
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}

0 comments on commit 474c588

Please sign in to comment.