Skip to content

Commit 83f00eb

Browse files
authored
fix: don't error on slot prop inside block inside other component (#15148)
`slot` is treated as a regular prop if it is not used directly inside another component, but we were running validations on such regular props that should only be run on real slots. Fixes #15125
1 parent b8607f8 commit 83f00eb

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

.changeset/good-rocks-talk.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: don't error on slot prop inside block inside other component

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/attribute.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,42 @@ export function validate_slot_attribute(context, attribute, is_component = false
8080
}
8181

8282
if (owner) {
83-
if (!is_text_attribute(attribute)) {
84-
e.slot_attribute_invalid(attribute);
85-
}
86-
8783
if (
8884
owner.type === 'Component' ||
8985
owner.type === 'SvelteComponent' ||
9086
owner.type === 'SvelteSelf'
9187
) {
9288
if (owner !== parent) {
93-
e.slot_attribute_invalid_placement(attribute);
94-
}
89+
if (!is_component) {
90+
e.slot_attribute_invalid_placement(attribute);
91+
}
92+
} else {
93+
if (!is_text_attribute(attribute)) {
94+
e.slot_attribute_invalid(attribute);
95+
}
9596

96-
const name = attribute.value[0].data;
97+
const name = attribute.value[0].data;
9798

98-
if (context.state.component_slots.has(name)) {
99-
e.slot_attribute_duplicate(attribute, name, owner.name);
100-
}
101-
102-
context.state.component_slots.add(name);
99+
if (context.state.component_slots.has(name)) {
100+
e.slot_attribute_duplicate(attribute, name, owner.name);
101+
}
103102

104-
if (name === 'default') {
105-
for (const node of owner.fragment.nodes) {
106-
if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
107-
continue;
108-
}
103+
context.state.component_slots.add(name);
109104

110-
if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
111-
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
105+
if (name === 'default') {
106+
for (const node of owner.fragment.nodes) {
107+
if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
112108
continue;
113109
}
114-
}
115110

116-
e.slot_default_duplicate(node);
111+
if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
112+
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
113+
continue;
114+
}
115+
}
116+
117+
e.slot_default_duplicate(node);
118+
}
117119
}
118120
}
119121
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
<Foo slot="foo">valid</Foo>
22
<Foo slot={foo}>valid</Foo>
3+
<Foo>
4+
{#if true}
5+
<Foo slot="foo">valid</Foo>
6+
<Foo slot={foo}>valid</Foo>
7+
{/if}
8+
</Foo>

0 commit comments

Comments
 (0)