Skip to content

Commit 720272e

Browse files
committed
fix: correctly reference destructured derived binding in event handler
1 parent 6145be5 commit 720272e

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

.changeset/strong-pans-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: correctly reference destructured derived binding in event handler

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,16 @@ function get_hoistable_params(node, context) {
523523
// We need both the subscription for getting the value and the store for updating
524524
params.push(b.id(binding.node.name.slice(1)));
525525
params.push(b.id(binding.node.name));
526+
} else if (
527+
// If it's a destructured derived binding, then we can extract the derived signal reference and use that.
528+
binding.expression !== null &&
529+
binding.expression.type === 'MemberExpression' &&
530+
binding.expression.object.type === 'CallExpression' &&
531+
binding.expression.object.callee.type === 'Identifier' &&
532+
binding.expression.object.callee.name === '$.get' &&
533+
binding.expression.object.arguments[0].type === 'Identifier'
534+
) {
535+
params.push(b.id(binding.expression.object.arguments[0].name));
526536
} else if (
527537
// If we are referencing a simple $$props value, then we need to reference the object property instead
528538
binding.kind === 'prop' &&
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test } from '../../test';
2+
import { log } from './log.js';
3+
4+
export default test({
5+
before_test() {
6+
log.length = 0;
7+
},
8+
9+
async test({ assert, target, window }) {
10+
const btn = target.querySelector('button');
11+
await btn?.click();
12+
13+
assert.deepEqual(log, ['works!']);
14+
}
15+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @type {any[]} */
2+
export const log = [];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import { log } from './log.js';
3+
4+
let structured = $state({
5+
handler() {
6+
log.push('works!')
7+
}
8+
});
9+
</script>
10+
11+
{#if structured}
12+
{@const { handler } = structured}
13+
14+
<button onclick={() => handler()}>click me</button>
15+
{/if}

0 commit comments

Comments
 (0)