All files / src/internal/shared validate.js

100% Statements 77/77
100% Branches 17/17
100% Functions 6/6
100% Lines 74/74

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 752x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 79x 79x 79x 2x 2x 2x 2x 2x 2x 2x 45x 45x 42x 45x 6x 6x 39x 39x 39x 2x 2x 2x 2x 2x 2x 3312x 5x 5x 3307x 3307x 3307x 2x 2x 2x 2x 2x 2x 29x 29x 16x 16x 29x 2x 2x 2x 31x 31x 31x 2x 2x 31x 2x 2x 2x 2x 2x 2x 429x 3x 3x 429x  
/** @import { TemplateNode } from '#client' */
/** @import { Getters } from '#shared' */
import { is_void } from '../../constants.js';
import * as w from './warnings.js';
import * as e from './errors.js';
 
const snippet_symbol = Symbol.for('svelte.snippet');
 
/**
 * @param {any} fn
 * @returns {import('svelte').Snippet}
 */
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
 * @param {Record<string, any> | undefined} $$props Only passed if render tag receives arguments and is for the children prop
 */
export function validate_snippet(snippet_fn, $$props) {
	if (
		($$props?.$$slots?.default && typeof $$props.$$slots.default !== 'boolean') ||
		(snippet_fn && snippet_fn[snippet_symbol] !== true)
	) {
		e.render_tag_invalid_argument();
	}
 
	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) {
		e.snippet_used_as_component();
	}
 
	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)) {
		w.dynamic_void_element_content(tag);
	}
}
 
/** @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) {
		e.svelte_element_invalid_this_value();
	}
}
 
/**
 * @param {any} store
 * @param {string} name
 */
export function validate_store(store, name) {
	if (store != null && typeof store.subscribe !== 'function') {
		e.store_invalid_shape(name);
	}
}