Skip to content

Commit

Permalink
fix: improve non state referenced warning (#9809)
Browse files Browse the repository at this point in the history
* fix: improve non state referenced warning

* add test
  • Loading branch information
trueadm authored Dec 6, 2023
1 parent d793d57 commit d5167e7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-dolphins-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: improve non state referenced warning
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from '../../test';

export default test({});
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

1 comment on commit d5167e7

@vercel
Copy link

@vercel vercel bot commented on d5167e7 Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview.vercel.app
svelte-octane.vercel.app
svelte-5-preview-svelte.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.