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: prevent state runes from being called with spread #15585

Merged
merged 2 commits into from
Mar 22, 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/nice-pianos-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: prevent state runes from being called with spread
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 @@ -660,6 +660,12 @@ Cannot access a computed property of a rune
`%name%` is not a valid rune
```

### rune_invalid_spread

```
`%rune%` cannot be called with a spread argument
```

### rune_invalid_usage

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ This turned out to be buggy and unpredictable, particularly when working with de

> `%name%` is not a valid rune

## rune_invalid_spread

> `%rune%` cannot be called with a spread argument

## rune_invalid_usage

> Cannot use `%rune%` rune in non-runes mode
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ export function rune_invalid_name(node, name) {
e(node, 'rune_invalid_name', `\`${name}\` is not a valid rune\nhttps://svelte.dev/e/rune_invalid_name`);
}

/**
* `%rune%` cannot be called with a spread argument
* @param {null | number | NodeLike} node
* @param {string} rune
* @returns {never}
*/
export function rune_invalid_spread(node, rune) {
e(node, 'rune_invalid_spread', `\`${rune}\` cannot be called with a spread argument\nhttps://svelte.dev/e/rune_invalid_spread`);
}

/**
* Cannot use `%rune%` rune in non-runes mode
* @param {null | number | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export function CallExpression(node, context) {

const rune = get_rune(node, context.state.scope);

if (rune && rune !== '$inspect') {
for (const arg of node.arguments) {
if (arg.type === 'SpreadElement') {
e.rune_invalid_spread(node, rune);
}
}
}

switch (rune) {
case null:
if (!is_safe_identifier(node.callee, context.state.scope)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "rune_invalid_spread",
"end": {
"column": 35,
"line": 3
},
"message": "`$derived.by` cannot be called with a spread argument",
"start": {
"column": 15,
"line": 3
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
const args = [0];
const count = $derived.by(...args);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "rune_invalid_spread",
"end": {
"column": 32,
"line": 3
},
"message": "`$derived` cannot be called with a spread argument",
"start": {
"column": 15,
"line": 3
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
const args = [0];
const count = $derived(...args);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "rune_invalid_spread",
"end": {
"column": 34,
"line": 3
},
"message": "`$state.raw` cannot be called with a spread argument",
"start": {
"column": 15,
"line": 3
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
const args = [0];
const count = $state.raw(...args);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "rune_invalid_spread",
"end": {
"column": 30,
"line": 3
},
"message": "`$state` cannot be called with a spread argument",
"start": {
"column": 15,
"line": 3
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
const args = [0];
const count = $state(...args);
</script>