All files / src/compiler/utils extract_svelte_ignore.js

84.31% Statements 43/51
81.81% Branches 9/11
100% Functions 1/1
83.67% Lines 41/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 610x 610x 98x 98x 98x 98x 98x 98x 98x 312x 105x 105x 105x 105x 105x 4x 4x 4x             4x     4x 105x 98x 98x 98x  
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;
}