File tree Expand file tree Collapse file tree 3 files changed +17
-1
lines changed
src/compiler/phases/2-analyze
tests/validator/samples/static-state-reference Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " svelte " : patch
3
+ ---
4
+
5
+ fix: don't warn on writes to ` $state `
Original file line number Diff line number Diff line change @@ -1242,6 +1242,7 @@ const common_visitors = {
1242
1242
if (
1243
1243
context . state . analysis . runes &&
1244
1244
node !== binding . node &&
1245
+ context . state . function_depth === binding . scope . function_depth &&
1245
1246
// If we have $state that can be proxied or frozen and isn't re-assigned, then that means
1246
1247
// it's likely not using a primitive value and thus this warning isn't that helpful.
1247
1248
( ( binding . kind === 'state' &&
@@ -1252,7 +1253,12 @@ const common_visitors = {
1252
1253
! should_proxy_or_freeze ( binding . initial . arguments [ 0 ] , context . state . scope ) ) ) ) ||
1253
1254
binding . kind === 'frozen_state' ||
1254
1255
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' ) )
1256
1262
) {
1257
1263
w . state_referenced_locally ( node ) ;
1258
1264
}
Original file line number Diff line number Diff line change 6
6
console .log (obj);
7
7
console .log (count);
8
8
console .log (doubled);
9
+ // these are ok because they're writes
10
+ count++ ;
11
+ count = 1 ;
12
+ obj .a ++ ;
13
+ obj .a = 1 ;
9
14
</script >
10
15
11
16
<button onclick ={() => count += 1 }>
You can’t perform that action at this time.
0 commit comments