Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ehrencrona committed Dec 9, 2020
1 parent 90274b9 commit 0011123
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
40 changes: 21 additions & 19 deletions src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,13 +25,13 @@ class PreprocessResult {
get_location: ReturnType<typeof getLocator>;

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);
}
Expand All @@ -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:
Expand All @@ -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
};
}
Expand All @@ -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 = `</${tag_name}>`;

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,
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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();
}
12 changes: 6 additions & 6 deletions src/compiler/preprocess/replace_in_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof getLocator>;
filename: string;
filename: string;
}

interface Replacement {
Expand All @@ -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 '';
Expand Down

0 comments on commit 0011123

Please sign in to comment.