Skip to content

Commit bdf6adb

Browse files
fix: delegate functions with shadowed variables if declared locally (#16417)
1 parent f343170 commit bdf6adb

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

.changeset/fast-parrots-draw.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: delegate functions with shadowed variables if declared locally

packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ function get_delegated_event(event_name, handler, context) {
192192
return unhoisted;
193193
}
194194

195-
// If we are referencing a binding that is shadowed in another scope then bail out.
196-
if (local_binding !== null && binding !== null && local_binding.node !== binding.node) {
195+
// If we are referencing a binding that is shadowed in another scope then bail out (unless it's declared within the function).
196+
if (
197+
local_binding !== null &&
198+
binding !== null &&
199+
local_binding.node !== binding.node &&
200+
scope.declarations.get(reference) !== binding
201+
) {
197202
return unhoisted;
198203
}
199204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'svelte/internal/disclose-version';
2+
import 'svelte/internal/flags/legacy';
3+
import * as $ from 'svelte/internal/client';
4+
5+
var on_click = (e) => {
6+
const index = Number(e.currentTarget.dataset.index);
7+
8+
console.log(index);
9+
};
10+
11+
var root_1 = $.from_html(`<button type="button">B</button>`);
12+
13+
export default function Delegated_locally_declared_shadowed($$anchor) {
14+
var fragment = $.comment();
15+
var node = $.first_child(fragment);
16+
17+
$.each(node, 0, () => ({ length: 1 }), $.index, ($$anchor, $$item, index) => {
18+
var button = root_1();
19+
20+
$.set_attribute(button, 'data-index', index);
21+
button.__click = [on_click];
22+
$.append($$anchor, button);
23+
});
24+
25+
$.append($$anchor, fragment);
26+
}
27+
28+
$.delegate(['click']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as $ from 'svelte/internal/server';
2+
3+
export default function Delegated_locally_declared_shadowed($$payload) {
4+
const each_array = $.ensure_array_like({ length: 1 });
5+
6+
$$payload.out += `<!--[-->`;
7+
8+
for (let index = 0, $$length = each_array.length; index < $$length; index++) {
9+
$$payload.out += `<button type="button"${$.attr('data-index', index)}>B</button>`;
10+
}
11+
12+
$$payload.out += `<!--]-->`;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts"></script>
2+
3+
{#each { length: 1 }, index}
4+
<button
5+
type="button"
6+
data-index={index}
7+
onclick={(e) => {
8+
const index = Number(e.currentTarget.dataset.index)!;
9+
console.log(index);
10+
}}>B</button
11+
>
12+
{/each}

0 commit comments

Comments
 (0)