All files / src/compiler/utils extract_svelte_ignore.js

100% Statements 56/56
94.44% Branches 17/18
100% Functions 3/3
100% Lines 55/55

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 562x 2x 2x 2x 2x 2x 2x 2x 2x 227x 227x 227x 50x 50x 50x 227x 227x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1171x 1171x 347x 347x 824x 824x 1171x 1346x 1346x 360x 360x 1346x 89x 72x 72x 89x 1346x 464x 464x 464x  
import { regex_whitespace } from '../phases/patterns.js';
 
const regex_svelte_ignore = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;
 
/**
 * @param {string} text
 * @returns {string[]}
 */
export function extract_svelte_ignore(text) {
	const match = regex_svelte_ignore.exec(text);
	return match
		? match[1]
				.split(regex_whitespace)
				.map(/** @param {any} x */ (x) => x.trim())
				.filter(Boolean)
		: [];
}
 
/**
 * @template {{ leadingComments?: Array<{ value: string }> }} Node
 * @param {Node} node
 * @returns {string[]}
 */
export function extract_svelte_ignore_from_comments(node) {
	return (node.leadingComments || []).flatMap(
		/** @param {any} comment */ (comment) => extract_svelte_ignore(comment.value)
	);
}
 
/**
 * @param {import('#compiler').TemplateNode} node
 * @param {import('#compiler').TemplateNode[]} template_nodes
 * @returns {string[]}
 */
export function extract_ignores_above_position(node, template_nodes) {
	const previous_node_idx = template_nodes.indexOf(node) - 1;
	if (previous_node_idx < 0) {
		return [];
	}
 
	const ignores = [];
	for (let i = previous_node_idx; i >= 0; i--) {
		const node = template_nodes[i];
		if (node.type !== 'Comment' && node.type !== 'Text') {
			return ignores;
		}
		if (node.type === 'Comment') {
			if (node.ignores.length) {
				ignores.push(...node.ignores);
			}
		}
	}
 
	return ignores;
}