Skip to content

Commit

Permalink
fix: don't error on slot prop inside block inside other component
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
dummdidumm committed Jan 30, 2025
1 parent 8e83127 commit 45eebc7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-rocks-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: don't error on slot prop inside block inside other component
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,42 @@ export function validate_slot_attribute(context, attribute, is_component = false
}

if (owner) {
if (!is_text_attribute(attribute)) {
e.slot_attribute_invalid(attribute);
}

if (
owner.type === 'Component' ||
owner.type === 'SvelteComponent' ||
owner.type === 'SvelteSelf'
) {
if (owner !== parent) {
e.slot_attribute_invalid_placement(attribute);
}
if (!is_component) {
e.slot_attribute_invalid_placement(attribute);
}
} else {
if (!is_text_attribute(attribute)) {
e.slot_attribute_invalid(attribute);
}

const name = attribute.value[0].data;
const name = attribute.value[0].data;

if (context.state.component_slots.has(name)) {
e.slot_attribute_duplicate(attribute, name, owner.name);
}

context.state.component_slots.add(name);
if (context.state.component_slots.has(name)) {
e.slot_attribute_duplicate(attribute, name, owner.name);
}

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

if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
if (name === 'default') {
for (const node of owner.fragment.nodes) {
if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
continue;
}
}

e.slot_default_duplicate(node);
if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
continue;
}
}

e.slot_default_duplicate(node);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<Foo slot="foo">valid</Foo>
<Foo slot={foo}>valid</Foo>
<Foo>
{#if true}
<Foo slot="foo">valid</Foo>
<Foo slot={foo}>valid</Foo>
{/if}
</Foo>

0 comments on commit 45eebc7

Please sign in to comment.