Skip to content

Commit

Permalink
fix: silence false-positive statel value warning (#14958)
Browse files Browse the repository at this point in the history
fixes #14687
  • Loading branch information
dummdidumm authored Jan 8, 2025
1 parent a2565ef commit 9aae463
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-guests-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: silence false-positive stale value warning
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ function build_assignment(operator, left, right, context) {

// special case — ignore `bind:prop={getter, (v) => (...)}` / `bind:value={x.y}`
if (
path.at(-1) === 'BindDirective' ||
path.at(-1) === 'Component' ||
path.at(-1) === 'SvelteComponent' ||
(path.at(-1) === 'ArrowFunctionExpression' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
import { ok, test } from '../../test';

export default test({
compileOptions: {
dev: true
},

html: `<button>items: null</button> <div>x</div>`,
html: `<button>items: null</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2">`,

test({ assert, target, warnings }) {
const btn = target.querySelector('button');
ok(btn);

flushSync(() => btn?.click());
assert.htmlEqual(target.innerHTML, `<button>items: []</button> <div>x</div>`);
flushSync(() => btn.click());
assert.htmlEqual(
target.innerHTML,
`<button>items: []</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2">`
);

flushSync(() => btn?.click());
assert.htmlEqual(target.innerHTML, `<button>items: [0]</button> <div>x</div>`);
flushSync(() => btn.click());
assert.htmlEqual(
target.innerHTML,
`<button>items: [0]</button> <div>x</div> <input type="checkbox" value="1"><input type="checkbox" value="2">`
);

const input = target.querySelector('input');
ok(input);
input.checked = true;
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.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Test from './Test.svelte';
let entries = $state([]);
let object = $state({ items: null });
let object = $state({ items: null, group: [] });
</script>

<button onclick={() => (object.items ??= []).push(object.items.length)}>
Expand All @@ -11,6 +11,8 @@

<!-- these should not emit warnings -->
<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>

0 comments on commit 9aae463

Please sign in to comment.