Skip to content

Commit 926ee8f

Browse files
committed
warn if passing slot not defined into Component
1 parent addea43 commit 926ee8f

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

src/compiler/compile/render_dom/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ export default function dom(
412412
413413
${unknown_props_check}
414414
415-
${component.slots.size ? b`let { $$slots = {}, $$scope } = $$props;` : null}
415+
${component.slots.size || component.compile_options.dev ? b`let { $$slots = {}, $$scope } = $$props;` : null}
416+
${component.compile_options.dev && b`@validate_slot($$slots, [${Array.from(component.slots.keys()).map(key => `"${key}"`).join(',')}]);`}
416417
417418
${renderer.binding_groups.length > 0 && b`const $$binding_groups = [${renderer.binding_groups.map(_ => x`[]`)}];`}
418419

src/runtime/internal/dev.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ export function validate_each_argument(arg) {
8989
}
9090
}
9191

92+
export function validate_slot(slot, keys) {
93+
keys = new Set(keys);
94+
for (const slot_key of Object.keys(slot)) {
95+
if (!keys.has(slot_key)) {
96+
console.warn(`Received unexpected slot named "${slot_key}"`);
97+
}
98+
}
99+
}
92100

93101
type Props = Record<string, any>;
94102
export interface SvelteComponentDev {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot name="slot2"></slot>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
compileOptions: {
3+
dev: true
4+
},
5+
warnings: [
6+
'Received unexpected slot named "default"',
7+
'Received unexpected slot named "slot1"'
8+
]
9+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Nested from "./Nested.svelte";
3+
</script>
4+
<Nested>
5+
<input slot="slot1">
6+
<input slot="slot2">
7+
</Nested>

0 commit comments

Comments
 (0)