Home / File/ extract_svelte_ignore.js — svelte Source File

extract_svelte_ignore.js — svelte Source File

Architecture documentation for extract_svelte_ignore.js, a javascript file in the svelte codebase. 5 imports, 4 dependents.

File javascript Compiler Transformer 5 imports 4 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  d1869e84_a713_3c60_3aae_40e1a6b78424["extract_svelte_ignore.js"]
  73865c3c_2786_c9ac_d34f_b51d28b3a29e["constants.js"]
  d1869e84_a713_3c60_3aae_40e1a6b78424 --> 73865c3c_2786_c9ac_d34f_b51d28b3a29e
  4057eb45_ab28_d989_1209_dfae45d590c0["fuzzymatch.js"]
  d1869e84_a713_3c60_3aae_40e1a6b78424 --> 4057eb45_ab28_d989_1209_dfae45d590c0
  0913e53f_3cfc_070a_7b42_568cf6860af3["fuzzymatch"]
  d1869e84_a713_3c60_3aae_40e1a6b78424 --> 0913e53f_3cfc_070a_7b42_568cf6860af3
  56a689f9_11c0_cc76_bd60_41bb6dc96475["warnings.js"]
  d1869e84_a713_3c60_3aae_40e1a6b78424 --> 56a689f9_11c0_cc76_bd60_41bb6dc96475
  3246e0bc_b9fc_f638_5e35_41e8c39a2408["w"]
  d1869e84_a713_3c60_3aae_40e1a6b78424 --> 3246e0bc_b9fc_f638_5e35_41e8c39a2408
  21beed52_94ea_f810_11b8_87a2cf50432f["legacy.js"]
  21beed52_94ea_f810_11b8_87a2cf50432f --> d1869e84_a713_3c60_3aae_40e1a6b78424
  cab41022_1b55_3b7a_06c6_b90763bbea47["index.js"]
  cab41022_1b55_3b7a_06c6_b90763bbea47 --> d1869e84_a713_3c60_3aae_40e1a6b78424
  4aa8a188_84d4_0274_ed83_cac0ab1d3572["index.js"]
  4aa8a188_84d4_0274_ed83_cac0ab1d3572 --> d1869e84_a713_3c60_3aae_40e1a6b78424
  f8debeb0_6284_1c94_22a8_0f28192b28a5["Text.js"]
  f8debeb0_6284_1c94_22a8_0f28192b28a5 --> d1869e84_a713_3c60_3aae_40e1a6b78424
  style d1869e84_a713_3c60_3aae_40e1a6b78424 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { IGNORABLE_RUNTIME_WARNINGS } from '../../constants.js';
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>} Map of legacy code -> new code */
const replacements = {
	'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement',
	'module-script-reactive-declaration': 'reactive_declaration_module_script',
	'empty-block': 'block_empty',
	'avoid-is': 'attribute_avoid_is',
	'invalid-html-attribute': 'attribute_invalid_property_name',
	'a11y-structure': 'a11y_figcaption_parent',
	'illegal-attribute-character': 'attribute_illegal_colon',
	'invalid-rest-eachblock-binding': 'bind_invalid_each_rest',
	'unused-export-let': 'export_let_unused'
};

const codes = w.codes.concat(IGNORABLE_RUNTIME_WARNINGS);

/**
 * @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 = [];

	if (runes) {
		// 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];

			if (codes.includes(code)) {
				ignores.push(code);
			} else {
				const replacement = replacements[code] ?? code.replace(/-/g, '_');

				// The type cast is for some reason necessary to pass the type check in CI
				const start = offset + /** @type {number} */ (match.index);
				const end = start + code.length;

				if (codes.includes(replacement)) {
					w.legacy_code({ start, end }, code, replacement);
				} else {
					const suggestion = fuzzymatch(code, codes);
					w.unknown_code({ start, end }, code, suggestion);
				}
			}

			if (!match[2]) {
				break;
			}
		}
	} else {
		// Non-runes mode: lax parsing, backwards compat with old codes
		for (const match of text.slice(length).matchAll(/[\w$-]+/gm)) {
			const code = match[0];

			ignores.push(code);

			if (!codes.includes(code)) {
				const replacement = replacements[code] ?? code.replace(/-/g, '_');

				if (codes.includes(replacement)) {
					ignores.push(replacement);
				}
			}
		}
	}

	return ignores;
}

/**
 * Replaces legacy svelte-ignore codes with new codes.
 * @param {string} text
 * @returns {string}
 */
export function migrate_svelte_ignore(text) {
	const match = regex_svelte_ignore.exec(text);
	if (!match) return text;

	const length = match[0].length;
	return (
		text.substring(0, length) +
		text.substring(length).replace(/\w+-\w+(-\w+)*/g, (code, _, idx) => {
			let replacement = replacements[code] ?? code.replace(/-/g, '_');
			if (/\w+-\w+/.test(text.substring(length + idx + code.length))) {
				replacement += ',';
			}
			return replacement;
		})
	);
}

Domain

Subdomains

Frequently Asked Questions

What does extract_svelte_ignore.js do?
extract_svelte_ignore.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Transformer subdomain.
What functions are defined in extract_svelte_ignore.js?
extract_svelte_ignore.js defines 2 function(s): extract_svelte_ignore, migrate_svelte_ignore.
What does extract_svelte_ignore.js depend on?
extract_svelte_ignore.js imports 5 module(s): constants.js, fuzzymatch, fuzzymatch.js, w, warnings.js.
What files import extract_svelte_ignore.js?
extract_svelte_ignore.js is imported by 4 file(s): Text.js, index.js, index.js, legacy.js.
Where is extract_svelte_ignore.js in the architecture?
extract_svelte_ignore.js is located at packages/svelte/src/compiler/utils/extract_svelte_ignore.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/utils).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free