fix: disallow $state/$derived in const tags (#15115)

pull/15129/head
Rich Harris 8 months ago committed by GitHub
parent 94d80c6e2c
commit 0e0f01ee1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: disallow $state/$derived in const tags

@ -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);

@ -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
}
}
]

@ -0,0 +1,3 @@
{#snippet test()}
{@const der = $derived(0)}
{/snippet}
Loading…
Cancel
Save