From ede1edd314452a1bfd729503e990cf06e04da641 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 16 Nov 2023 17:18:04 -0500 Subject: [PATCH] fix: emit useful error on invalid binding to derived state (#9497) closes #9495 --- .changeset/clean-eels-beg.md | 5 +++++ packages/svelte/src/compiler/errors.js | 1 + .../svelte/src/compiler/phases/2-analyze/validation.js | 4 ++++ .../samples/runes-no-derived-binding/_config.js | 8 ++++++++ .../samples/runes-no-derived-binding/main.svelte | 6 ++++++ 5 files changed, 24 insertions(+) create mode 100644 .changeset/clean-eels-beg.md create mode 100644 packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/_config.js create mode 100644 packages/svelte/tests/compiler-errors/samples/runes-no-derived-binding/main.svelte 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 @@ + + +