From ce4f9722dab8d99140d410a9d1f71ec19d16535e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 8 Jan 2025 09:54:29 -0500 Subject: [PATCH] fix: remove bindable prop validation (#14946) --- .changeset/flat-otters-cheer.md | 5 ++++ .../3-transform/client/transform-client.js | 22 ---------------- packages/svelte/src/internal/client/index.js | 2 +- .../src/internal/client/reactivity/props.js | 6 +++-- .../svelte/src/internal/client/validate.js | 25 +------------------ .../samples/export-binding/_config.js | 10 -------- .../export-binding/counter/index.svelte | 8 ------ .../samples/export-binding/main.svelte | 7 ------ .../props-not-bindable-spread/Counter.svelte | 5 ---- .../props-not-bindable-spread/_config.js | 13 ---------- .../props-not-bindable-spread/main.svelte | 7 ------ .../samples/props-not-bindable/Counter.svelte | 5 ---- .../samples/props-not-bindable/_config.js | 13 ---------- .../samples/props-not-bindable/main.svelte | 7 ------ 14 files changed, 11 insertions(+), 124 deletions(-) create mode 100644 .changeset/flat-otters-cheer.md delete mode 100644 packages/svelte/tests/runtime-runes/samples/export-binding/_config.js delete mode 100644 packages/svelte/tests/runtime-runes/samples/export-binding/counter/index.svelte delete mode 100644 packages/svelte/tests/runtime-runes/samples/export-binding/main.svelte delete mode 100644 packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/Counter.svelte delete mode 100644 packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/_config.js delete mode 100644 packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/main.svelte delete mode 100644 packages/svelte/tests/runtime-runes/samples/props-not-bindable/Counter.svelte delete mode 100644 packages/svelte/tests/runtime-runes/samples/props-not-bindable/_config.js delete mode 100644 packages/svelte/tests/runtime-runes/samples/props-not-bindable/main.svelte diff --git a/.changeset/flat-otters-cheer.md b/.changeset/flat-otters-cheer.md new file mode 100644 index 000000000000..3f84de824247 --- /dev/null +++ b/.changeset/flat-otters-cheer.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: remove bindable prop validation diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index 90901d29ce7d..a969117ed353 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -299,28 +299,6 @@ export function client_component(analysis, options) { (binding.kind === 'prop' || binding.kind === 'bindable_prop') && !name.startsWith('$$') ); - if (dev && analysis.runes) { - const exports = analysis.exports.map(({ name, alias }) => b.literal(alias ?? name)); - /** @type {ESTree.Literal[]} */ - const bindable = []; - for (const [name, binding] of properties) { - if (binding.kind === 'bindable_prop') { - bindable.push(b.literal(binding.prop_alias ?? name)); - } - } - instance.body.unshift( - b.stmt( - b.call( - '$.validate_prop_bindings', - b.id('$$props'), - b.array(bindable), - b.array(exports), - b.id(`${analysis.name}`) - ) - ) - ); - } - if (analysis.accessors) { for (const [name, binding] of properties) { const key = binding.prop_alias ?? name; diff --git a/packages/svelte/src/internal/client/index.js b/packages/svelte/src/internal/client/index.js index 3b85ae18166e..2bf58c51f75d 100644 --- a/packages/svelte/src/internal/client/index.js +++ b/packages/svelte/src/internal/client/index.js @@ -150,7 +150,7 @@ export { setContext, hasContext } from './runtime.js'; -export { validate_binding, validate_each_keys, validate_prop_bindings } from './validate.js'; +export { validate_binding, validate_each_keys } from './validate.js'; export { raf } from './timing.js'; export { proxy } from './proxy.js'; export { create_custom_element } from './dom/elements/custom-element.js'; diff --git a/packages/svelte/src/internal/client/reactivity/props.js b/packages/svelte/src/internal/client/reactivity/props.js index 8fb13f7e2cd0..3e5a0258c744 100644 --- a/packages/svelte/src/internal/client/reactivity/props.js +++ b/packages/svelte/src/internal/client/reactivity/props.js @@ -297,8 +297,10 @@ export function prop(props, key, flags, fallback) { var is_entry_props = STATE_SYMBOL in props || LEGACY_PROPS in props; var setter = - get_descriptor(props, key)?.set ?? - (is_entry_props && bindable && key in props ? (v) => (props[key] = v) : undefined); + (bindable && + (get_descriptor(props, key)?.set ?? + (is_entry_props && key in props && ((v) => (props[key] = v))))) || + undefined; var fallback_value = /** @type {V} */ (fallback); var fallback_dirty = true; diff --git a/packages/svelte/src/internal/client/validate.js b/packages/svelte/src/internal/client/validate.js index 417c145eb2ec..24e280edf88b 100644 --- a/packages/svelte/src/internal/client/validate.js +++ b/packages/svelte/src/internal/client/validate.js @@ -1,5 +1,5 @@ import { dev_current_component_function } from './runtime.js'; -import { get_descriptor, is_array } from '../shared/utils.js'; +import { is_array } from '../shared/utils.js'; import * as e from './errors.js'; import { FILENAME } from '../../constants.js'; import { render_effect } from './reactivity/effects.js'; @@ -38,29 +38,6 @@ export function validate_each_keys(collection, key_fn) { }); } -/** - * @param {Record} $$props - * @param {string[]} bindable - * @param {string[]} exports - * @param {Function & { [FILENAME]: string }} component - */ -export function validate_prop_bindings($$props, bindable, exports, component) { - for (const key in $$props) { - var setter = get_descriptor($$props, key)?.set; - var name = component.name; - - if (setter) { - if (exports.includes(key) && !bindable.includes(key)) { - e.bind_invalid_export(component[FILENAME], key, name); - } - - if (!bindable.includes(key)) { - e.bind_not_bindable(key, component[FILENAME], name); - } - } - } -} - /** * @param {string} binding * @param {() => Record} get_object diff --git a/packages/svelte/tests/runtime-runes/samples/export-binding/_config.js b/packages/svelte/tests/runtime-runes/samples/export-binding/_config.js deleted file mode 100644 index d3b9c8e4eb69..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/export-binding/_config.js +++ /dev/null @@ -1,10 +0,0 @@ -import { test } from '../../test'; - -export default test({ - compileOptions: { - dev: true // to ensure we we catch the error - }, - error: - 'bind_invalid_export\n' + - 'Component counter/index.svelte has an export named `increment` that a consumer component is trying to access using `bind:increment`, which is disallowed. Instead, use `bind:this` (e.g. ``) and then access the property on the bound component instance (e.g. `component.increment`)' -}); diff --git a/packages/svelte/tests/runtime-runes/samples/export-binding/counter/index.svelte b/packages/svelte/tests/runtime-runes/samples/export-binding/counter/index.svelte deleted file mode 100644 index 14e0de961b21..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/export-binding/counter/index.svelte +++ /dev/null @@ -1,8 +0,0 @@ - - -{count} diff --git a/packages/svelte/tests/runtime-runes/samples/export-binding/main.svelte b/packages/svelte/tests/runtime-runes/samples/export-binding/main.svelte deleted file mode 100644 index 4ad1684701f9..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/export-binding/main.svelte +++ /dev/null @@ -1,7 +0,0 @@ - - - - diff --git a/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/Counter.svelte b/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/Counter.svelte deleted file mode 100644 index f22fd6e976dd..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/Counter.svelte +++ /dev/null @@ -1,5 +0,0 @@ - - -{rest.count} diff --git a/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/_config.js b/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/_config.js deleted file mode 100644 index fa0994c3704a..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/_config.js +++ /dev/null @@ -1,13 +0,0 @@ -import { test } from '../../test'; - -export default test({ - compileOptions: { - dev: true - }, - - html: '0', - - error: - 'bind_not_bindable\n' + - 'A component is attempting to bind to a non-bindable property `count` belonging to Counter.svelte (i.e. ``). To mark a property as bindable: `let { count = $bindable() } = $props()`' -}); diff --git a/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/main.svelte b/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/main.svelte deleted file mode 100644 index 80242b75c6dd..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/props-not-bindable-spread/main.svelte +++ /dev/null @@ -1,7 +0,0 @@ - - - diff --git a/packages/svelte/tests/runtime-runes/samples/props-not-bindable/Counter.svelte b/packages/svelte/tests/runtime-runes/samples/props-not-bindable/Counter.svelte deleted file mode 100644 index 4bc2db3968e7..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/props-not-bindable/Counter.svelte +++ /dev/null @@ -1,5 +0,0 @@ - - -{count} diff --git a/packages/svelte/tests/runtime-runes/samples/props-not-bindable/_config.js b/packages/svelte/tests/runtime-runes/samples/props-not-bindable/_config.js deleted file mode 100644 index fa0994c3704a..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/props-not-bindable/_config.js +++ /dev/null @@ -1,13 +0,0 @@ -import { test } from '../../test'; - -export default test({ - compileOptions: { - dev: true - }, - - html: '0', - - error: - 'bind_not_bindable\n' + - 'A component is attempting to bind to a non-bindable property `count` belonging to Counter.svelte (i.e. ``). To mark a property as bindable: `let { count = $bindable() } = $props()`' -}); diff --git a/packages/svelte/tests/runtime-runes/samples/props-not-bindable/main.svelte b/packages/svelte/tests/runtime-runes/samples/props-not-bindable/main.svelte deleted file mode 100644 index 80242b75c6dd..000000000000 --- a/packages/svelte/tests/runtime-runes/samples/props-not-bindable/main.svelte +++ /dev/null @@ -1,7 +0,0 @@ - - -