Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: warn instead of error when binding a non-existent prop #13416

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-pans-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: do not error when binding a non-existent prop
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,26 @@ export function client_component(analysis, options) {

const properties = [...analysis.instance.scope.declarations].filter(
([name, binding]) =>
(binding.kind === 'prop' || binding.kind === 'bindable_prop') && !name.startsWith('$$')
(binding.kind === 'prop' ||
binding.kind === 'bindable_prop' ||
binding.kind === 'rest_prop') &&
!name.startsWith('$$')
);

if (dev && analysis.runes) {
const exports = analysis.exports.map(({ name, alias }) => b.literal(alias ?? name));
/** @type {ESTree.Literal[]} */
const bindable = [];
const non_bindable = [];
let has_rest = false;

for (const [name, binding] of properties) {
if (binding.kind === 'bindable_prop') {
bindable.push(b.literal(binding.prop_alias ?? name));
} else if (binding.kind === 'rest_prop') {
has_rest = true;
} else {
non_bindable.push(b.literal(binding.prop_alias ?? name));
}
}
instance.body.unshift(
Expand All @@ -304,8 +314,10 @@ export function client_component(analysis, options) {
'$.validate_prop_bindings',
b.id('$$props'),
b.array(bindable),
b.array(non_bindable),
b.array(exports),
b.id(`${analysis.name}`)
b.id(`${analysis.name}`),
b.literal(has_rest)
)
)
);
Expand Down
15 changes: 13 additions & 2 deletions packages/svelte/src/internal/client/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ export function validate_each_keys(collection, key_fn) {
/**
* @param {Record<string, any>} $$props
* @param {string[]} bindable
* @param {string[]} non_bindable
* @param {string[]} exports
* @param {Function & { [FILENAME]: string }} component
* @param {boolean} has_rest
*/
export function validate_prop_bindings($$props, bindable, exports, component) {
export function validate_prop_bindings(
$$props,
bindable,
non_bindable,
exports,
component,
has_rest
) {
for (const key in $$props) {
var setter = get_descriptor($$props, key)?.set;
var name = component.name;
Expand All @@ -63,7 +72,9 @@ export function validate_prop_bindings($$props, bindable, exports, component) {
}

if (!bindable.includes(key)) {
e.bind_not_bindable(key, component[FILENAME], name);
if (has_rest || non_bindable.includes(key)) {
e.bind_not_bindable(key, component[FILENAME], name);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let {
value = $bindable()
} = $props()
</script>

<input type="text" bind:value />
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
let {
value = $bindable(),
content = $bindable(),
} = $props()
</script>
<div bind:innerText={content} contenteditable>
</div>
<input type="text" bind:value />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import Input from './Input.svelte';
import InputEditable from './InputEditable.svelte';

const components = [
Input,
InputEditable
]

const results = $state([{}, {}]);
</script>

{#each components as Component, i}
<Component bind:content={results[i].content} bind:value={results[i].value} />
{/each}
Loading