fix: assign correct scope to attributes of named slot (#12476)

fixes #12213
pull/12478/head
Simon H 5 months ago committed by GitHub
parent 0c15a7f98e
commit 4fd6d29227
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: assign correct scope to attributes of named slot

@ -386,16 +386,20 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
Component(node, { state, visit, path }) { Component(node, { state, visit, path }) {
state.scope.reference(b.id(node.name), path); state.scope.reference(b.id(node.name), path);
for (const attribute of node.attributes) {
visit(attribute);
}
// let:x is super weird: // let:x is super weird:
// - for the default slot, its scope only applies to children that are not slots themselves // - for the default slot, its scope only applies to children that are not slots themselves
// - for named slots, its scope applies to the component itself, too // - for named slots, its scope applies to the component itself, too
const [scope, is_default_slot] = analyze_let_directives(node, state.scope); const [scope, is_default_slot] = analyze_let_directives(node, state.scope);
if (!is_default_slot) { if (is_default_slot) {
for (const attribute of node.attributes) {
visit(attribute);
}
} else {
scopes.set(node, scope); scopes.set(node, scope);
for (const attribute of node.attributes) {
visit(attribute, { ...state, scope });
}
} }
for (const child of node.fragment.nodes) { for (const child of node.fragment.nodes) {

@ -0,0 +1,7 @@
<script lang="ts">
export let onclick;
</script>
<button {onclick}>
<slot />
</button>

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

@ -0,0 +1,8 @@
<script lang="ts">
import Parent from './Parent.svelte';
import Child from './Child.svelte';
</script>
<Parent>
<Child slot="item" let:item onclick={() => console.log(item)}>asd</Child>
</Parent>
Loading…
Cancel
Save