Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-jars-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: correctly handle closure passed to $derived.by when destructuring
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export const javascript_visitors_runes = {
declarations.push(
b.declarator(
b.id(object_id),
b.call('$.derived', b.thunk(rune === '$derived.by' ? b.call(value) : value))
b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value))
)
);
declarations.push(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from '../../test';
import { log } from './log.js';

export default test({
before_test() {
log.length = 0;
},

html: `<button>0</button>`,

async test({ assert, target, window }) {
const btn = target.querySelector('button');
const clickEvent = new window.Event('click', { bubbles: true });
await btn?.dispatchEvent(clickEvent);

assert.htmlEqual(target.innerHTML, `<button>2</button>`);
assert.deepEqual(log, ['create_derived']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
import {log} from './log.js'

let count = $state(0);
function create_derived() {
log.push('create_derived');
return () => {
return {
get double() {
return count * 2;
}
}
}
}
let {double} = $derived.by(create_derived());
</script>

<button on:click={() => count++}>{double}</button>