Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-fans-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

chore: improve $state static reference warning heuristics
10 changes: 9 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { create_attribute, is_element_node } from '../nodes.js';
import { DelegatedEvents } from '../../../constants.js';
import { should_proxy_or_freeze } from '../3-transform/client/utils.js';

/**
* @param {import('#compiler').Script | null} script
Expand Down Expand Up @@ -918,7 +919,14 @@ const common_visitors = {

if (
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 === 'derived') &&
context.state.function_depth === binding.scope.function_depth
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script>
let obj = $state({ a: 0 });
let count = $state(0);
let doubled = $derived(count * 2);

console.log(obj);
console.log(count);
console.log(doubled);
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
"message": "State referenced in its own scope will never update. Did you mean to reference it inside a closure?",
"start": {
"column": 13,
"line": 5
"line": 7
},
"end": {
"column": 18,
"line": 5
"line": 7
}
},
{
"code": "static-state-reference",
"message": "State referenced in its own scope will never update. Did you mean to reference it inside a closure?",
"start": {
"column": 13,
"line": 6
"line": 8
},
"end": {
"column": 20,
"line": 6
"line": 8
}
}
]