Skip to content

Commit faa5f74

Browse files
committed
fix: correctly migrate slots with static props
1 parent d6bad46 commit faa5f74

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

.changeset/nine-kids-whisper.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'svelte': patch
33
---
44

5-
fix: correctly migrate `$$slots` with bracket member expressions
5+
fix: correctly migrate `$$slots` with bracket member expressions & slots with static props

packages/svelte/src/compiler/migrate/index.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ const template = {
673673
handle_events(node, state);
674674
next();
675675
},
676-
SlotElement(node, { state, next }) {
676+
SlotElement(node, { state, next, visit }) {
677677
if (state.analysis.custom_element) return;
678678
let name = 'children';
679679
let slot_name = 'default';
@@ -688,13 +688,22 @@ const template = {
688688
} else {
689689
const attr_value =
690690
attr.value === true || Array.isArray(attr.value) ? attr.value : [attr.value];
691-
const value =
692-
attr_value !== true
693-
? state.str.original.substring(
694-
attr_value[0].start,
695-
attr_value[attr_value.length - 1].end
696-
)
697-
: 'true';
691+
let value = 'true';
692+
if (attr_value !== true) {
693+
const first = attr_value[0];
694+
const last = attr_value[attr_value.length - 1];
695+
for (const attr of attr_value) {
696+
visit(attr);
697+
}
698+
value = state.str
699+
.snip(
700+
first.type === 'Text'
701+
? first.start - 1
702+
: /** @type {number} */ (first.expression.start),
703+
last.type === 'Text' ? last.end + 1 : /** @type {number} */ (last.expression.end)
704+
)
705+
.toString();
706+
}
698707
slot_props += value === attr.name ? `${value}, ` : `${attr.name}: ${value}, `;
699708
}
700709
}

packages/svelte/tests/migrate/samples/slots/input.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
{#if $$slots['dashed-name']}foo{/if}
1717

1818
<slot name="dashed-name" />
19+
20+
<slot header="something" title={my_title} {id} />

packages/svelte/tests/migrate/samples/slots/output.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@
2525

2626
{#if dashed_name}foo{/if}
2727

28-
{@render dashed_name?.()}
28+
{@render dashed_name?.()}
29+
30+
{@render children?.({ header: "something", title: my_title, id, })}

0 commit comments

Comments
 (0)