fix: silence assignment warning on more function bindings (#15644)

* fix: silence assignment warning on more function bindings

fixes #15550

* Update packages/svelte/tests/html_equal.js
pull/15651/head
Simon H 6 months ago committed by GitHub
parent 046145fdd7
commit 97542048df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: silence assignment warning on more function bindings

@ -165,7 +165,9 @@ function build_assignment(operator, left, right, context) {
path.at(-1) === 'SvelteComponent' ||
(path.at(-1) === 'ArrowFunctionExpression' &&
path.at(-2) === 'SequenceExpression' &&
(path.at(-3) === 'Component' || path.at(-3) === 'SvelteComponent'))
(path.at(-3) === 'Component' ||
path.at(-3) === 'SvelteComponent' ||
path.at(-3) === 'BindDirective'))
) {
should_transform = false;
}

@ -86,7 +86,7 @@ export function normalize_html(
clean_children(node);
return node.innerHTML;
} catch (err) {
throw new Error(`Failed to normalize HTML:\n${html}`);
throw new Error(`Failed to normalize HTML:\n${html}\nCause: ${err}`);
}
}

@ -6,7 +6,7 @@ export default test({
dev: true
},
html: `<button>items: null</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2">`,
html: `<button>items: null</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2"><input>`,
test({ assert, target, warnings }) {
const btn = target.querySelector('button');
@ -15,13 +15,13 @@ export default test({
flushSync(() => btn.click());
assert.htmlEqual(
target.innerHTML,
`<button>items: []</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2">`
`<button>items: []</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2"><input>`
);
flushSync(() => btn.click());
assert.htmlEqual(
target.innerHTML,
`<button>items: [0]</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2">`
`<button>items: [0]</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2"><input>`
);
const input = target.querySelector('input');
@ -30,7 +30,7 @@ export default test({
flushSync(() => input.dispatchEvent(new Event('change', { bubbles: true })));
assert.deepEqual(warnings, [
'Assignment to `items` property (main.svelte:8:24) will evaluate to the right-hand side, not the value of `items` following the assignment. This may result in unexpected behaviour.'
'Assignment to `items` property (main.svelte:9:24) will evaluate to the right-hand side, not the value of `items` following the assignment. This may result in unexpected behaviour.'
]);
}
});

@ -3,6 +3,7 @@
let entries = $state([]);
let object = $state({ items: null, group: [] });
let elementFunBind = $state();
</script>
<button onclick={() => (object.items ??= []).push(object.items.length)}>
@ -13,6 +14,12 @@
<div bind:this={entries[0]}>x</div>
<input type="checkbox" value=1 bind:group={object.group}>
<input type="checkbox" value=2 bind:group={object.group}>
<Test bind:this={entries[1]}></Test>
<Test bind:this={() => entries[2], (v) => (entries[2] = v)}></Test>
<Test bind:x={entries[3]}></Test>
{#snippet funBind(context)}
<input bind:this={() => {}, (e) => (context.element = e)} />
{/snippet}
{@render funBind({ set element(e) { elementFunBind = e } })}

Loading…
Cancel
Save