Skip to content

Commit 31d6ba9

Browse files
committed
fix: ensure migrate correctly handles named slots
1 parent c9d85c2 commit 31d6ba9

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

.changeset/silver-beans-hear.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: ensure migrate correctly handles named slots

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,10 @@ const template = {
996996
if (attr.type === 'SpreadAttribute') {
997997
slot_props += `...${state.str.original.substring(/** @type {number} */ (attr.expression.start), attr.expression.end)}, `;
998998
} else if (attr.type === 'Attribute') {
999+
if (attr.name === 'slot') {
1000+
continue;
1001+
}
1002+
9991003
if (attr.name === 'name') {
10001004
slot_name = /** @type {any} */ (attr.value)[0].data;
10011005
} else {
@@ -1189,18 +1193,17 @@ function migrate_slot_usage(node, path, state) {
11891193
);
11901194
}
11911195
} else {
1196+
const prepend_string = `{#snippet ${snippet_name}(${props})}\n${state.indent.repeat(path.length - 2)}`;
11921197
// Named slot or `svelte:fragment`: wrap element itself in a snippet
1193-
state.str.prependLeft(
1194-
node.start,
1195-
`{#snippet ${snippet_name}(${props})}\n${state.indent.repeat(path.length - 2)}`
1196-
);
1198+
state.str.prependLeft(node.start, prepend_string);
11971199
state.str.indent(state.indent, {
11981200
exclude: [
11991201
[0, node.start],
12001202
[node.end, state.str.original.length]
12011203
]
12021204
});
1203-
state.str.appendLeft(node.end, `\n${state.indent.repeat(path.length - 2)}{/snippet}`);
1205+
const append_string = `\n${state.indent.repeat(path.length - 2)}{/snippet}`;
1206+
state.str.appendLeft(node.end, append_string);
12041207
}
12051208
}
12061209

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
import Component from './Component.svelte';
3+
</script>
4+
5+
<Component>
6+
<slot slot="msg"></slot>
7+
</Component>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
import Component from './Component.svelte';
3+
interface Props {
4+
children?: import('svelte').Snippet;
5+
}
6+
7+
let { children }: Props = $props();
8+
</script>
9+
10+
<Component>
11+
{#snippet msg()}
12+
{@render children?.()}
13+
</Component>

0 commit comments

Comments
 (0)