Skip to content

Commit 481df0e

Browse files
authored
fix: correctly apply scope on component children (#9824)
Co-authored-by: Rich Harris <[email protected]>
1 parent 5797bb3 commit 481df0e

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

.changeset/unlucky-boxes-obey.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: correctly apply scope on component children

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
370370
}
371371

372372
const scope = analyze_let_directives(node, state.scope);
373-
scopes.set(node, scope);
374373

375374
for (const child of node.fragment.nodes) {
376375
if (
@@ -382,9 +381,15 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
382381
// <div slot="..."> inherits the scope above the component, because slots are hella weird
383382
scopes.set(child, state.scope);
384383
visit(child);
385-
} else if (child.type === 'SnippetBlock') {
386-
visit(child, { scope });
387384
} else {
385+
if (child.type === 'ExpressionTag') {
386+
// expression tag is a special case — we don't visit it directly, but via process_children,
387+
// so we need to set the scope on the expression rather than the tag itself
388+
scopes.set(child.expression, scope);
389+
} else {
390+
scopes.set(child, scope);
391+
}
392+
388393
visit(child, { scope });
389394
}
390395
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { message } = $props();
3+
</script>
4+
5+
<slot {message} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<p>message: hello</p>`
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import Child from './Child.svelte';
3+
4+
let message = 'hello';
5+
</script>
6+
7+
<Child message={message} let:message>
8+
<p>message: {message}</p>
9+
</Child>

0 commit comments

Comments
 (0)