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 5369x 5369x 5369x 5369x 4x 5369x 2x 2x 5369x 2x 2x 2x 2x 2x 2x 1176x 1176x 1176x 1176x 8x 8x 8x 8x 1176x 4x 4x 1176x 1176x 1162x 1162x 14x 1176x 1176x 1176x 2x 2x 1176x 2x 2x 2x 2x 2x 2x 2x 216x 216x 216x 216x 2x     2x 2x 214x 214x 216x 692x 692x 692x 447x 447x 447x 447x 447x 692x 211x 211x 692x 214x 214x 211x 1x 1x 210x 210x 211x 211x 9x 211x 201x 6x 6x 195x 195x 195x 201x 5x 5x 190x 190x 190x 201x 1x 4x 2x 2x 2x 4x 2x 1x 1x 2x 1x 1x 1x   201x 216x 1x 1x 216x  
/** @import { AST, 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 {AST.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 {AST.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 {AST.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);
	}
}