Skip to content

Commit b6575cc

Browse files
committed
fix: don't warn on writes to $state
fixes #10905
1 parent f219c79 commit b6575cc

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

.changeset/calm-buses-clap.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: don't warn on writes to `$state`

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,7 @@ const common_visitors = {
12421242
if (
12431243
context.state.analysis.runes &&
12441244
node !== binding.node &&
1245+
context.state.function_depth === binding.scope.function_depth &&
12451246
// If we have $state that can be proxied or frozen and isn't re-assigned, then that means
12461247
// it's likely not using a primitive value and thus this warning isn't that helpful.
12471248
((binding.kind === 'state' &&
@@ -1252,7 +1253,12 @@ const common_visitors = {
12521253
!should_proxy_or_freeze(binding.initial.arguments[0], context.state.scope)))) ||
12531254
binding.kind === 'frozen_state' ||
12541255
binding.kind === 'derived') &&
1255-
context.state.function_depth === binding.scope.function_depth
1256+
// We're only concerned with reads here
1257+
parent.type !== 'AssignmentExpression' &&
1258+
parent.type !== 'UpdateExpression' &&
1259+
(parent.type !== 'MemberExpression' ||
1260+
(context.path.at(-2)?.type !== 'AssignmentExpression' &&
1261+
context.path.at(-2)?.type !== 'UpdateExpression'))
12561262
) {
12571263
w.state_referenced_locally(node);
12581264
}

packages/svelte/tests/validator/samples/static-state-reference/input.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
console.log(obj);
77
console.log(count);
88
console.log(doubled);
9+
// these are ok because they're writes
10+
count++;
11+
count = 1;
12+
obj.a++;
13+
obj.a = 1;
914
</script>
1015

1116
<button onclick={() => count += 1}>

0 commit comments

Comments
 (0)