Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-timers-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: skip certain slot validations for custom elements
38 changes: 22 additions & 16 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,37 @@ function validate_slot_attribute(context, attribute) {
error(attribute, 'invalid-slot-attribute');
}

if (owner.type === 'Component' || owner.type === 'SvelteComponent') {
if (
owner.type === 'Component' ||
owner.type === 'SvelteComponent' ||
owner.type === 'SvelteSelf'
) {
if (owner !== context.path.at(-2)) {
error(attribute, 'invalid-slot-placement');
}
}

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

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

if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
context.state.component_slots.add(name);

if (name === 'default') {
for (const node of owner.fragment.nodes) {
if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
continue;
}
}

error(node, 'invalid-default-slot-content');
if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
continue;
}
}

error(node, 'invalid-default-slot-content');
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import Nested from './irrelevant';
</script>

<!-- allowed in custom elements -->
<c-e>
<c-e-item slot="allowed"></c-e-item>
<c-e-item slot="allowed"></c-e-item>
</c-e>

<Nested>
<p slot="foo">{value}</p>
<p slot="foo">{value}</p>
Expand Down