All files / src/compiler state.js

100% Statements 99/99
100% Branches 13/13
100% Functions 6/6
100% Lines 98/98

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 992x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 86x 86x 86x 2x 2x 86x 86x 2x 2x 2x 2x 2x 2x 5437x 5437x 2x 2x 2x 2x 2x 2x 2x 2x 2107x 2107x 2x 2x 2x 2x 2x 2x 5436x 5436x 5436x 5436x 5436x 5436x 5436x 4918x 4918x 4918x 5436x 5436x 5436x 5436x 5436x 5436x  
/** @import { CompileOptions, SvelteNode } from './types' */
/** @import { Warning } from '#compiler' */
import { getLocator } from 'locate-character';
 
/** @typedef {{ start?: number, end?: number }} NodeLike */
 
/** @type {Warning[]} */
export let warnings = [];
 
/**
 * The filename relative to the rootDir (if specified).
 * This should not be used in the compiler output except in dev mode
 * @type {string}
 */
export let filename;
 
/**
 * The original source code
 * @type {string}
 */
export let source;
 
/**
 * True if compiling with `dev: true`
 * @type {boolean}
 */
export let dev;
 
export let locator = getLocator('', { offsetLine: 1 });
 
/** @type {NonNullable<CompileOptions['warningFilter']>} */
export let warning_filter;
 
/**
 * The current stack of ignored warnings
 * @type {Set<string>[]}
 */
export let ignore_stack = [];
 
/**
 * For each node the list of warnings that should be ignored for that node.
 * Exists in addition to `ignore_stack` because not all warnings are emitted
 * while the stack is being built.
 * @type {Map<SvelteNode | NodeLike, Set<string>[]>}
 */
export let ignore_map = new Map();
 
/**
 * @param {string[]} ignores
 */
export function push_ignore(ignores) {
	const next = new Set([...(ignore_stack.at(-1) || []), ...ignores]);
	ignore_stack.push(next);
}
 
export function pop_ignore() {
	ignore_stack.pop();
}
 
/**
 *
 * @param {(warning: Warning) => boolean} fn
 */
export function reset_warning_filter(fn = () => true) {
	warning_filter = fn;
}
 
/**
 *
 * @param {SvelteNode | NodeLike} node
 * @param {import('../constants.js').IGNORABLE_RUNTIME_WARNINGS[number]} code
 * @returns
 */
export function is_ignored(node, code) {
	return dev && !!ignore_map.get(node)?.some((codes) => codes.has(code));
}
 
/**
 * @param {string} _source
 * @param {{ dev?: boolean; filename: string; rootDir?: string }} options
 */
export function reset(_source, options) {
	source = _source;
	const root_dir = options.rootDir?.replace(/\\/g, '/');
	filename = options.filename.replace(/\\/g, '/');
 
	dev = !!options.dev;
 
	if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
		// make filename relative to rootDir
		filename = filename.replace(root_dir, '').replace(/^[/\\]/, '');
	}
 
	locator = getLocator(source, { offsetLine: 1 });
	warnings = [];
	ignore_stack = [];
	ignore_map.clear();
}