diff --git a/.changeset/clean-eels-beg.md b/.changeset/clean-eels-beg.md new file mode 100644 index 000000000000..2cf079196c74 --- /dev/null +++ b/.changeset/clean-eels-beg.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: emit useful error on invalid binding to derived state diff --git a/packages/svelte/src/compiler/errors.js b/packages/svelte/src/compiler/errors.js index 615123088f0f..700ec21e19e0 100644 --- a/packages/svelte/src/compiler/errors.js +++ b/packages/svelte/src/compiler/errors.js @@ -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 diff --git a/packages/svelte/src/compiler/phases/2-analyze/validation.js b/packages/svelte/src/compiler/phases/2-analyze/validation.js index 34be46269f98..8c6c3365b003 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/validation.js +++ b/packages/svelte/src/compiler/phases/2-analyze/validation.js @@ -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 } diff --git a/packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/_config.js b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/_config.js new file mode 100644 index 000000000000..3a27b1f9bdac --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/_config.js @@ -0,0 +1,8 @@ +import { test } from '../../test'; + +export default test({ + error: { + code: 'invalid-derived-binding', + message: 'Invalid binding to derived state' + } +}); diff --git a/packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/main.svelte b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/main.svelte new file mode 100644 index 000000000000..6c198dc068fe --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/main.svelte @@ -0,0 +1,6 @@ + + +