Skip to content

Commit 0e0f01e

Browse files
authored
fix: disallow $state/$derived in const tags (#15115)
1 parent 94d80c6 commit 0e0f01e

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

.changeset/strong-shoes-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: disallow $state/$derived in const tags

packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export function CallExpression(node, context) {
7979
case '$derived':
8080
case '$derived.by':
8181
if (
82-
parent.type !== 'VariableDeclarator' &&
82+
(parent.type !== 'VariableDeclarator' ||
83+
get_parent(context.path, -3).type === 'ConstTag') &&
8384
!(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed)
8485
) {
8586
e.state_invalid_placement(node, rune);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "state_invalid_placement",
4+
"message": "`$derived(...)` can only be used as a variable declaration initializer or a class field",
5+
"start": {
6+
"line": 2,
7+
"column": 15
8+
},
9+
"end": {
10+
"line": 2,
11+
"column": 26
12+
}
13+
}
14+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{#snippet test()}
2+
{@const der = $derived(0)}
3+
{/snippet}

0 commit comments

Comments
 (0)