Skip to content

Commit 682f4a6

Browse files
authored
fix: adjust scope parent for named slots (#10843)
fixes #10802
1 parent 117082b commit 682f4a6

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

.changeset/mighty-cooks-scream.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: adjust scope parent for named slots

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,9 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
400400
(attribute) => attribute.type === 'Attribute' && attribute.name === 'slot'
401401
)
402402
) {
403-
// <div slot="..."> inherits the scope above the component, because slots are hella weird
404-
scopes.set(child, state.scope);
405-
visit(child);
403+
// <div slot="..."> inherits the scope above the component unless the component is a named slot itself, because slots are hella weird
404+
scopes.set(child, is_default_slot ? state.scope : scope);
405+
visit(child, { scope: is_default_slot ? state.scope : scope });
406406
} else {
407407
if (child.type === 'ExpressionTag') {
408408
// expression tag is a special case — we don't visit it directly, but via process_children,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot name="inner" text="hello world" />
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
export let text;
3+
</script>
4+
5+
<div>
6+
{text}
7+
<hr />
8+
<slot name="footer" />
9+
</div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `
5+
<div>hello world <hr> <div slot="footer">hello world</div></div>
6+
`
7+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import Nested from "./Nested.svelte"
3+
import Nested2 from "./Nested2.svelte"
4+
</script>
5+
6+
<Nested>
7+
<Nested2 slot="inner" let:text {text}>
8+
<div slot="footer">
9+
{text}
10+
</div>
11+
</Nested2>
12+
</Nested>

0 commit comments

Comments
 (0)