mirror of https://github.com/sveltejs/svelte
breaking: apply fallback value every time in runes mode (#10797)
* breaking: apply fallback value every time in runes mode closes #9948 * apply fallback value in setter * encapsulate fallback logic * we should keep this logic out of b.set, since it's very specific to accessors * oops --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/10870/head
parent
e8ce41815a
commit
a339c28bb7
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
breaking: apply fallback value every time in runes mode
|
@ -0,0 +1,29 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
// Tests that default values only fire lazily when the prop is undefined, and every time
|
||||
export default test({
|
||||
props: {
|
||||
p0: 0,
|
||||
p1: 0,
|
||||
p2: 0,
|
||||
p3: 0
|
||||
},
|
||||
html: `<p>props: 0 0 0 0 1 1 1 1</p><p>log: nested.fallback_value,fallback_fn`,
|
||||
async test({ assert, target, component }) {
|
||||
component.p0 = undefined;
|
||||
component.p1 = undefined;
|
||||
component.p2 = undefined;
|
||||
component.p3 = undefined;
|
||||
// Nuance: these are already undefined in the props, but we're setting them to undefined again,
|
||||
// which calls the fallback value again, even if it will result in the same value. There's no way
|
||||
// to prevent this, and in practise it won't matter - and you shouldn't use accessors in runes mode anyway.
|
||||
component.p4 = undefined;
|
||||
component.p5 = undefined;
|
||||
component.p6 = undefined;
|
||||
component.p7 = undefined;
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<p>props: 1 1 1 1 1 1 1 1</p><p>log: nested.fallback_value,fallback_fn,nested.fallback_value,fallback_fn,nested.fallback_value,fallback_fn`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,29 @@
|
||||
<script>
|
||||
let log = $state([]);
|
||||
|
||||
const fallback_value = 1;
|
||||
const nested = {
|
||||
get fallback_value() {
|
||||
log.push('nested.fallback_value');
|
||||
return fallback_value;
|
||||
}
|
||||
}
|
||||
const fallback_fn = () => {
|
||||
log.push('fallback_fn');
|
||||
return fallback_value;
|
||||
}
|
||||
|
||||
const {
|
||||
p0 = 1,
|
||||
p1 = fallback_value,
|
||||
p2 = nested.fallback_value,
|
||||
p3 = fallback_fn(),
|
||||
p4 = 1,
|
||||
p5 = fallback_value,
|
||||
p6 = nested.fallback_value,
|
||||
p7 = fallback_fn()
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<p>props: {p0} {p1} {p2} {p3} {p4} {p5} {p6} {p7}</p>
|
||||
<p>log: {log}</p>
|
@ -1,23 +1,24 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
// Tests that default values only fire lazily when the prop is undefined, and only initially
|
||||
// Tests that default values only fire lazily when the prop is undefined, and every time
|
||||
export default test({
|
||||
props: {
|
||||
p0: 0,
|
||||
p1: 0,
|
||||
p2: 0,
|
||||
p3: 0
|
||||
},
|
||||
html: `<p>props: 0 0 0 0 1 1 1 1</p><p>log: nested.fallback_value,fallback_fn`,
|
||||
async test({ assert, target, component }) {
|
||||
component.p0 = undefined;
|
||||
component.p1 = undefined;
|
||||
component.p2 = undefined;
|
||||
component.p3 = undefined;
|
||||
component.p4 = undefined;
|
||||
component.p5 = undefined;
|
||||
component.p6 = undefined;
|
||||
component.p7 = undefined;
|
||||
assert.htmlEqual(target.innerHTML, `<p>props: </p><p>log: nested.fallback_value,fallback_fn`);
|
||||
html: `
|
||||
<p>props: 0 0 0 0 1 1 1 1</p>
|
||||
<p>log: nested.fallback_value,fallback_fn</p>
|
||||
<button>Set all to undefined</button>
|
||||
`,
|
||||
async test({ assert, target }) {
|
||||
const btn = target.querySelector('button');
|
||||
btn?.click();
|
||||
await tick();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<p>props: 1 1 1 1 1 1 1 1</p>
|
||||
<p>log: nested.fallback_value,fallback_fn,nested.fallback_value,fallback_fn</p>
|
||||
<button>Set all to undefined</button>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
@ -1,31 +1,25 @@
|
||||
<script>
|
||||
let log = $state([]);
|
||||
import Sub from "./sub.svelte";
|
||||
|
||||
const fallback_value = 1;
|
||||
const nested = {
|
||||
get fallback_value() {
|
||||
log.push('nested.fallback_value');
|
||||
log = log;
|
||||
return fallback_value;
|
||||
}
|
||||
}
|
||||
const fallback_fn = () => {
|
||||
log.push('fallback_fn');
|
||||
log = log;
|
||||
return fallback_value;
|
||||
}
|
||||
|
||||
const {
|
||||
p0 = 1,
|
||||
p1 = fallback_value,
|
||||
p2 = nested.fallback_value,
|
||||
p3 = fallback_fn(),
|
||||
p4 = 1,
|
||||
p5 = fallback_value,
|
||||
p6 = nested.fallback_value,
|
||||
p7 = fallback_fn()
|
||||
} = $props();
|
||||
let p0 = $state(0);
|
||||
let p1 = $state(0);
|
||||
let p2 = $state(0);
|
||||
let p3 = $state(0);
|
||||
let p4 = $state();
|
||||
let p5 = $state();
|
||||
let p6 = $state();
|
||||
let p7 = $state();
|
||||
</script>
|
||||
|
||||
<p>props: {p0} {p1} {p2} {p3} {p4} {p5} {p6} {p7}</p>
|
||||
<p>log: {log}</p>
|
||||
<Sub {p0} {p1} {p2} {p3} {p4} {p5} {p6} {p7} />
|
||||
|
||||
<button onclick={() => {
|
||||
p0 = undefined;
|
||||
p1 = undefined;
|
||||
p2 = undefined;
|
||||
p3 = undefined;
|
||||
p4 = undefined;
|
||||
p5 = undefined;
|
||||
p6 = undefined;
|
||||
p7 = undefined;
|
||||
}}>Set all to undefined</button>
|
||||
|
@ -0,0 +1,29 @@
|
||||
<script>
|
||||
let log = $state([]);
|
||||
|
||||
const fallback_value = 1;
|
||||
const nested = {
|
||||
get fallback_value() {
|
||||
log.push('nested.fallback_value');
|
||||
return fallback_value;
|
||||
}
|
||||
}
|
||||
const fallback_fn = () => {
|
||||
log.push('fallback_fn');
|
||||
return fallback_value;
|
||||
}
|
||||
|
||||
const {
|
||||
p0 = 1,
|
||||
p1 = fallback_value,
|
||||
p2 = nested.fallback_value,
|
||||
p3 = fallback_fn(),
|
||||
p4 = 1,
|
||||
p5 = fallback_value,
|
||||
p6 = nested.fallback_value,
|
||||
p7 = fallback_fn()
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<p>props: {p0} {p1} {p2} {p3} {p4} {p5} {p6} {p7}</p>
|
||||
<p>log: {log}</p>
|
Loading…
Reference in new issue