From 926ee8fa1d3e0ce4577f7e0bfdf9fb192db1d2a2 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Tue, 3 Mar 2020 15:50:39 +0800 Subject: [PATCH] warn if passing slot not defined into Component --- src/compiler/compile/render_dom/index.ts | 3 ++- src/runtime/internal/dev.ts | 8 ++++++++ .../runtime/samples/component-slot-warning/Nested.svelte | 1 + test/runtime/samples/component-slot-warning/_config.js | 9 +++++++++ test/runtime/samples/component-slot-warning/main.svelte | 7 +++++++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/component-slot-warning/Nested.svelte create mode 100644 test/runtime/samples/component-slot-warning/_config.js create mode 100644 test/runtime/samples/component-slot-warning/main.svelte diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index d6da6142768c..179115b5b248 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -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`[]`)}];`} diff --git a/src/runtime/internal/dev.ts b/src/runtime/internal/dev.ts index 8aefc88ba7b0..865f23ce1165 100644 --- a/src/runtime/internal/dev.ts +++ b/src/runtime/internal/dev.ts @@ -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; export interface SvelteComponentDev { diff --git a/test/runtime/samples/component-slot-warning/Nested.svelte b/test/runtime/samples/component-slot-warning/Nested.svelte new file mode 100644 index 000000000000..c6f086d96c19 --- /dev/null +++ b/test/runtime/samples/component-slot-warning/Nested.svelte @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/runtime/samples/component-slot-warning/_config.js b/test/runtime/samples/component-slot-warning/_config.js new file mode 100644 index 000000000000..e652d8e4fd17 --- /dev/null +++ b/test/runtime/samples/component-slot-warning/_config.js @@ -0,0 +1,9 @@ +export default { + compileOptions: { + dev: true + }, + warnings: [ + 'Received unexpected slot named "default"', + 'Received unexpected slot named "slot1"' + ] +}; diff --git a/test/runtime/samples/component-slot-warning/main.svelte b/test/runtime/samples/component-slot-warning/main.svelte new file mode 100644 index 000000000000..c29ef3e85be2 --- /dev/null +++ b/test/runtime/samples/component-slot-warning/main.svelte @@ -0,0 +1,7 @@ + + + + +