All files / src/internal/shared validate.js

100% Statements 58/58
100% Branches 11/11
100% Functions 5/5
100% Lines 57/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 582x 2x 2x 2x 2x 2x 2x 2x 19x 19x 19x 2x 2x 2x 2x 2x 2x 19x 3x 3x 3x 3x 3x 16x 16x 2x 2x 2x 2x 2x 2x 66x 3x 3x 63x 63x 2x 2x 2x 2x 2x 2x 19x 19x 16x 16x 16x 19x 2x 2x 2x 21x 21x 21x 2x 2x 21x  
import { is_void } from '../../compiler/phases/1-parse/utils/names.js';
 
const snippet_symbol = Symbol.for('svelte.snippet');
 
/**
 * @param {any} fn
 */
export function add_snippet_symbol(fn) {
	fn[snippet_symbol] = true;
	return fn;
}
 
/**
 * Validate that the function handed to `{@render ...}` is a snippet function, and not some other kind of function.
 * @param {any} snippet_fn
 */
export function validate_snippet(snippet_fn) {
	if (snippet_fn && snippet_fn[snippet_symbol] !== true) {
		throw new Error(
			'The argument to `{@render ...}` must be a snippet function, not a component or some other kind of function. ' +
				'If you want to dynamically render one snippet or another, use `$derived` and pass its result to `{@render ...}`.'
		);
	}
	return snippet_fn;
}
 
/**
 * Validate that the function behind `<Component />` isn't a snippet.
 * @param {any} component_fn
 */
export function validate_component(component_fn) {
	if (component_fn?.[snippet_symbol] === true) {
		throw new Error('A snippet must be rendered with `{@render ...}`');
	}
	return component_fn;
}
 
/**
 * @param {() => string} tag_fn
 * @returns {void}
 */
export function validate_void_dynamic_element(tag_fn) {
	const tag = tag_fn();
	if (tag && is_void(tag)) {
		// eslint-disable-next-line no-console
		console.warn(`<svelte:element this="${tag}"> is self-closing and cannot have content.`);
	}
}
 
/** @param {() => unknown} tag_fn */
export function validate_dynamic_element_tag(tag_fn) {
	const tag = tag_fn();
	const is_string = typeof tag === 'string';
	if (tag && !is_string) {
		throw new Error('<svelte:element> expects "this" attribute to be a string.');
	}
}