From 4fd6d292274a91e4a3b7acdd6e62451780d3ceb4 Mon Sep 17 00:00:00 2001
From: Simon H <5968653+dummdidumm@users.noreply.github.com>
Date: Wed, 17 Jul 2024 16:07:31 +0200
Subject: [PATCH] fix: assign correct scope to attributes of named slot
(#12476)
fixes #12213
---
.changeset/cold-shrimps-hug.md | 5 +++++
packages/svelte/src/compiler/phases/scope.js | 14 +++++++++-----
.../component-slot-named-scope/Child.svelte | 7 +++++++
.../component-slot-named-scope/Parent.svelte | 1 +
.../samples/component-slot-named-scope/_config.js | 12 ++++++++++++
.../samples/component-slot-named-scope/main.svelte | 8 ++++++++
6 files changed, 42 insertions(+), 5 deletions(-)
create mode 100644 .changeset/cold-shrimps-hug.md
create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Child.svelte
create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Parent.svelte
create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/_config.js
create mode 100644 packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/main.svelte
diff --git a/.changeset/cold-shrimps-hug.md b/.changeset/cold-shrimps-hug.md
new file mode 100644
index 0000000000..be5dc91d2f
--- /dev/null
+++ b/.changeset/cold-shrimps-hug.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: assign correct scope to attributes of named slot
diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js
index 7b23486d9a..f9f208d6ae 100644
--- a/packages/svelte/src/compiler/phases/scope.js
+++ b/packages/svelte/src/compiler/phases/scope.js
@@ -386,16 +386,20 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
Component(node, { state, visit, path }) {
state.scope.reference(b.id(node.name), path);
- for (const attribute of node.attributes) {
- visit(attribute);
- }
-
// let:x is super weird:
// - 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
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);
+
+ for (const attribute of node.attributes) {
+ visit(attribute, { ...state, scope });
+ }
}
for (const child of node.fragment.nodes) {
diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Child.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Child.svelte
new file mode 100644
index 0000000000..c46c742315
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Child.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Parent.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Parent.svelte
new file mode 100644
index 0000000000..1d9ecbcc2d
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/Parent.svelte
@@ -0,0 +1 @@
+
diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/_config.js b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/_config.js
new file mode 100644
index 0000000000..f11eb14e71
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/_config.js
@@ -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]);
+ }
+});
diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/main.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/main.svelte
new file mode 100644
index 0000000000..d0a38e4d08
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-named-scope/main.svelte
@@ -0,0 +1,8 @@
+
+
+
+ console.log(item)}>asd
+