All files / src/compiler/phases/2-analyze/css css-analyze.js

98.76% Statements 160/162
98.33% Branches 59/60
100% Functions 8/8
98.73% Lines 156/158

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1592x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4473x 4473x 4473x 4473x 4473x 4473x 136x 136x 147x 136x 4473x 4473x 2x 2x 2x 2x 94x 20x 16x 16x 20x 2x 2x 1418x 1418x 1418x 1418x 1418x 1418x 1418x 2x 2x 2304x 2304x 2304x 2304x 103x 103x 108x 2304x 2304x 2304x 2248x 2248x 2248x 2304x 2304x 2304x 2304x 2304x 2304x 2x 2x 1279x 1279x 1279x 1279x 1279x 1279x 1279x 1279x 1268x 1268x 1268x 1279x 1279x 2x 2x 2x 2x 2x 1418x 1418x 1418x 1418x 1418x 1418x 731x 1552x 3x 3x 1552x 728x 1418x 1415x 1415x 1415x 1418x 848x 1750x 1816x 1816x 1816x 1816x 97x 1816x 6x 6x 1816x 1744x 842x 1409x 1409x 1418x 2307x 2372x 2372x 2372x 112x 112x 112x 112x 70x 67x 67x 67x 67x 70x 112x 3x 3x 112x 2372x 2369x 2x 2x 18x 18x     18x 2x 2x 2x 2x 2x 2x 2x 2x 2x 371x 371x  
import { walk } from 'zimmerframe';
import { error } from '../../../errors.js';
import { is_keyframes_node } from '../../css.js';
import { merge } from '../../visitors.js';
 
/**
 * @typedef {import('zimmerframe').Visitors<
 *   import('#compiler').Css.Node,
 *   {
 *     keyframes: string[];
 *     rule: import('#compiler').Css.Rule | null;
 *   }
 * >} Visitors
 */
 
/** @param {import('#compiler').Css.RelativeSelector} relative_selector */
function is_global(relative_selector) {
	const first = relative_selector.selectors[0];
 
	return (
		first.type === 'PseudoClassSelector' &&
		first.name === 'global' &&
		relative_selector.selectors.every(
			(selector) =>
				selector.type === 'PseudoClassSelector' || selector.type === 'PseudoElementSelector'
		)
	);
}
 
/** @type {Visitors} */
const analysis_visitors = {
	Atrule(node, context) {
		if (is_keyframes_node(node)) {
			if (!node.prelude.startsWith('-global-')) {
				context.state.keyframes.push(node.prelude);
			}
		}
	},
	ComplexSelector(node, context) {
		context.next(); // analyse relevant selectors first
 
		node.metadata.rule = context.state.rule;
 
		node.metadata.used = node.children.every(
			({ metadata }) => metadata.is_global || metadata.is_host || metadata.is_root
		);
	},
	RelativeSelector(node, context) {
		node.metadata.is_global =
			node.selectors.length >= 1 &&
			node.selectors[0].type === 'PseudoClassSelector' &&
			node.selectors[0].name === 'global' &&
			node.selectors.every(
				(selector) =>
					selector.type === 'PseudoClassSelector' || selector.type === 'PseudoElementSelector'
			);
 
		if (node.selectors.length === 1) {
			const first = node.selectors[0];
			node.metadata.is_host = first.type === 'PseudoClassSelector' && first.name === 'host';
		}
 
		node.metadata.is_root = !!node.selectors.find(
			(child) => child.type === 'PseudoClassSelector' && child.name === 'root'
		);
 
		context.next();
	},
	Rule(node, context) {
		node.metadata.parent_rule = context.state.rule;
 
		context.next({
			...context.state,
			rule: node
		});
 
		node.metadata.has_local_selectors = node.prelude.children.some((selector) => {
			return selector.children.some(
				({ metadata }) => !metadata.is_global && !metadata.is_host && !metadata.is_root
			);
		});
	}
};
 
/** @type {Visitors} */
const validation_visitors = {
	ComplexSelector(node, context) {
		// ensure `:global(...)` is not used in the middle of a selector
		{
			const a = node.children.findIndex((child) => !is_global(child));
			const b = node.children.findLastIndex((child) => !is_global(child));
 
			if (a !== b) {
				for (let i = a; i <= b; i += 1) {
					if (is_global(node.children[i])) {
						error(node.children[i].selectors[0], 'invalid-css-global-placement');
					}
				}
			}
		}
 
		// ensure `:global(...)`contains a single selector
		// (standalone :global() with multiple selectors is OK)
		if (node.children.length > 1 || node.children[0].selectors.length > 1) {
			for (const relative_selector of node.children) {
				for (const selector of relative_selector.selectors) {
					if (
						selector.type === 'PseudoClassSelector' &&
						selector.name === 'global' &&
						selector.args !== null &&
						selector.args.children.length > 1
					) {
						error(selector, 'invalid-css-global-selector');
					}
				}
			}
		}
 
		// ensure `:global(...)` is not part of a larger compound selector
		for (const relative_selector of node.children) {
			for (let i = 0; i < relative_selector.selectors.length; i++) {
				const selector = relative_selector.selectors[i];
 
				if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
					const child = selector.args?.children[0].children[0];
					if (
						child?.selectors[0].type === 'TypeSelector' &&
						!/[.:#]/.test(child.selectors[0].name[0]) &&
						(i !== 0 ||
							relative_selector.selectors
								.slice(1)
								.some(
									(s) => s.type !== 'PseudoElementSelector' && s.type !== 'PseudoClassSelector'
								))
					) {
						error(selector, 'invalid-css-global-selector-list');
					}
				}
			}
		}
	},
	NestingSelector(node, context) {
		const rule = /** @type {import('#compiler').Css.Rule} */ (context.state.rule);
		if (!rule.metadata.parent_rule) {
			error(node, 'invalid-nesting-selector');
		}
	}
};
 
const css_visitors = merge(analysis_visitors, validation_visitors);
 
/**
 * @param {import('#compiler').Css.StyleSheet} stylesheet
 * @param {import('../../types.js').ComponentAnalysis} analysis
 */
export function analyze_css(stylesheet, analysis) {
	walk(stylesheet, { keyframes: analysis.css.keyframes, rule: null }, css_visitors);
}