From 682f4a651351d4c0c492be1806bfb119b6bb76a7 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:25:40 +0100 Subject: [PATCH] fix: adjust scope parent for named slots (#10843) fixes #10802 --- .changeset/mighty-cooks-scream.md | 5 +++++ packages/svelte/src/compiler/phases/scope.js | 6 +++--- .../samples/component-slot-let-scope-4/Nested.svelte | 1 + .../component-slot-let-scope-4/Nested2.svelte | 9 +++++++++ .../samples/component-slot-let-scope-4/_config.js | 7 +++++++ .../samples/component-slot-let-scope-4/main.svelte | 12 ++++++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 .changeset/mighty-cooks-scream.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested.svelte create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested2.svelte create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/main.svelte diff --git a/.changeset/mighty-cooks-scream.md b/.changeset/mighty-cooks-scream.md new file mode 100644 index 0000000000..3d3e662fce --- /dev/null +++ b/.changeset/mighty-cooks-scream.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: adjust scope parent for named slots diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index cecd52bda8..9b0d3e7dcb 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -400,9 +400,9 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) { (attribute) => attribute.type === 'Attribute' && attribute.name === 'slot' ) ) { - //
inherits the scope above the component, because slots are hella weird - scopes.set(child, state.scope); - visit(child); + //
inherits the scope above the component unless the component is a named slot itself, because slots are hella weird + scopes.set(child, is_default_slot ? state.scope : scope); + visit(child, { scope: is_default_slot ? state.scope : scope }); } else { if (child.type === 'ExpressionTag') { // expression tag is a special case — we don't visit it directly, but via process_children, diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested.svelte new file mode 100644 index 0000000000..5811f40c6a --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested.svelte @@ -0,0 +1 @@ + diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested2.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested2.svelte new file mode 100644 index 0000000000..f4575dc01c --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/Nested2.svelte @@ -0,0 +1,9 @@ + + +
+ {text} +
+ +
\ No newline at end of file diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/_config.js b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/_config.js new file mode 100644 index 0000000000..18dd268ada --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/_config.js @@ -0,0 +1,7 @@ +import { test } from '../../test'; + +export default test({ + html: ` +
hello world
hello world
+ ` +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/main.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/main.svelte new file mode 100644 index 0000000000..12d231dbbe --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-let-scope-4/main.svelte @@ -0,0 +1,12 @@ + + + + +
+ {text} +
+
+
\ No newline at end of file