Skip to content

Commit a476e81

Browse files
authored
chore: improve $state static reference warning heuristics (#10275)
* chore: improve $state static reference warning heuristics * fix bug * update test * lint
1 parent 03c067f commit a476e81

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

.changeset/swift-fans-stare.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+
chore: improve $state static reference warning heuristics

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
2121
import { regex_starts_with_newline } from '../patterns.js';
2222
import { create_attribute, is_element_node } from '../nodes.js';
2323
import { 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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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>

packages/svelte/tests/validator/samples/static-state-reference/warnings.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
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
]

0 commit comments

Comments
 (0)