File tree Expand file tree Collapse file tree 4 files changed +20
-5
lines changed
src/compiler/phases/2-analyze
tests/validator/samples/static-state-reference Expand file tree Collapse file tree 4 files changed +20
-5
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " svelte " : patch
3
+ ---
4
+
5
+ chore: improve $state static reference warning heuristics
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
21
21
import { regex_starts_with_newline } from '../patterns.js' ;
22
22
import { create_attribute , is_element_node } from '../nodes.js' ;
23
23
import { DelegatedEvents } from '../../../constants.js' ;
24
+ import { should_proxy_or_freeze } from '../3-transform/client/utils.js' ;
24
25
25
26
/**
26
27
* @param {import('#compiler').Script | null } script
@@ -918,7 +919,14 @@ const common_visitors = {
918
919
919
920
if (
920
921
node !== binding . node &&
921
- ( binding . kind === 'state' ||
922
+ // If we have $state that can be proxied or frozen and isn't re-assigned, then that means
923
+ // it's likely not using a primitive value and thus this warning isn't that helpful.
924
+ ( ( binding . kind === 'state' &&
925
+ ( binding . reassigned ||
926
+ ( binding . initial ?. type === 'CallExpression' &&
927
+ binding . initial . arguments . length === 1 &&
928
+ binding . initial . arguments [ 0 ] . type !== 'SpreadElement' &&
929
+ ! should_proxy_or_freeze ( binding . initial . arguments [ 0 ] , context . state . scope ) ) ) ) ||
922
930
binding . kind === 'frozen_state' ||
923
931
binding . kind === 'derived' ) &&
924
932
context . state . function_depth === binding . scope . function_depth
Original file line number Diff line number Diff line change 1
1
<script >
2
+ let obj = $state ({ a: 0 });
2
3
let count = $state (0 );
3
4
let doubled = $derived (count * 2 );
4
5
6
+ console .log (obj);
5
7
console .log (count);
6
8
console .log (doubled);
7
9
</script >
Original file line number Diff line number Diff line change 4
4
"message" : " State referenced in its own scope will never update. Did you mean to reference it inside a closure?" ,
5
5
"start" : {
6
6
"column" : 13 ,
7
- "line" : 5
7
+ "line" : 7
8
8
},
9
9
"end" : {
10
10
"column" : 18 ,
11
- "line" : 5
11
+ "line" : 7
12
12
}
13
13
},
14
14
{
15
15
"code" : " static-state-reference" ,
16
16
"message" : " State referenced in its own scope will never update. Did you mean to reference it inside a closure?" ,
17
17
"start" : {
18
18
"column" : 13 ,
19
- "line" : 6
19
+ "line" : 8
20
20
},
21
21
"end" : {
22
22
"column" : 20 ,
23
- "line" : 6
23
+ "line" : 8
24
24
}
25
25
}
26
26
]
You can’t perform that action at this time.
0 commit comments