pull/14273/head
Rich Harris 2 years ago
parent 25cd6f3c76
commit 2aa2f57834

@ -83,6 +83,10 @@ function build_assignment(operator, left, right, context) {
const path = context.path.map((node) => node.type); const path = context.path.map((node) => node.type);
const is_primitive = path.at(-1) === 'BindDirective' && path.at(-2) === 'RegularElement'; const is_primitive = path.at(-1) === 'BindDirective' && path.at(-2) === 'RegularElement';
let value = /** @type {Expression} */ (
context.visit(build_assignment_value(operator, left, right))
);
if ( if (
!is_primitive && !is_primitive &&
binding.kind !== 'prop' && binding.kind !== 'prop' &&
@ -93,13 +97,9 @@ function build_assignment(operator, left, right, context) {
// other operators result in coercion // other operators result in coercion
['=', '||=', '&&=', '??='].includes(operator) ['=', '||=', '&&=', '??='].includes(operator)
) { ) {
right = build_proxy_reassignment(right, object); value = build_proxy_reassignment(value, object);
} }
let value = /** @type {Expression} */ (
context.visit(build_assignment_value(operator, left, right))
);
return transform.assign(object, value); return transform.assign(object, value);
} }

@ -2,24 +2,15 @@ import { flushSync } from 'svelte';
import { test } from '../../test'; import { test } from '../../test';
export default test({ export default test({
html: `<button>items: null</button><button>items: null</button>`, html: `<button>items: null</button>`,
test({ assert, target }) { test({ assert, target }) {
const [btn1, btn2] = target.querySelectorAll('button'); const [btn1, btn2] = target.querySelectorAll('button');
flushSync(() => btn1.click()); flushSync(() => btn1.click());
assert.htmlEqual(target.innerHTML, `<button>items: [0]</button><button>items: null</button>`); assert.htmlEqual(target.innerHTML, `<button>items: [0]</button>`);
flushSync(() => btn1.click()); flushSync(() => btn1.click());
assert.htmlEqual(target.innerHTML, `<button>items: [0,1]</button><button>items: null</button>`); assert.htmlEqual(target.innerHTML, `<button>items: [0,1]</button>`);
flushSync(() => btn2.click());
assert.htmlEqual(target.innerHTML, `<button>items: [0,1]</button><button>items: [0]</button>`);
flushSync(() => btn2.click());
assert.htmlEqual(
target.innerHTML,
`<button>items: [0,1]</button><button>items: [0,1]</button>`
);
} }
}); });

@ -1,25 +1,7 @@
<script> <script>
let items = $state(null); let items = $state(null);
class Foo {
#items = $state(null);
get items() {
return this.#items;
}
add() {
(this.#items ??= []).push(this.#items.length);
}
}
const foo = new Foo();
</script> </script>
<button onclick={() => (items ??= []).push(items.length)}> <button onclick={() => (items ??= []).push(items.length)}>
items: {JSON.stringify(items)} items: {JSON.stringify(items)}
</button> </button>
<button onclick={() => foo.add()}>
items: {JSON.stringify(foo.items)}
</button>

Loading…
Cancel
Save