diff --git a/src/compiler/preprocess/replace_in_code.js b/src/compiler/preprocess/replace_in_code.js index c7221ccf08..c5f65f4afd 100644 --- a/src/compiler/preprocess/replace_in_code.js +++ b/src/compiler/preprocess/replace_in_code.js @@ -7,12 +7,12 @@ import { MappedCode } from '../utils/mapped_code'; * @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 - }; + return { + source: code_slice, + get_location: (index) => get_location(index + offset), + file_basename, + filename + }; } /** @@ -21,20 +21,21 @@ export function slice_source(code_slice, offset, { file_basename, filename, get_ * @param {string} source */ function calculate_replacements(re, get_replacement, source) { - - /** - * @type {Array>} - */ - 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); + /** + * @type {Array>} + */ + 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); } /** @@ -43,15 +44,19 @@ function calculate_replacements(re, get_replacement, 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); + 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); } /** @@ -61,15 +66,12 @@ function perform_replacements(replacements, source) { * @returns {Promise} */ 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); + const replacements = await calculate_replacements(regex, get_replacement, location.source); + return perform_replacements(replacements, location); } - - - /** @typedef {Object} Replacement * @property {number} offset * @property {number} length - * @property {MappedCode} replacement - */ \ No newline at end of file + * @property {MappedCode} replacement + */