Skip to content

Commit

Permalink
fix: emit useful error on invalid binding to derived state (#9497)
Browse files Browse the repository at this point in the history
closes #9495
  • Loading branch information
Rich-Harris authored Nov 16, 2023
1 parent 37f2493 commit ede1edd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-eels-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: emit useful error on invalid binding to derived state
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ const runes = {
: ''
}`,
'invalid-derived-assignment': () => `Invalid assignment to derived state`,
'invalid-derived-binding': () => `Invalid binding to derived state`,
/**
* @param {string} rune
* @param {number[]} args
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ export const validation = {
error(node.expression, 'invalid-binding-value');
}

if (binding.kind === 'derived') {
error(node.expression, 'invalid-derived-binding');
}

// TODO handle mutations of non-state/props in runes mode
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from '../../test';

export default test({
error: {
code: 'invalid-derived-binding',
message: 'Invalid binding to derived state'
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
let a = $state(0);
let b = $derived({ a });
</script>

<input type="number" bind:value={b} />

1 comment on commit ede1edd

@vercel
Copy link

@vercel vercel bot commented on ede1edd Nov 16, 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-svelte.vercel.app
svelte-octane.vercel.app
svelte-5-preview-git-main-svelte.vercel.app
svelte-5-preview.vercel.app

Please sign in to comment.