improve validation

pull/7551/head
Yuichiro Yamashita 4 years ago
parent d727a0d6e5
commit 7d8d7bac15

@ -416,8 +416,7 @@ export default class InlineComponentWrapper extends Wrapper {
}
if (${switch_value}) {
${component.compile_options.dev && b`@validate_svelte_component(${switch_value});`}
${name} = new ${switch_value}(${switch_props}(#ctx));
${name} = @construct_svelte_component(${switch_value}, ${switch_props}(#ctx));
${munged_bindings}
${munged_handlers}
@ -461,8 +460,7 @@ export default class InlineComponentWrapper extends Wrapper {
}
if (${switch_value}) {
${component.compile_options.dev && b`@validate_svelte_component(${switch_value});`}
${name} = new ${switch_value}(${switch_props}(#ctx));
${name} = @construct_svelte_component(${switch_value}, ${switch_props}(#ctx));
${munged_bindings}
${munged_handlers}

@ -121,9 +121,21 @@ export function validate_void_dynamic_element(tag: undefined | string) {
}
}
export function validate_svelte_component(component: any) {
if (!(component.prototype instanceof SvelteComponent)) {
throw new Error('this={...} of <svelte:component> should specify a Svelte component.');
export function construct_svelte_component_dev(component, props) {
const error_message = 'this={...} of <svelte:component> should specify a Svelte component.';
try {
const instance = new component(props);
if (!instance.$$ || !instance.$set || !instance.$on || !instance.$destroy) {
throw new Error(error_message);
}
return instance;
} catch (err) {
const { message } = err;
if (typeof message === 'string' && message.indexOf('is not a constructor') !== -1) {
throw new Error(error_message);
} else {
throw err;
}
}
}

@ -740,3 +740,7 @@ export function get_custom_elements_slots(element: HTMLElement) {
});
return result;
}
export function construct_svelte_component(component, props) {
return new component(props);
}

Loading…
Cancel
Save