Home / File/ replace_in_code.js — svelte Source File

replace_in_code.js — svelte Source File

Architecture documentation for replace_in_code.js, a javascript file in the svelte codebase. 2 imports, 1 dependents.

File javascript Compiler Migrator 2 imports 1 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  76cde72f_b08b_6c0b_1e04_dda16a7ac104["replace_in_code.js"]
  d383a41d_5383_ee86_cab6_03bf1a2daf93["mapped_code.js"]
  76cde72f_b08b_6c0b_1e04_dda16a7ac104 --> d383a41d_5383_ee86_cab6_03bf1a2daf93
  fd3be7c7_b876_965d_9025_a9b9bd4c6aaf["MappedCode"]
  76cde72f_b08b_6c0b_1e04_dda16a7ac104 --> fd3be7c7_b876_965d_9025_a9b9bd4c6aaf
  ccf6927a_1f1b_1dda_8616_80903a72ba16["index.js"]
  ccf6927a_1f1b_1dda_8616_80903a72ba16 --> 76cde72f_b08b_6c0b_1e04_dda16a7ac104
  style 76cde72f_b08b_6c0b_1e04_dda16a7ac104 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Source, Replacement } from './private.js' */
import { MappedCode } from '../utils/mapped_code.js';

/**
 * @param {string} code_slice
 * @param {number} offset
 * @param {Source} opts
 * @returns {Source}
 */
export function slice_source(code_slice, offset, { file_basename, filename, get_location }) {
	return {
		source: code_slice,
		get_location: (index) => get_location(index + offset),
		file_basename,
		filename
	};
}

/**
 * @param {RegExp} re
 * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
 * @param {string} source
 */
function calculate_replacements(re, get_replacement, source) {
	/**
	 * @type {Array<Promise<Replacement>>}
	 */
	const replacements = [];
	source.replace(re, (...match) => {
		replacements.push(
			get_replacement(...match).then((replacement) => {
				const matched_string = match[0];
				const offset = match[match.length - 2];
				return { offset, length: matched_string.length, replacement };
			})
		);
		return '';
	});
	return Promise.all(replacements);
}

/**
 * @param {Replacement[]} replacements
 * @param {Source} source
 * @returns {MappedCode}
 */
function perform_replacements(replacements, source) {
	const out = new MappedCode();
	let last_end = 0;
	for (const { offset, length, replacement } of replacements) {
		const unchanged_prefix = MappedCode.from_source(
			slice_source(source.source.slice(last_end, offset), last_end, source)
		);
		out.concat(unchanged_prefix).concat(replacement);
		last_end = offset + length;
	}
	const unchanged_suffix = MappedCode.from_source(
		slice_source(source.source.slice(last_end), last_end, source)
	);
	return out.concat(unchanged_suffix);
}

/**
 * @param {RegExp} regex
 * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
 * @param {Source} location
 * @returns {Promise<MappedCode>}
 */
export async function replace_in_code(regex, get_replacement, location) {
	const replacements = await calculate_replacements(regex, get_replacement, location.source);
	return perform_replacements(replacements, location);
}

Domain

Subdomains

Frequently Asked Questions

What does replace_in_code.js do?
replace_in_code.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Migrator subdomain.
What functions are defined in replace_in_code.js?
replace_in_code.js defines 4 function(s): calculate_replacements, perform_replacements, replace_in_code, slice_source.
What does replace_in_code.js depend on?
replace_in_code.js imports 2 module(s): MappedCode, mapped_code.js.
What files import replace_in_code.js?
replace_in_code.js is imported by 1 file(s): index.js.
Where is replace_in_code.js in the architecture?
replace_in_code.js is located at packages/svelte/src/compiler/preprocess/replace_in_code.js (domain: Compiler, subdomain: Migrator, directory: packages/svelte/src/compiler/preprocess).

Analyze Your Own Codebase

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

Try Supermodel Free