Skip to content

Commit d2a7f54

Browse files
committed
fix: skip certain slot validations for custom elements
fixes #10196
1 parent c7a7725 commit d2a7f54

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

.changeset/blue-timers-film.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: skip certain slot validations for custom elements

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,31 +172,37 @@ function validate_slot_attribute(context, attribute) {
172172
error(attribute, 'invalid-slot-attribute');
173173
}
174174

175-
if (owner.type === 'Component' || owner.type === 'SvelteComponent') {
175+
if (
176+
owner.type === 'Component' ||
177+
owner.type === 'SvelteComponent' ||
178+
owner.type === 'SvelteSelf'
179+
) {
176180
if (owner !== context.path.at(-2)) {
177181
error(attribute, 'invalid-slot-placement');
178182
}
179-
}
180183

181-
const name = attribute.value[0].data;
182-
if (context.state.component_slots.has(name)) {
183-
error(attribute, 'duplicate-slot-name', name, owner.name);
184-
}
185-
context.state.component_slots.add(name);
184+
const name = attribute.value[0].data;
186185

187-
if (name === 'default') {
188-
for (const node of owner.fragment.nodes) {
189-
if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
190-
continue;
191-
}
186+
if (context.state.component_slots.has(name)) {
187+
error(attribute, 'duplicate-slot-name', name, owner.name);
188+
}
192189

193-
if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
194-
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
190+
context.state.component_slots.add(name);
191+
192+
if (name === 'default') {
193+
for (const node of owner.fragment.nodes) {
194+
if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
195195
continue;
196196
}
197-
}
198197

199-
error(node, 'invalid-default-slot-content');
198+
if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
199+
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
200+
continue;
201+
}
202+
}
203+
204+
error(node, 'invalid-default-slot-content');
205+
}
200206
}
201207
}
202208
} else {

packages/svelte/tests/compiler-errors/samples/component-slot-duplicate-error/main.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import Nested from './irrelevant';
33
</script>
44

5+
<!-- allowed in custom elements -->
6+
<c-e>
7+
<c-e-item slot="allowed"></c-e-item>
8+
<c-e-item slot="allowed"></c-e-item>
9+
</c-e>
10+
511
<Nested>
612
<p slot="foo">{value}</p>
713
<p slot="foo">{value}</p>

0 commit comments

Comments
 (0)