Skip to content

Commit

Permalink
warn if passing slot not defined into Component
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Mar 3, 2020
1 parent addea43 commit 926ee8f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ export default function dom(
${unknown_props_check}
${component.slots.size ? b`let { $$slots = {}, $$scope } = $$props;` : null}
${component.slots.size || component.compile_options.dev ? b`let { $$slots = {}, $$scope } = $$props;` : null}
${component.compile_options.dev && b`@validate_slot($$slots, [${Array.from(component.slots.keys()).map(key => `"${key}"`).join(',')}]);`}
${renderer.binding_groups.length > 0 && b`const $$binding_groups = [${renderer.binding_groups.map(_ => x`[]`)}];`}
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/internal/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export function validate_each_argument(arg) {
}
}

export function validate_slot(slot, keys) {
keys = new Set(keys);
for (const slot_key of Object.keys(slot)) {
if (!keys.has(slot_key)) {
console.warn(`Received unexpected slot named "${slot_key}"`);
}
}
}

type Props = Record<string, any>;
export interface SvelteComponentDev {
Expand Down
1 change: 1 addition & 0 deletions test/runtime/samples/component-slot-warning/Nested.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot name="slot2"></slot>
9 changes: 9 additions & 0 deletions test/runtime/samples/component-slot-warning/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
compileOptions: {
dev: true
},
warnings: [
'Received unexpected slot named "default"',
'Received unexpected slot named "slot1"'
]
};
7 changes: 7 additions & 0 deletions test/runtime/samples/component-slot-warning/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Nested from "./Nested.svelte";
</script>
<Nested>
<input slot="slot1">
<input slot="slot2">
</Nested>

0 comments on commit 926ee8f

Please sign in to comment.