Skip to content

Commit 09e5085

Browse files
committed
review feedback
1 parent a4d7c82 commit 09e5085

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

src/compiler/preprocess/index.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types
22
import { getLocator } from 'locate-character';
33
import { MappedCode, SourceLocation, sourcemap_add_offset, combine_sourcemaps } from '../utils/mapped_code';
44
import { decode_map } from './decode_sourcemap';
5-
import { replace_in_code, Source } from './replace_in_code';
6-
import { MarkupPreprocessor, Preprocessor, PreprocessorGroup, Processed } from './types';
5+
import { replace_in_code, slice_source } from './replace_in_code';
6+
import { MarkupPreprocessor, Source, Preprocessor, PreprocessorGroup, Processed } from './types';
77

88
interface SourceUpdate {
99
string?: string;
@@ -36,7 +36,7 @@ class PreprocessResult implements Source {
3636
}
3737

3838
update_source({ string: source, map, dependencies }: SourceUpdate) {
39-
if (source) {
39+
if (source != null) {
4040
this.source = source;
4141
this.get_location = getLocator(source);
4242
}
@@ -95,16 +95,18 @@ function processed_tag_to_code(
9595
processed: Processed,
9696
tag_name: 'style' | 'script',
9797
attributes: string,
98-
{ source, file_basename, get_location }: Source
98+
source: Source
9999
): MappedCode {
100-
const build_mapped_code = (source: string, offset: number) =>
101-
MappedCode.from_source(file_basename, source, get_location(offset));
100+
const { file_basename, get_location } = source;
101+
102+
const build_mapped_code = (code: string, offset: number) =>
103+
MappedCode.from_source(slice_source(code, offset, source));
102104

103105
const tag_open = `<${tag_name}${attributes || ''}>`;
104106
const tag_close = `</${tag_name}>`;
105107

106108
const tag_open_code = build_mapped_code(tag_open, 0);
107-
const tag_close_code = build_mapped_code(tag_close, tag_open.length + source.length);
109+
const tag_close_code = build_mapped_code(tag_close, tag_open.length + source.source.length);
108110

109111
const content_code = processed_content_to_code(processed, get_location(tag_open.length), file_basename);
110112

@@ -117,7 +119,8 @@ function parse_tag_attributes(str: string) {
117119
.split(/\s+/)
118120
.filter(Boolean)
119121
.reduce((attrs, attr) => {
120-
const [key, value] = attr.split('=');
122+
const i = attr.indexOf('=');
123+
const [key, value] = i > 0 ? [attr.slice(0, i), attr.slice(i+1)] : [attr];
121124
const [, unquoted] = (value && value.match(/^['"](.*)['"]$/)) || [];
122125

123126
return { ...attrs, [key]: unquoted ?? value ?? true };
@@ -132,7 +135,7 @@ async function process_tag(
132135
preprocessor: Preprocessor,
133136
source: Source
134137
): Promise<SourceUpdate> {
135-
const { file_basename, filename, get_location } = source;
138+
const { filename } = source;
136139
const tag_regex =
137140
tag_name === 'style'
138141
? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi
@@ -146,7 +149,7 @@ async function process_tag(
146149
content = '',
147150
tag_offset: number
148151
): Promise<MappedCode> {
149-
const no_change = () => MappedCode.from_source(file_basename, tag_with_content, get_location(tag_offset));
152+
const no_change = () => MappedCode.from_source(slice_source(tag_with_content, tag_offset, source));
150153

151154
if (!attributes && !content) return no_change();
152155

@@ -160,12 +163,7 @@ async function process_tag(
160163
if (processed.dependencies) dependencies.push(...processed.dependencies);
161164
if (!processed.map && processed.code === content) return no_change();
162165

163-
return processed_tag_to_code(processed, tag_name, attributes, {
164-
source: content,
165-
get_location: offset => source.get_location(offset + tag_offset),
166-
filename,
167-
file_basename
168-
});
166+
return processed_tag_to_code(processed, tag_name, attributes, slice_source(content, tag_offset, source));
169167
}
170168

171169
const { string, map } = await replace_in_code(tag_regex, process_single_tag, source);

src/compiler/preprocess/replace_in_code.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
import { getLocator } from 'locate-character';
21
import { MappedCode } from '../utils/mapped_code';
3-
4-
export interface Source {
5-
source: string;
6-
get_location: ReturnType<typeof getLocator>;
7-
file_basename: string;
8-
filename: string;
9-
}
2+
import { Source } from './types';
103

114
interface Replacement {
125
offset: number;
136
length: number;
147
replacement: MappedCode;
158
}
169

10+
export function slice_source(
11+
code_slice: string,
12+
offset: number,
13+
{ file_basename, filename, get_location }: Source
14+
): Source {
15+
return {
16+
source: code_slice,
17+
get_location: (index: number) => get_location(index + offset),
18+
file_basename,
19+
filename
20+
};
21+
}
22+
1723
function calculate_replacements(
1824
re: RegExp,
1925
get_replacement: (...match: any[]) => Promise<MappedCode>,
@@ -40,20 +46,20 @@ function calculate_replacements(
4046

4147
function perform_replacements(
4248
replacements: Replacement[],
43-
{ file_basename, source, get_location }: Source
49+
source: Source
4450
): MappedCode {
4551
const out = new MappedCode();
4652
let last_end = 0;
4753

4854
for (const { offset, length, replacement } of replacements) {
4955
const unchanged_prefix = MappedCode.from_source(
50-
file_basename, source.slice(last_end, offset), get_location(last_end));
56+
slice_source(source.source.slice(last_end, offset), last_end, source)
57+
);
5158
out.concat(unchanged_prefix).concat(replacement);
5259
last_end = offset + length;
5360
}
5461

55-
const unchanged_suffix = MappedCode.from_source(
56-
file_basename, source.slice(last_end), get_location(last_end));
62+
const unchanged_suffix = MappedCode.from_source(slice_source(source.source.slice(last_end), last_end, source));
5763

5864
return out.concat(unchanged_suffix);
5965
}

src/compiler/preprocess/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import { Location } from 'locate-character';
2+
3+
export interface Source {
4+
source: string;
5+
get_location: (search: number) => Location;
6+
file_basename: string;
7+
filename: string;
8+
}
9+
110
export interface Processed {
211
code: string;
312
map?: string | object; // we are opaque with the type here to avoid dependency on the remapping module for our public types.

src/compiler/utils/mapped_code.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DecodedSourceMap, RawSourceMap, SourceMapLoader } from '@ampproject/remapping/dist/types/types';
22
import remapping from '@ampproject/remapping';
33
import { SourceMap } from 'magic-string';
4+
import { Source } from '../preprocess/types';
45

56
export type SourceLocation = {
67
line: number;
@@ -170,7 +171,7 @@ export class MappedCode {
170171
const line_count = string.split('\n').length;
171172

172173
if (map) {
173-
// ensure that count of source map mappings lines
174+
// ensure that count of source map mappings lines
174175
// is equal to count of generated code lines
175176
// (some tools may produce less)
176177
const missing_lines = line_count - map.mappings.length;
@@ -188,11 +189,11 @@ export class MappedCode {
188189
return new MappedCode(string, map);
189190
}
190191

191-
static from_source(
192-
source_file: string, source: string, offset?: SourceLocation
193-
): MappedCode {
192+
static from_source({ source, file_basename, get_location }: Source): MappedCode {
193+
let offset: SourceLocation = get_location(0);
194+
194195
if (!offset) offset = { line: 0, column: 0 };
195-
const map: DecodedSourceMap = { version: 3, names: [], sources: [source_file], mappings: [] };
196+
const map: DecodedSourceMap = { version: 3, names: [], sources: [file_basename], mappings: [] };
196197
if (source == '') return new MappedCode(source, map);
197198

198199
// we create a high resolution identity map here,

0 commit comments

Comments
 (0)