Skip to content

Commit 4fd6d29

Browse files
authored
fix: assign correct scope to attributes of named slot (#12476)
fixes #12213
1 parent 0c15a7f commit 4fd6d29

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

.changeset/cold-shrimps-hug.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: assign correct scope to attributes of named slot

packages/svelte/src/compiler/phases/scope.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,20 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
386386
Component(node, { state, visit, path }) {
387387
state.scope.reference(b.id(node.name), path);
388388

389-
for (const attribute of node.attributes) {
390-
visit(attribute);
391-
}
392-
393389
// let:x is super weird:
394390
// - for the default slot, its scope only applies to children that are not slots themselves
395391
// - for named slots, its scope applies to the component itself, too
396392
const [scope, is_default_slot] = analyze_let_directives(node, state.scope);
397-
if (!is_default_slot) {
393+
if (is_default_slot) {
394+
for (const attribute of node.attributes) {
395+
visit(attribute);
396+
}
397+
} else {
398398
scopes.set(node, scope);
399+
400+
for (const attribute of node.attributes) {
401+
visit(attribute, { ...state, scope });
402+
}
399403
}
400404

401405
for (const child of node.fragment.nodes) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
export let onclick;
3+
</script>
4+
5+
<button {onclick}>
6+
<slot />
7+
</button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot name="item" item={1} />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, logs, target }) {
6+
const btn = target.querySelector('button');
7+
8+
btn?.click();
9+
flushSync();
10+
assert.deepEqual(logs, [1]);
11+
}
12+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
import Parent from './Parent.svelte';
3+
import Child from './Child.svelte';
4+
</script>
5+
6+
<Parent>
7+
<Child slot="item" let:item onclick={() => console.log(item)}>asd</Child>
8+
</Parent>

0 commit comments

Comments
 (0)