Skip to content

Commit 0011123

Browse files
committed
minor tweaks
1 parent 90274b9 commit 0011123

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/compiler/preprocess/index.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ interface SourceUpdate {
1212
dependencies?: string[];
1313
}
1414

15+
/**
16+
* Represents intermediate states of the preprocessing.
17+
*/
1518
class PreprocessResult {
1619
// sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1)
1720
// so we use sourcemap_list.unshift() to add new maps
@@ -22,13 +25,13 @@ class PreprocessResult {
2225
get_location: ReturnType<typeof getLocator>;
2326

2427
constructor(public source: string, public filename: string) {
25-
this.update_source({string: source});
28+
this.update_source({ string: source });
2629
}
2730

28-
update_source({string: source, map, dependencies}: SourceUpdate) {
31+
update_source({ string: source, map, dependencies }: SourceUpdate) {
2932
this.source = source;
3033
this.get_location = getLocator(source);
31-
34+
3235
if (map) {
3336
this.sourcemap_list.unshift(map);
3437
}
@@ -38,12 +41,9 @@ class PreprocessResult {
3841
}
3942
}
4043

41-
processed(): Processed {
44+
to_processed(): Processed {
4245
// Combine all the source maps for each preprocessor function into one
43-
const map: RawSourceMap = combine_sourcemaps(
44-
this.filename,
45-
this.sourcemap_list
46-
);
46+
const map: RawSourceMap = combine_sourcemaps(this.filename, this.sourcemap_list);
4747

4848
return {
4949
// TODO return separated output, in future version where svelte.compile supports it:
@@ -53,7 +53,7 @@ class PreprocessResult {
5353

5454
code: this.source,
5555
dependencies: [...new Set(this.dependencies)],
56-
map: (map as object),
56+
map: map as object,
5757
toString: () => this.source
5858
};
5959
}
@@ -78,29 +78,31 @@ function processed_content_to_sws(
7878
}
7979

8080
/**
81-
* Convert the whole tag including content (replacing it with `processed`)
82-
* into a `StringWithSourcemap` representing the transformed code.
81+
* Given the whole tag including content, return a `StringWithSourcemap`
82+
* representing the tag content replaced with `processed`.
8383
*/
8484
function processed_tag_to_sws(
8585
processed: Processed,
8686
tag_name: 'style' | 'script',
8787
attributes: string,
88-
content: string,
89-
{ filename, get_location }: Source): StringWithSourcemap {
90-
const build_sws = (content: string, offset: number) =>
91-
StringWithSourcemap.from_source(filename, content, get_location(offset));
88+
{ source, filename, get_location }: Source): StringWithSourcemap {
89+
const build_sws = (source: string, offset: number) =>
90+
StringWithSourcemap.from_source(filename, source, get_location(offset));
9291

9392
const tag_open = `<${tag_name}${attributes || ''}>`;
9493
const tag_close = `</${tag_name}>`;
9594

9695
const tag_open_sws = build_sws(tag_open, 0);
97-
const tag_close_sws = build_sws(tag_close, tag_open.length + content.length);
96+
const tag_close_sws = build_sws(tag_close, tag_open.length + source.length);
9897

9998
const content_sws = processed_content_to_sws(processed, get_location(tag_open.length));
10099

101100
return tag_open_sws.concat(content_sws).concat(tag_close_sws);
102101
}
103102

103+
/**
104+
* Calculate the updates required to process all instances of the specified tag.
105+
*/
104106
async function process_tag(
105107
tag_name: 'style' | 'script',
106108
preprocessor: Preprocessor,
@@ -134,8 +136,8 @@ async function process_tag(
134136
if (!processed) return no_change();
135137
if (processed.dependencies) dependencies.push(...processed.dependencies);
136138

137-
return processed_tag_to_sws(processed, tag_name, attributes, content,
138-
{...source, get_location: offset => source.get_location(offset + tag_offset)});
139+
return processed_tag_to_sws(processed, tag_name, attributes,
140+
{source: content, get_location: offset => source.get_location(offset + tag_offset), filename});
139141
}
140142

141143
return {...await replace_in_code(tag_regex, process_single_tag, source), dependencies};
@@ -190,5 +192,5 @@ export default async function preprocess(
190192
result.update_source(await process_tag('style', preprocess, result));
191193
}
192194

193-
return result.processed();
195+
return result.to_processed();
194196
}

src/compiler/preprocess/replace_in_code.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ 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>;
7-
filename: string;
7+
filename: string;
88
}
99

1010
interface Replacement {
@@ -24,11 +24,11 @@ function calculate_replacements(
2424
replacements.push(
2525
get_replacement(...match).then(
2626
replacement => {
27-
const matched_string = match[0];
28-
const offset = match[match.length-2];
27+
const matched_string = match[0];
28+
const offset = match[match.length-2];
2929

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

0 commit comments

Comments
 (0)