Skip to content

fix: prevent ownership warnings if the fallback of a bindable is used #15720

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

Merged
merged 5 commits into from
Apr 10, 2025
Merged
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/wise-turkeys-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: prevent ownership warnings if the fallback of a bindable is used
12 changes: 8 additions & 4 deletions packages/svelte/src/internal/client/dev/ownership.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function create_ownership_validator(props) {
*/
mutation: (prop, path, result, line, column) => {
const name = path[0];
if (is_bound(props, name) || !parent) {
if (is_bound_or_unset(props, name) || !parent) {
return result;
}

Expand All @@ -52,7 +52,7 @@ export function create_ownership_validator(props) {
* @param {() => any} value
*/
binding: (key, child_component, value) => {
if (!is_bound(props, key) && parent && value()?.[STATE_SYMBOL]) {
if (!is_bound_or_unset(props, key) && parent && value()?.[STATE_SYMBOL]) {
w.ownership_invalid_binding(
component[FILENAME],
key,
Expand All @@ -68,9 +68,13 @@ export function create_ownership_validator(props) {
* @param {Record<string, any>} props
* @param {string} prop_name
*/
function is_bound(props, prop_name) {
function is_bound_or_unset(props, prop_name) {
// Can be the case when someone does `mount(Component, props)` with `let props = $state({...})`
// or `createClassComponent(Component, props)`
const is_entry_props = STATE_SYMBOL in props || LEGACY_PROPS in props;
return !!get_descriptor(props, prop_name)?.set || (is_entry_props && prop_name in props);
return (
!!get_descriptor(props, prop_name)?.set ||
(is_entry_props && prop_name in props) ||
!(prop_name in props)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that's all correct the function name now is confusing because it no longer is what this check is about. is_bound_or_unset maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm yeah let me change that, even tho I read that as "if it's unset is automatically bound" but maybe it's better to rename it

);
}
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PROPS_IS_RUNES,
PROPS_IS_UPDATED
} from '../../../constants.js';
import { get_descriptor, is_function } from '../../shared/utils.js';
import { define_property, get_descriptor, is_function } from '../../shared/utils.js';
import { mutable_source, set, source, update } from './sources.js';
import { derived, derived_safe_equal } from './deriveds.js';
import { get, captured_signals, untrack } from '../runtime.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
const { test = $bindable() } = $props();
</script>

{test}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Child from './Child.svelte';

let { test = $bindable({}) } = $props();
</script>

<Child bind:test />
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from '../../test';

export default test({
mode: ['client'],
compileOptions: {
dev: true
},
async test({ warnings, assert }) {
assert.deepEqual(warnings, []);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Parent from './Parent.svelte';
</script>

<Parent />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
let { test = $bindable({}) } = $props();
</script>

<button onclick={()=>test = {}}></button>
<button onclick={()=>test.test = {}}></button>

{test}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client'],
compileOptions: {
dev: true
},
async test({ warnings, assert, target }) {
const [btn, btn2] = target.querySelectorAll('button');
flushSync(() => {
btn2.click();
});
assert.deepEqual(warnings, []);
flushSync(() => {
btn.click();
});
flushSync(() => {
btn2.click();
});
assert.deepEqual(warnings, []);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Parent from './Parent.svelte';
</script>

<Parent />