mirror of https://github.com/sveltejs/svelte
feat: warn if binding to a non-reactive property (#12500)
* feat: warn if binding to a non-reactive property * tweakpull/12504/head
parent
dd9ade7736
commit
2ce2b7d98b
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
feat: warn if binding to a non-reactive property
|
@ -0,0 +1,5 @@
|
||||
<script lang="ts">
|
||||
let { value = $bindable() }: { value: number } = $props();
|
||||
</script>
|
||||
|
||||
<p>{value}</p>
|
@ -0,0 +1,16 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
compileOptions: {
|
||||
dev: true
|
||||
},
|
||||
|
||||
async test({ assert, warnings }) {
|
||||
assert.deepEqual(warnings, [
|
||||
`\`bind:value={pojo.value}\` (main.svelte:50:7) is binding to a non-reactive property`,
|
||||
`\`bind:value={frozen.value}\` (main.svelte:51:7) is binding to a non-reactive property`,
|
||||
`\`bind:value={pojo.value}\` (main.svelte:52:7) is binding to a non-reactive property`,
|
||||
`\`bind:value={frozen.value}\` (main.svelte:53:7) is binding to a non-reactive property`
|
||||
]);
|
||||
}
|
||||
});
|
@ -0,0 +1,61 @@
|
||||
<script>
|
||||
import Child from './Child.svelte';
|
||||
|
||||
let pojo = {
|
||||
value: 1
|
||||
};
|
||||
|
||||
let frozen = $state.frozen({
|
||||
value: 2
|
||||
});
|
||||
|
||||
let reactive = $state({
|
||||
value: 3
|
||||
});
|
||||
|
||||
let value = $state(4);
|
||||
let accessors = {
|
||||
get value() {
|
||||
return value;
|
||||
},
|
||||
set value(v) {
|
||||
value = v;
|
||||
}
|
||||
};
|
||||
|
||||
let proxied = $state(5);
|
||||
let proxy = new Proxy(
|
||||
{},
|
||||
{
|
||||
get(target, prop, receiver) {
|
||||
if (prop === 'value') {
|
||||
return proxied;
|
||||
}
|
||||
|
||||
return Reflect.get(target, prop, receiver);
|
||||
},
|
||||
set(target, prop, value, receiver) {
|
||||
if (prop === 'value') {
|
||||
proxied = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
return Reflect.set(target, prop, value, receiver);
|
||||
}
|
||||
}
|
||||
);
|
||||
</script>
|
||||
|
||||
<!-- should warn -->
|
||||
<input bind:value={pojo.value} />
|
||||
<input bind:value={frozen.value} />
|
||||
<Child bind:value={pojo.value} />
|
||||
<Child bind:value={frozen.value} />
|
||||
|
||||
<!-- should not warn -->
|
||||
<input bind:value={reactive.value} />
|
||||
<input bind:value={accessors.value} />
|
||||
<input bind:value={proxy.value} />
|
||||
<Child bind:value={reactive.value} />
|
||||
<Child bind:value={accessors.value} />
|
||||
<Child bind:value={proxy.value} />
|
Loading…
Reference in new issue