From d7a88dadc75e7be772b7e26d59157bb9023026af Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 4 Jan 2024 13:43:44 +0100 Subject: [PATCH 1/2] feat: allow modifiying derived props --- .../src/compiler/phases/2-analyze/validation.js | 14 +++++++------- .../runes-no-derived-assignment/main.svelte | 4 ++-- .../samples/runes-no-derived-update/main.svelte | 4 ++-- .../samples/derived-proxy/_config.js | 15 +++++++++++++++ .../samples/derived-proxy/main.svelte | 7 +++++++ 5 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 packages/svelte/tests/runtime-runes/samples/derived-proxy/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/derived-proxy/main.svelte diff --git a/packages/svelte/src/compiler/phases/2-analyze/validation.js b/packages/svelte/src/compiler/phases/2-analyze/validation.js index 40244f97d0cc..f64b0268ab1f 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/validation.js +++ b/packages/svelte/src/compiler/phases/2-analyze/validation.js @@ -854,6 +854,13 @@ function validate_assignment(node, argument, state) { let left = /** @type {import('estree').Expression | import('estree').Super} */ (argument); + if (left.type === 'Identifier') { + const binding = state.scope.get(left.name); + if (binding?.kind === 'derived') { + error(node, 'invalid-derived-assignment'); + } + } + /** @type {import('estree').Expression | import('estree').PrivateIdentifier | null} */ let property = null; @@ -862,13 +869,6 @@ function validate_assignment(node, argument, state) { left = left.object; } - if (left.type === 'Identifier') { - const binding = state.scope.get(left.name); - if (binding?.kind === 'derived') { - error(node, 'invalid-derived-assignment'); - } - } - if (left.type === 'ThisExpression' && property?.type === 'PrivateIdentifier') { if (state.private_derived_state.includes(property.name)) { error(node, 'invalid-derived-assignment'); diff --git a/packages/svelte/tests/compiler-errors/samples/runes-no-derived-assignment/main.svelte b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-assignment/main.svelte index f89cc7b6bab6..3bf836f6c586 100644 --- a/packages/svelte/tests/compiler-errors/samples/runes-no-derived-assignment/main.svelte +++ b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-assignment/main.svelte @@ -1,5 +1,5 @@ diff --git a/packages/svelte/tests/compiler-errors/samples/runes-no-derived-update/main.svelte b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-update/main.svelte index a4aca48089eb..d266c95bb872 100644 --- a/packages/svelte/tests/compiler-errors/samples/runes-no-derived-update/main.svelte +++ b/packages/svelte/tests/compiler-errors/samples/runes-no-derived-update/main.svelte @@ -1,5 +1,5 @@ diff --git a/packages/svelte/tests/runtime-runes/samples/derived-proxy/_config.js b/packages/svelte/tests/runtime-runes/samples/derived-proxy/_config.js new file mode 100644 index 000000000000..32340d018a0e --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/derived-proxy/_config.js @@ -0,0 +1,15 @@ +import { test } from '../../test'; + +export default test({ + html: ``, + + async test({ assert, target }) { + const [btn1, btn2] = target.querySelectorAll('button'); + + await btn1?.click(); + assert.htmlEqual(target.innerHTML, ``); + + await btn2?.click(); + assert.htmlEqual(target.innerHTML, ``); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/derived-proxy/main.svelte b/packages/svelte/tests/runtime-runes/samples/derived-proxy/main.svelte new file mode 100644 index 000000000000..ddbc56aa7b09 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/derived-proxy/main.svelte @@ -0,0 +1,7 @@ + + + + From 1794b0fbbb846539303267813f545d0fc2093d6f Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 4 Jan 2024 13:47:17 +0100 Subject: [PATCH 2/2] changeset --- .changeset/hungry-trees-travel.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-trees-travel.md diff --git a/.changeset/hungry-trees-travel.md b/.changeset/hungry-trees-travel.md new file mode 100644 index 000000000000..d90a448daa44 --- /dev/null +++ b/.changeset/hungry-trees-travel.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: allow modifiying derived props