All files / src/compiler/utils extract_svelte_ignore.js

100% Statements 51/51
100% Branches 15/15
100% Functions 1/1
100% Lines 49/49

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 502x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 622x 622x 111x 111x 111x 111x 111x 111x 111x 324x 118x 118x 118x 118x 118x 15x 15x 15x 3x 3x 3x 3x 3x 3x 15x 7x 7x 15x 118x 111x 111x 111x  
import fuzzymatch from '../phases/1-parse/utils/fuzzymatch.js';
import * as w from '../warnings.js';
 
const regex_svelte_ignore = /^\s*svelte-ignore\s/;
 
/** @type {Record<string, string>} */
const replacements = {
	'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement'
};
 
/**
 * @param {number} offset
 * @param {string} text
 * @param {boolean} runes
 * @returns {string[]}
 */
export function extract_svelte_ignore(offset, text, runes) {
	const match = regex_svelte_ignore.exec(text);
	if (!match) return [];
 
	let length = match[0].length;
	offset += length;
 
	/** @type {string[]} */
	const ignores = [];
 
	for (const match of text.slice(length).matchAll(/\S+/gm)) {
		const code = match[0];
 
		ignores.push(code);
 
		if (!w.codes.includes(code)) {
			const replacement = replacements[code] ?? code.replace(/-/g, '_');
 
			if (runes) {
				const start = offset + match.index;
				const end = start + code.length;
 
				const suggestion = w.codes.includes(replacement) ? replacement : fuzzymatch(code, w.codes);
 
				w.unknown_code({ start, end }, code, suggestion);
			} else if (w.codes.includes(replacement)) {
				ignores.push(replacement);
			}
		}
	}
 
	return ignores;
}