Skip to content

fix: delegate functions with shadowed variables if declared locally #16417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
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/fast-parrots-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: delegate functions with shadowed variables if declared locally
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,13 @@ function get_delegated_event(event_name, handler, context) {
return unhoisted;
}

// 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) {
// If we are referencing a binding that is shadowed in another scope then bail out (unless it's declared within the function).
if (
local_binding !== null &&
binding !== null &&
local_binding.node !== binding.node &&
scope.declarations.get(reference) !== binding
) {
return unhoisted;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var on_click = (e) => {
const index = Number(e.currentTarget.dataset.index);

console.log(index);
};

var root_1 = $.from_html(`<button type="button">B</button>`);

export default function Delegated_locally_declared_shadowed($$anchor) {
var fragment = $.comment();
var node = $.first_child(fragment);

$.each(node, 0, () => ({ length: 1 }), $.index, ($$anchor, $$item, index) => {
var button = root_1();

$.set_attribute(button, 'data-index', index);
button.__click = [on_click];
$.append($$anchor, button);
});

$.append($$anchor, fragment);
}

$.delegate(['click']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as $ from 'svelte/internal/server';

export default function Delegated_locally_declared_shadowed($$payload) {
const each_array = $.ensure_array_like({ length: 1 });

$$payload.out += `<!--[-->`;

for (let index = 0, $$length = each_array.length; index < $$length; index++) {
$$payload.out += `<button type="button"${$.attr('data-index', index)}>B</button>`;
}

$$payload.out += `<!--]-->`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts"></script>

{#each { length: 1 }, index}
<button
type="button"
data-index={index}
onclick={(e) => {
const index = Number(e.currentTarget.dataset.index)!;
console.log(index);
}}>B</button
>
{/each}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy