fix: don't hoist listeners that access non hoistable snippets ()

* fix: don't hoist listeners that access non hoistable snippets

* chore: add comment

* chore: fix auto import fumble
pull/15536/head
Paolo Ricciuti 2 weeks ago committed by GitHub
parent 74917ae703
commit 5b9f0df8ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't hoist listeners that access non hoistable snippets

@ -183,6 +183,15 @@ function get_delegated_event(event_name, handler, context) {
const binding = scope.get(reference);
const local_binding = context.state.scope.get(reference);
// if the function access a snippet that can't be hoisted we bail out
if (
local_binding !== null &&
local_binding.initial?.type === 'SnippetBlock' &&
!local_binding.initial.metadata.can_hoist
) {
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) {
return unhoisted;

@ -0,0 +1,12 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
async test({ assert, target, errors }) {
const button = target.querySelector('button');
flushSync(() => {
button?.click();
});
assert.deepEqual(errors, []);
}
});

@ -0,0 +1,12 @@
<script>
const log = () => {
console.log(snip);
}
let x = $state(0);
</script>
<button onclick={log}></button>
{#snippet snip()}
snippet {x}
{/snippet}
Loading…
Cancel
Save