chore: improve $state static reference warning heuristics (#10275)

* chore: improve $state static reference warning heuristics

* fix bug

* update test

* lint
pull/10229/head
Dominic Gannaway 10 months ago committed by GitHub
parent 03c067f598
commit a476e81e63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
chore: improve $state static reference warning heuristics

@ -21,6 +21,7 @@ import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
import { regex_starts_with_newline } from '../patterns.js'; import { regex_starts_with_newline } from '../patterns.js';
import { create_attribute, is_element_node } from '../nodes.js'; import { create_attribute, is_element_node } from '../nodes.js';
import { DelegatedEvents } from '../../../constants.js'; import { DelegatedEvents } from '../../../constants.js';
import { should_proxy_or_freeze } from '../3-transform/client/utils.js';
/** /**
* @param {import('#compiler').Script | null} script * @param {import('#compiler').Script | null} script
@ -918,7 +919,14 @@ const common_visitors = {
if ( if (
node !== binding.node && node !== binding.node &&
(binding.kind === 'state' || // If we have $state that can be proxied or frozen and isn't re-assigned, then that means
// it's likely not using a primitive value and thus this warning isn't that helpful.
((binding.kind === 'state' &&
(binding.reassigned ||
(binding.initial?.type === 'CallExpression' &&
binding.initial.arguments.length === 1 &&
binding.initial.arguments[0].type !== 'SpreadElement' &&
!should_proxy_or_freeze(binding.initial.arguments[0], context.state.scope)))) ||
binding.kind === 'frozen_state' || binding.kind === 'frozen_state' ||
binding.kind === 'derived') && binding.kind === 'derived') &&
context.state.function_depth === binding.scope.function_depth context.state.function_depth === binding.scope.function_depth

@ -1,7 +1,9 @@
<script> <script>
let obj = $state({ a: 0 });
let count = $state(0); let count = $state(0);
let doubled = $derived(count * 2); let doubled = $derived(count * 2);
console.log(obj);
console.log(count); console.log(count);
console.log(doubled); console.log(doubled);
</script> </script>

@ -4,11 +4,11 @@
"message": "State referenced in its own scope will never update. Did you mean to reference it inside a closure?", "message": "State referenced in its own scope will never update. Did you mean to reference it inside a closure?",
"start": { "start": {
"column": 13, "column": 13,
"line": 5 "line": 7
}, },
"end": { "end": {
"column": 18, "column": 18,
"line": 5 "line": 7
} }
}, },
{ {
@ -16,11 +16,11 @@
"message": "State referenced in its own scope will never update. Did you mean to reference it inside a closure?", "message": "State referenced in its own scope will never update. Did you mean to reference it inside a closure?",
"start": { "start": {
"column": 13, "column": 13,
"line": 6 "line": 8
}, },
"end": { "end": {
"column": 20, "column": 20,
"line": 6 "line": 8
} }
} }
] ]

Loading…
Cancel
Save