Skip to content

Commit e835ade

Browse files
committed
minor tweaks
1 parent 77739d7 commit e835ade

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/compiler/preprocess/index.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function get_file_basename(filename: string) {
1616
return filename.split(/[/\\]/).pop();
1717
}
1818

19+
/**
20+
* Represents intermediate states of the preprocessing.
21+
*/
1922
class PreprocessResult {
2023
// sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1)
2124
// so we use sourcemap_list.unshift() to add new maps
@@ -27,16 +30,16 @@ class PreprocessResult {
2730
get_location: ReturnType<typeof getLocator>;
2831

2932
constructor(public source: string, public filename: string) {
30-
this.update_source({string: source});
33+
this.update_source({ string: source });
3134

3235
// preprocess source must be relative to itself or equal null
3336
this.file_basename = filename == null ? null : get_file_basename(filename);
3437
}
3538

36-
update_source({string: source, map, dependencies}: SourceUpdate) {
39+
update_source({ string: source, map, dependencies }: SourceUpdate) {
3740
this.source = source;
3841
this.get_location = getLocator(source);
39-
42+
4043
if (map) {
4144
this.sourcemap_list.unshift(map);
4245
}
@@ -46,12 +49,9 @@ class PreprocessResult {
4649
}
4750
}
4851

49-
processed(): Processed {
52+
to_processed(): Processed {
5053
// Combine all the source maps for each preprocessor function into one
51-
const map: RawSourceMap = combine_sourcemaps(
52-
this.file_basename,
53-
this.sourcemap_list
54-
);
54+
const map: RawSourceMap = combine_sourcemaps(this.file_basename, this.sourcemap_list);
5555

5656
return {
5757
// TODO return separated output, in future version where svelte.compile supports it:
@@ -61,7 +61,7 @@ class PreprocessResult {
6161

6262
code: this.source,
6363
dependencies: [...new Set(this.dependencies)],
64-
map: (map as object),
64+
map: map as object,
6565
toString: () => this.source
6666
};
6767
}
@@ -91,29 +91,31 @@ function processed_content_to_sws(
9191
}
9292

9393
/**
94-
* Convert the whole tag including content (replacing it with `processed`)
95-
* into a `StringWithSourcemap` representing the transformed code.
94+
* Given the whole tag including content, return a `StringWithSourcemap`
95+
* representing the tag content replaced with `processed`.
9696
*/
9797
function processed_tag_to_sws(
9898
processed: Processed,
9999
tag_name: 'style' | 'script',
100100
attributes: string,
101-
content: string,
102-
{ file_basename, get_location }: Source): StringWithSourcemap {
103-
const build_sws = (content: string, offset: number) =>
104-
StringWithSourcemap.from_source(file_basename, content, get_location(offset));
101+
{ source, file_basename, get_location }: Source): StringWithSourcemap {
102+
const build_sws = (source: string, offset: number) =>
103+
StringWithSourcemap.from_source(file_basename, source, get_location(offset));
105104

106105
const tag_open = `<${tag_name}${attributes || ''}>`;
107106
const tag_close = `</${tag_name}>`;
108107

109108
const tag_open_sws = build_sws(tag_open, 0);
110-
const tag_close_sws = build_sws(tag_close, tag_open.length + content.length);
109+
const tag_close_sws = build_sws(tag_close, tag_open.length + source.length);
111110

112111
const content_sws = processed_content_to_sws(processed, get_location(tag_open.length), file_basename);
113112

114113
return tag_open_sws.concat(content_sws).concat(tag_close_sws);
115114
}
116115

116+
/**
117+
* Calculate the updates required to process all instances of the specified tag.
118+
*/
117119
async function process_tag(
118120
tag_name: 'style' | 'script',
119121
preprocessor: Preprocessor,
@@ -147,8 +149,8 @@ async function process_tag(
147149
if (processed && processed.dependencies) dependencies.push(...processed.dependencies);
148150
if (!processed || !processed.map && processed.code === content) return no_change();
149151

150-
return processed_tag_to_sws(processed, tag_name, attributes, content,
151-
{...source, get_location: offset => source.get_location(offset + tag_offset)});
152+
return processed_tag_to_sws(processed, tag_name, attributes,
153+
{source: content, get_location: offset => source.get_location(offset + tag_offset), filename, file_basename});
152154
}
153155

154156
return {...await replace_in_code(tag_regex, process_single_tag, source), dependencies};
@@ -203,5 +205,5 @@ export default async function preprocess(
203205
result.update_source(await process_tag('style', preprocess, result));
204206
}
205207

206-
return result.processed();
208+
return result.to_processed();
207209
}

src/compiler/preprocess/replace_in_code.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getLocator } from 'locate-character';
22
import { StringWithSourcemap } from '../utils/string_with_sourcemap';
33

44
export interface Source {
5-
source: string;
5+
source: string;
66
get_location: ReturnType<typeof getLocator>;
77
file_basename: string;
88
filename: string;
@@ -25,11 +25,11 @@ function calculate_replacements(
2525
replacements.push(
2626
get_replacement(...match).then(
2727
replacement => {
28-
const matched_string = match[0];
29-
const offset = match[match.length-2];
28+
const matched_string = match[0];
29+
const offset = match[match.length-2];
3030

31-
return ({ offset, length: matched_string.length, replacement });
32-
}
31+
return ({ offset, length: matched_string.length, replacement });
32+
}
3333
)
3434
);
3535
return '';

0 commit comments

Comments
 (0)