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';
2121import { regex_starts_with_newline } from '../patterns.js' ;
2222import { create_attribute , is_element_node } from '../nodes.js' ;
2323import { DelegatedEvents } from '../../../constants.js' ;
24+ import { should_proxy_or_freeze } from '../3-transform/client/utils.js' ;
2425
2526/**
2627 * @param {import('#compiler').Script | null } script
@@ -918,7 +919,14 @@ const common_visitors = {
918919
919920 if (
920921 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 ) ) ) ) ||
922930 binding . kind === 'frozen_state' ||
923931 binding . kind === 'derived' ) &&
924932 context . state . function_depth === binding . scope . function_depth
Original file line number Diff line number Diff line change 11<script >
2+ let obj = $state ({ a: 0 });
23 let count = $state (0 );
34 let doubled = $derived (count * 2 );
45
6+ console .log (obj);
57 console .log (count);
68 console .log (doubled);
79 </script >
Original file line number Diff line number Diff line change 44 "message" : " State referenced in its own scope will never update. Did you mean to reference it inside a closure?" ,
55 "start" : {
66 "column" : 13 ,
7- "line" : 5
7+ "line" : 7
88 },
99 "end" : {
1010 "column" : 18 ,
11- "line" : 5
11+ "line" : 7
1212 }
1313 },
1414 {
1515 "code" : " static-state-reference" ,
1616 "message" : " State referenced in its own scope will never update. Did you mean to reference it inside a closure?" ,
1717 "start" : {
1818 "column" : 13 ,
19- "line" : 6
19+ "line" : 8
2020 },
2121 "end" : {
2222 "column" : 20 ,
23- "line" : 6
23+ "line" : 8
2424 }
2525 }
2626]
You can’t perform that action at this time.
0 commit comments