All files / src/internal/client validate.js

96.89% Statements 125/129
88% Branches 22/25
75% Functions 3/4
96.74% Lines 119/123

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1242x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 6x 12x 12x 12x 12x 12x   12x 12x 12x 28x 28x 4x 4x 4x 4x 4x 4x 4x 4x 4x 24x 24x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 222x 222x 222x 222x 222x 222x 222x 222x 129x 129x 129x 129x 45x 2x 2x 43x 45x 6x 4x 4x 2x 2x 45x 129x 216x 2x 2x 2x 2x 2x 2x 2x 2x 2x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 38x 36x 36x 36x 36x 36x 36x 36x 36x 10x 10x 10x 10x 10x 36x 36x  
import { dev_current_component_function, untrack } from './runtime.js';
import { get_descriptor, is_array } from '../shared/utils.js';
import * as e from './errors.js';
import { FILENAME } from '../../constants.js';
import { render_effect } from './reactivity/effects.js';
import * as w from './warnings.js';
 
/** regex of all html void element names */
const void_element_names =
	/^(?:area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/;
 
/** @param {string} tag */
function is_void(tag) {
	return void_element_names.test(tag) || tag.toLowerCase() === '!doctype';
}
 
/**
 * @param {() => any} collection
 * @param {(item: any, index: number) => string} key_fn
 * @returns {void}
 */
export function validate_each_keys(collection, key_fn) {
	render_effect(() => {
		const keys = new Map();
		const maybe_array = collection();
		const array = is_array(maybe_array)
			? maybe_array
			: maybe_array == null
				? []
				: Array.from(maybe_array);
		const length = array.length;
		for (let i = 0; i < length; i++) {
			const key = key_fn(array[i], i);
			if (keys.has(key)) {
				const a = String(keys.get(key));
				const b = String(i);
 
				/** @type {string | null} */
				let k = String(array[i]);
				if (k.startsWith('[object ')) k = null;
 
				e.each_key_duplicate(a, b, k);
			}
			keys.set(key, i);
		}
	});
}
 
/**
 * @param {Record<string, any>} $$props
 * @param {string[]} bindable
 * @param {string[]} non_bindable
 * @param {string[]} exports
 * @param {Function & { [FILENAME]: string }} component
 * @param {boolean} has_rest
 */
export function validate_prop_bindings(
	$$props,
	bindable,
	non_bindable,
	exports,
	component,
	has_rest
) {
	for (const key in $$props) {
		var setter = get_descriptor($$props, key)?.set;
		var name = component.name;
 
		if (setter) {
			if (exports.includes(key)) {
				e.bind_invalid_export(component[FILENAME], key, name);
			}
 
			if (!bindable.includes(key)) {
				if (has_rest || non_bindable.includes(key)) {
					e.bind_not_bindable(key, component[FILENAME], name);
				}
				w.binding_non_existent_prop(key, component[FILENAME], name);
			}
		}
	}
}
 
/**
 * @param {string} binding
 * @param {() => Record<string, any>} get_object
 * @param {() => string} get_property
 * @param {number} line
 * @param {number} column
 */
export function validate_binding(binding, get_object, get_property, line, column) {
	var warned = false;
 
	var filename = dev_current_component_function?.[FILENAME];
 
	render_effect(() => {
		if (warned) return;
 
		var object = get_object();
		var property = get_property();
 
		var ran = false;
 
		// by making the (possibly false, but it would be an extreme edge case) assumption
		// that a getter has a corresponding setter, we can determine if a property is
		// reactive by seeing if this effect has dependencies
		var effect = render_effect(() => {
			if (ran) return;
 
			// eslint-disable-next-line @typescript-eslint/no-unused-expressions
			object[property];
		});
 
		ran = true;
 
		if (effect.deps === null) {
			var location = `${filename}:${line}:${column}`;
			w.binding_property_non_reactive(binding, location);
 
			warned = true;
		}
	});
}