All files / src/compiler/phases/2-analyze/visitors/shared attribute.js

97.65% Statements 125/128
96.36% Branches 53/55
100% Functions 3/3
97.56% Lines 120/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 5164x 5164x 5164x 5164x 4x 5164x 2x 2x 5164x 2x 2x 2x 2x 2x 2x 1018x 1018x 1018x 1018x 8x 8x 8x 8x 1018x 4x 4x 1018x 1018x 1004x 1004x 14x 1018x 1018x 1018x 2x 2x 1018x 2x 2x 2x 2x 2x 2x 2x 203x 203x 203x 203x 2x     2x 2x 201x 201x 203x 653x 653x 653x 421x 421x 421x 421x 421x 653x 198x 198x 653x 201x 201x 198x 1x 1x 197x 197x 198x 198x 8x 198x 189x 6x 6x 183x 183x 183x 189x 5x 5x 178x 178x 178x 189x 1x 4x 2x 2x 2x 4x 2x 1x 1x 2x 1x 1x 1x   189x 203x 1x 1x 203x  
/** @import { Attribute, ElementLike } from '#compiler' */
/** @import { Context } from '../../types' */
import * as e from '../../../../errors.js';
import { is_text_attribute } from '../../../../utils/ast.js';
import * as w from '../../../../warnings.js';
import { is_custom_element_node } from '../../../nodes.js';
import { regex_only_whitespaces } from '../../../patterns.js';
 
/**
 * @param {Attribute} attribute
 */
export function validate_attribute_name(attribute) {
	if (
		attribute.name.includes(':') &&
		!attribute.name.startsWith('xmlns:') &&
		!attribute.name.startsWith('xlink:') &&
		!attribute.name.startsWith('xml:')
	) {
		w.attribute_illegal_colon(attribute);
	}
}
 
/**
 * @param {Attribute} attribute
 * @param {ElementLike} parent
 */
export function validate_attribute(attribute, parent) {
	if (
		Array.isArray(attribute.value) &&
		attribute.value.length === 1 &&
		attribute.value[0].type === 'ExpressionTag' &&
		(parent.type === 'Component' ||
			parent.type === 'SvelteComponent' ||
			parent.type === 'SvelteSelf' ||
			(parent.type === 'RegularElement' && is_custom_element_node(parent)))
	) {
		w.attribute_quoted(attribute);
	}
 
	if (attribute.value === true || !Array.isArray(attribute.value) || attribute.value.length === 1) {
		return;
	}
 
	const is_quoted = attribute.value.at(-1)?.end !== attribute.end;
 
	if (!is_quoted) {
		e.attribute_unquoted_sequence(attribute);
	}
}
 
/**
 * @param {Context} context
 * @param {Attribute} attribute
 * @param {boolean} is_component
 */
export function validate_slot_attribute(context, attribute, is_component = false) {
	const parent = context.path.at(-2);
	let owner = undefined;
 
	if (parent?.type === 'SnippetBlock') {
		if (!is_text_attribute(attribute)) {
			e.slot_attribute_invalid(attribute);
		}
		return;
	}
 
	let i = context.path.length;
	while (i--) {
		const ancestor = context.path[i];
		if (
			!owner &&
			(ancestor.type === 'Component' ||
				ancestor.type === 'SvelteComponent' ||
				ancestor.type === 'SvelteSelf' ||
				ancestor.type === 'SvelteElement' ||
				(ancestor.type === 'RegularElement' && is_custom_element_node(ancestor)))
		) {
			owner = ancestor;
		}
	}
 
	if (owner) {
		if (!is_text_attribute(attribute)) {
			e.slot_attribute_invalid(attribute);
		}
 
		if (
			owner.type === 'Component' ||
			owner.type === 'SvelteComponent' ||
			owner.type === 'SvelteSelf'
		) {
			if (owner !== parent) {
				e.slot_attribute_invalid_placement(attribute);
			}
 
			const name = attribute.value[0].data;
 
			if (context.state.component_slots.has(name)) {
				e.slot_attribute_duplicate(attribute, name, owner.name);
			}
 
			context.state.component_slots.add(name);
 
			if (name === 'default') {
				for (const node of owner.fragment.nodes) {
					if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
						continue;
					}
 
					if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
						if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
							continue;
						}
					}
 
					e.slot_default_duplicate(node);
				}
			}
		}
	} else if (!is_component) {
		e.slot_attribute_invalid_placement(attribute);
	}
}