All files / src/compiler/utils extract_svelte_ignore.js

100% Statements 60/60
100% Branches 16/16
100% Functions 1/1
100% Lines 58/58

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 592x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 627x 627x 115x 115x 115x 115x 115x 115x 115x 115x 329x 122x 122x 122x 122x 122x 15x 15x 15x 3x 3x 3x 3x 3x 2x 3x 1x 1x 1x 15x 7x 7x 15x 122x 122x 115x 115x 122x 115x 115x 115x  
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 = [];
 
	// Warnings have to be separated by commas, everything after is interpreted as prose
	for (const match of text.slice(length).matchAll(/([\w$-]+)(,)?/gm)) {
		const code = match[1];
 
		ignores.push(code);
 
		if (!w.codes.includes(code)) {
			const replacement = replacements[code] ?? code.replace(/-/g, '_');
 
			if (runes) {
				// github are you stuck?
				const start = offset + match.index;
				const end = start + code.length;
 
				if (w.codes.includes(replacement)) {
					w.legacy_code({ start, end }, code, replacement);
				} else {
					const suggestion = fuzzymatch(code, w.codes);
					w.unknown_code({ start, end }, code, suggestion);
				}
			} else if (w.codes.includes(replacement)) {
				ignores.push(replacement);
			}
		}
 
		if (!match[2]) {
			break;
		}
	}
 
	return ignores;
}