fix: improve non state referenced warning (#9809)

* fix: improve non state referenced warning

* add test
pull/9816/head
Dominic Gannaway 1 year ago committed by GitHub
parent d793d570e2
commit d5167e75b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: improve non state referenced warning

@ -409,10 +409,10 @@ export function analyze_component(root, options) {
analysis.reactive_statements = order_reactive_statements(analysis.reactive_statements);
}
// warn on any nonstate declarations that are a) mutated and b) referenced in the template
// warn on any nonstate declarations that are a) reassigned and mutated and b) referenced in the template
for (const scope of [module.scope, instance.scope]) {
outer: for (const [name, binding] of scope.declarations) {
if (binding.kind === 'normal' && binding.mutated) {
if (binding.kind === 'normal' && binding.reassigned && binding.mutated) {
for (const { path } of binding.references) {
if (path[0].type !== 'Fragment') continue;
for (let i = 1; i < path.length; i += 1) {

@ -0,0 +1,3 @@
import { test } from '../../test';
export default test({});

@ -0,0 +1,6 @@
<script>
let a = $state({b: 0});
</script>
<button onclick={() => a.b += 1}>a += 1</button>
<p>{JSON.stringify(a)} + {a.b}</p>
Loading…
Cancel
Save