Convert src/compiler/preprocess/replace_in_code.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 7284997aec
commit b9d9742d74

@ -1,72 +1,75 @@
import { MappedCode } from '../utils/mapped_code'; import { MappedCode } from '../utils/mapped_code';
import { Source } from './types';
interface Replacement { /**
offset: number; * @param {string} code_slice
length: number; * @param {number} offset
replacement: MappedCode; * @param {Source}
} * @returns {Source}
*/
export function slice_source( export function slice_source(code_slice, offset, { file_basename, filename, get_location }) {
code_slice: string,
offset: number,
{ file_basename, filename, get_location }: Source
): Source {
return { return {
source: code_slice, source: code_slice,
get_location: (index: number) => get_location(index + offset), get_location: (index) => get_location(index + offset),
file_basename, file_basename,
filename filename
}; };
} }
function calculate_replacements( /**
re: RegExp, * @param {RegExp} re
get_replacement: (...match: any[]) => Promise<MappedCode>, * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
source: string * @param {string} source
) { */
const replacements: Array<Promise<Replacement>> = []; function calculate_replacements(re, get_replacement, source) {
/**
* @type {Array<Promise<Replacement>>}
*/
const replacements = [];
source.replace(re, (...match) => { source.replace(re, (...match) => {
replacements.push( replacements.push(get_replacement(...match).then((replacement) => {
get_replacement(...match).then((replacement) => {
const matched_string = match[0]; const matched_string = match[0];
const offset = match[match.length - 2]; const offset = match[match.length - 2];
return { offset, length: matched_string.length, replacement }; return { offset, length: matched_string.length, replacement };
}) }));
);
return ''; return '';
}); });
return Promise.all(replacements); return Promise.all(replacements);
} }
function perform_replacements(replacements: Replacement[], source: Source): MappedCode { /**
* @param {Replacement[]} replacements
* @param {Source} source
* @returns {MappedCode}
*/
function perform_replacements(replacements, source) {
const out = new MappedCode(); const out = new MappedCode();
let last_end = 0; let last_end = 0;
for (const { offset, length, replacement } of replacements) { for (const { offset, length, replacement } of replacements) {
const unchanged_prefix = MappedCode.from_source( const unchanged_prefix = MappedCode.from_source(slice_source(source.source.slice(last_end, offset), last_end, source));
slice_source(source.source.slice(last_end, offset), last_end, source)
);
out.concat(unchanged_prefix).concat(replacement); out.concat(unchanged_prefix).concat(replacement);
last_end = offset + length; last_end = offset + length;
} }
const unchanged_suffix = MappedCode.from_source(slice_source(source.source.slice(last_end), last_end, source));
const unchanged_suffix = MappedCode.from_source(
slice_source(source.source.slice(last_end), last_end, source)
);
return out.concat(unchanged_suffix); return out.concat(unchanged_suffix);
} }
export async function replace_in_code( /**
regex: RegExp, * @param {RegExp} regex
get_replacement: (...match: any[]) => Promise<MappedCode>, * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
location: Source * @param {Source} location
): Promise<MappedCode> { * @returns {Promise<MappedCode>}
*/
export async function replace_in_code(regex, get_replacement, location) {
const replacements = await calculate_replacements(regex, get_replacement, location.source); const replacements = await calculate_replacements(regex, get_replacement, location.source);
return perform_replacements(replacements, location); return perform_replacements(replacements, location);
} }
/** @typedef {Object} Replacement
* @property {number} offset
* @property {number} length
* @property {MappedCode} replacement
*/
Loading…
Cancel
Save