Skip to content

Commit

Permalink
fix: improve event delegation with shadowed bindings (#10620)
Browse files Browse the repository at this point in the history
* fix: improve event delegation with shadowed bindings

* fix: improve event delegation with shadowed bindings
  • Loading branch information
trueadm authored Feb 23, 2024
1 parent 351d463 commit b4a70ea
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-pans-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve event delegation with shadowed bindings
6 changes: 6 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ function get_delegated_event(event_name, handler, context) {
return non_hoistable;
}
const binding = scope.get(reference);
const local_binding = context.state.scope.get(reference);

// If we are referencing a binding that is shadowed in another scope then bail out.
if (local_binding !== null && binding !== null && local_binding.node !== binding.node) {
return non_hoistable;
}

// If we have multiple references to the same store using $ prefix, bail out.
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test } from '../../test';
import { log } from './log.js';

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

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

btn?.click();
await Promise.resolve();
assert.deepEqual(log, ['method']);
}
});
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,12 @@
<script>
import { log } from './log.js';
let method = $state('method');
function submitPay() {
log.push(method);
}
let methods = [{method:1}];
</script>
{#each methods as {method}}
<button onclick={submitPay}>{method}</button>
{/each}

0 comments on commit b4a70ea

Please sign in to comment.