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

pull/16418/head
Paolo Ricciuti 2 months ago committed by GitHub
parent f343170927
commit bdf6adb411
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: delegate functions with shadowed variables if declared locally

@ -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;
}

@ -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']);

@ -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 += `<!--]-->`;
}

@ -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…
Cancel
Save