From 76cb89c75000b384946adfea39cfe83e47d1bd8f Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 5 Aug 2024 16:26:33 +0100 Subject: [PATCH] fix: allow deletion of $$restProps properties (#12736) --- .changeset/loud-news-deliver.md | 5 +++++ .../src/internal/client/reactivity/props.js | 7 +++++++ .../samples/rest-props-delete/App.svelte | 10 ++++++++++ .../samples/rest-props-delete/_config.js | 15 +++++++++++++++ .../samples/rest-props-delete/main.svelte | 5 +++++ 5 files changed, 42 insertions(+) create mode 100644 .changeset/loud-news-deliver.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/rest-props-delete/App.svelte create mode 100644 packages/svelte/tests/runtime-legacy/samples/rest-props-delete/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/rest-props-delete/main.svelte diff --git a/.changeset/loud-news-deliver.md b/.changeset/loud-news-deliver.md new file mode 100644 index 0000000000..f562caff8f --- /dev/null +++ b/.changeset/loud-news-deliver.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: allow deletion of $$restProps properties diff --git a/packages/svelte/src/internal/client/reactivity/props.js b/packages/svelte/src/internal/client/reactivity/props.js index b822f8d4ec..81c3e4d743 100644 --- a/packages/svelte/src/internal/client/reactivity/props.js +++ b/packages/svelte/src/internal/client/reactivity/props.js @@ -126,6 +126,13 @@ const legacy_rest_props_handler = { }; } }, + deleteProperty(target, key) { + // Svelte 4 allowed for deletions on $$restProps + if (target.exclude.includes(key)) return false; + target.exclude.push(key); + update(target.version); + return true; + }, has(target, key) { if (target.exclude.includes(key)) return false; return key in target.props; diff --git a/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/App.svelte b/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/App.svelte new file mode 100644 index 0000000000..37825576e5 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/App.svelte @@ -0,0 +1,10 @@ + + + + +{JSON.stringify($$restProps)} diff --git a/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/_config.js b/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/_config.js new file mode 100644 index 0000000000..853af731f9 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/_config.js @@ -0,0 +1,15 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + html: `\n{"a":1,"b":2}`, + + test({ assert, target }) { + const [btn1] = target.querySelectorAll('button'); + + btn1.click(); + flushSync(); + + assert.htmlEqual(target.innerHTML, `\n{"b":2}`); + } +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/main.svelte b/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/main.svelte new file mode 100644 index 0000000000..461848a3b3 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/rest-props-delete/main.svelte @@ -0,0 +1,5 @@ + + +