Skip to content

Commit 9733996

Browse files
authored
feat: support SourceMapOptions#hires (#14)
1 parent 705fa80 commit 9733996

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/magic_string/source_map.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ use crate::{locator::Locator, sourcemap_builder::SourcemapBuilder, MagicString};
66
pub struct SourceMapOptions {
77
pub include_content: bool,
88
pub source: Arc<str>,
9+
pub hires: bool
910
}
1011

1112
impl Default for SourceMapOptions {
1213
fn default() -> Self {
1314
Self {
1415
include_content: false,
1516
source: "".into(),
17+
hires: false
1618
}
1719
}
1820
}
1921

2022
impl<'s> MagicString<'s> {
2123
pub fn source_map(&self, opts: SourceMapOptions) -> oxc::sourcemap::SourceMap {
22-
let mut source_builder = SourcemapBuilder::new();
24+
let mut source_builder = SourcemapBuilder::new(opts.hires);
2325

2426
source_builder.set_source_and_content(&opts.source, &self.source);
2527

src/sourcemap_builder.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{chunk::Chunk, locator::Locator, TextSize};
22

33
pub struct SourcemapBuilder {
4+
hires: bool,
45
generated_code_line: TextSize,
56
/// `generated_code_column` is calculated based on utf-16.
67
generated_code_column: TextSize,
@@ -9,8 +10,9 @@ pub struct SourcemapBuilder {
910
}
1011

1112
impl SourcemapBuilder {
12-
pub fn new() -> Self {
13+
pub fn new(hires: bool) -> Self {
1314
Self {
15+
hires,
1416
generated_code_line: 0,
1517
generated_code_column: 0,
1618
source_id: 0,
@@ -51,28 +53,28 @@ impl SourcemapBuilder {
5153
let chunk_content = chunk.span.text(source);
5254
let mut new_line = true;
5355
for char in chunk_content.chars() {
56+
// TODO support hires boundary
57+
if new_line || self.hires {
58+
self.source_map_builder.add_token(
59+
self.generated_code_line,
60+
self.generated_code_column,
61+
loc.line,
62+
loc.column,
63+
Some(self.source_id),
64+
name_id,
65+
);
66+
}
5467
match char {
5568
'\n' => {
5669
loc.bump_line();
5770
self.bump_line();
5871
new_line = true;
5972
}
6073
_ => {
61-
if new_line {
62-
new_line = false;
63-
self.source_map_builder.add_token(
64-
self.generated_code_line,
65-
self.generated_code_column,
66-
loc.line,
67-
loc.column,
68-
Some(self.source_id),
69-
name_id,
70-
);
71-
}
72-
7374
let char_utf16_len = char.len_utf16() as u32;
7475
loc.column += char_utf16_len;
7576
self.generated_code_column += char_utf16_len;
77+
new_line = false;
7678
}
7779
}
7880
}

tests/magic_string_source_map.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ fn basic() {
3636
sm.to_json_string().unwrap(),
3737
"{\"version\":3,\"names\":[\"d\",\"v\",\"div\"],\"sources\":[\"\"],\"sourcesContent\":[\"<div>\\n hello, world\\n</div>\"],\"mappings\":\";AAAA,CAACA,CAAC,CAACC,CAAC;AACJ;AACA,EAAEC,EAAG\"}"
3838
);
39+
40+
let sm = s.source_map(SourceMapOptions {
41+
include_content: true,
42+
hires: true,
43+
..Default::default()
44+
});
45+
assert_eq!(
46+
sm.to_json_string().unwrap(),
47+
"{\"version\":3,\"names\":[\"d\",\"v\",\"div\"],\"sources\":[\"\"],\"sourcesContent\":[\"<div>\\n hello, world\\n</div>\"],\"mappings\":\";AAAA,CAACA,CAAC,CAACC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACd,CAAC,CAACC,EAAG\"}"
48+
);
3949
}

0 commit comments

Comments
 (0)