Skip to content

Commit

Permalink
fix: disallow $state/$derived in const tags (#15115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Jan 27, 2025
1 parent 94d80c6 commit 0e0f01e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/strong-shoes-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: disallow $state/$derived in const tags
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export function CallExpression(node, context) {
case '$derived':
case '$derived.by':
if (
parent.type !== 'VariableDeclarator' &&
(parent.type !== 'VariableDeclarator' ||
get_parent(context.path, -3).type === 'ConstTag') &&
!(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed)
) {
e.state_invalid_placement(node, rune);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "state_invalid_placement",
"message": "`$derived(...)` can only be used as a variable declaration initializer or a class field",
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 26
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#snippet test()}
{@const der = $derived(0)}
{/snippet}

0 comments on commit 0e0f01e

Please sign in to comment.