From 0011123174442ca2b6fe180392dd68f6b4af3059 Mon Sep 17 00:00:00 2001 From: Andreas Ehrencrona Date: Wed, 9 Dec 2020 13:01:50 +0200 Subject: [PATCH] minor tweaks --- src/compiler/preprocess/index.ts | 40 ++++++++++++---------- src/compiler/preprocess/replace_in_code.ts | 12 +++---- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 84537c684a..3a8b8676ff 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -12,6 +12,9 @@ interface SourceUpdate { dependencies?: string[]; } +/** + * Represents intermediate states of the preprocessing. + */ class PreprocessResult { // sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1) // so we use sourcemap_list.unshift() to add new maps @@ -22,13 +25,13 @@ class PreprocessResult { get_location: ReturnType; constructor(public source: string, public filename: string) { - this.update_source({string: source}); + this.update_source({ string: source }); } - update_source({string: source, map, dependencies}: SourceUpdate) { + update_source({ string: source, map, dependencies }: SourceUpdate) { this.source = source; this.get_location = getLocator(source); - + if (map) { this.sourcemap_list.unshift(map); } @@ -38,12 +41,9 @@ class PreprocessResult { } } - processed(): Processed { + to_processed(): Processed { // Combine all the source maps for each preprocessor function into one - const map: RawSourceMap = combine_sourcemaps( - this.filename, - this.sourcemap_list - ); + const map: RawSourceMap = combine_sourcemaps(this.filename, this.sourcemap_list); return { // TODO return separated output, in future version where svelte.compile supports it: @@ -53,7 +53,7 @@ class PreprocessResult { code: this.source, dependencies: [...new Set(this.dependencies)], - map: (map as object), + map: map as object, toString: () => this.source }; } @@ -78,29 +78,31 @@ function processed_content_to_sws( } /** - * Convert the whole tag including content (replacing it with `processed`) - * into a `StringWithSourcemap` representing the transformed code. + * Given the whole tag including content, return a `StringWithSourcemap` + * representing the tag content replaced with `processed`. */ function processed_tag_to_sws( processed: Processed, tag_name: 'style' | 'script', attributes: string, - content: string, - { filename, get_location }: Source): StringWithSourcemap { - const build_sws = (content: string, offset: number) => - StringWithSourcemap.from_source(filename, content, get_location(offset)); + { source, filename, get_location }: Source): StringWithSourcemap { + const build_sws = (source: string, offset: number) => + StringWithSourcemap.from_source(filename, source, get_location(offset)); const tag_open = `<${tag_name}${attributes || ''}>`; const tag_close = ``; const tag_open_sws = build_sws(tag_open, 0); - const tag_close_sws = build_sws(tag_close, tag_open.length + content.length); + const tag_close_sws = build_sws(tag_close, tag_open.length + source.length); const content_sws = processed_content_to_sws(processed, get_location(tag_open.length)); return tag_open_sws.concat(content_sws).concat(tag_close_sws); } +/** + * Calculate the updates required to process all instances of the specified tag. + */ async function process_tag( tag_name: 'style' | 'script', preprocessor: Preprocessor, @@ -134,8 +136,8 @@ async function process_tag( if (!processed) return no_change(); if (processed.dependencies) dependencies.push(...processed.dependencies); - return processed_tag_to_sws(processed, tag_name, attributes, content, - {...source, get_location: offset => source.get_location(offset + tag_offset)}); + return processed_tag_to_sws(processed, tag_name, attributes, + {source: content, get_location: offset => source.get_location(offset + tag_offset), filename}); } return {...await replace_in_code(tag_regex, process_single_tag, source), dependencies}; @@ -190,5 +192,5 @@ export default async function preprocess( result.update_source(await process_tag('style', preprocess, result)); } - return result.processed(); + return result.to_processed(); } diff --git a/src/compiler/preprocess/replace_in_code.ts b/src/compiler/preprocess/replace_in_code.ts index 96a071a1d6..eb2e553547 100644 --- a/src/compiler/preprocess/replace_in_code.ts +++ b/src/compiler/preprocess/replace_in_code.ts @@ -2,9 +2,9 @@ import { getLocator } from 'locate-character'; import { StringWithSourcemap } from '../utils/string_with_sourcemap'; export interface Source { - source: string; + source: string; get_location: ReturnType; - filename: string; + filename: string; } interface Replacement { @@ -24,11 +24,11 @@ function calculate_replacements( replacements.push( get_replacement(...match).then( replacement => { - const matched_string = match[0]; - const offset = match[match.length-2]; + const matched_string = match[0]; + const offset = match[match.length-2]; - return ({ offset, length: matched_string.length, replacement }); - } + return ({ offset, length: matched_string.length, replacement }); + } ) ); return '';