Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ itertools = "0.13"
codspeed-criterion-compat = { version = "2.7.2", default-features = false, optional = true }
static_assertions = "1.1.0"
simd-json = "0.14.3"
self_cell = "1.2.0"

[dev-dependencies]
twox-hash = "2.1.0"
Expand Down
5 changes: 1 addition & 4 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use rspack_sources::{

use bench_complex_replace_source::benchmark_complex_replace_source;
use bench_source_map::{
benchmark_parse_source_map_from_json, benchmark_source_map_clone,
benchmark_stringify_source_map_to_json,
benchmark_parse_source_map_from_json, benchmark_stringify_source_map_to_json,
};

const HELLOWORLD_JS: &str = include_str!(concat!(
Expand Down Expand Up @@ -253,8 +252,6 @@ fn bench_rspack_sources(criterion: &mut Criterion) {
benchmark_parse_source_map_from_json,
);

group.bench_function("source_map_clone", benchmark_source_map_clone);

group.bench_function(
"stringify_source_map_to_json",
benchmark_stringify_source_map_to_json,
Expand Down
7 changes: 0 additions & 7 deletions benches/bench_source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ pub fn benchmark_parse_source_map_from_json(b: &mut Bencher) {
})
}

pub fn benchmark_source_map_clone(b: &mut Bencher) {
let source = SourceMap::from_json(ANTD_MIN_JS_MAP).unwrap();
b.iter(|| {
let _ = black_box(source.clone());
})
}

pub fn benchmark_stringify_source_map_to_json(b: &mut Bencher) {
let source = SourceMap::from_json(ANTD_MIN_JS_MAP).unwrap();
b.iter(|| {
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ pub enum Error {
BadJson(simd_json::Error),
/// rope related failure
Rope(&'static str),
/// IO related failure
IO(std::io::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::BadJson(err) => write!(f, "bad json: {err}"),
Error::Rope(err) => write!(f, "rope error: {err}"),
Error::IO(err) => write!(f, "io error: {err}"),
}
}
}
Expand All @@ -28,3 +31,9 @@ impl From<simd_json::Error> for Error {
Error::BadJson(err)
}
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::IO(err)
}
}
18 changes: 9 additions & 9 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::{
type InnerSourceContentLine<'a, 'b> =
RefCell<LinearMap<OnceCell<Option<Vec<WithIndices<'a, Rope<'b>>>>>>>;

pub fn get_map<'a, S: StreamChunks>(
stream: &'a S,
options: &'a MapOptions,
pub fn get_map<S: StreamChunks>(
stream: &S,
options: &MapOptions,
) -> Option<SourceMap> {
let mut mappings_encoder = create_encoder(options.columns);
let mut sources: Vec<String> = Vec::new();
Expand Down Expand Up @@ -367,14 +367,14 @@ where
if result.generated_line == 1 && result.generated_column == 0 {
return result;
}
for (i, source) in source_map.sources().iter().enumerate() {
for (i, source) in source_map.sources().enumerate() {
on_source(
i as u32,
get_source(source_map, source),
source_map.get_source_content(i).map(Rope::from),
)
}
for (i, name) in source_map.names().iter().enumerate() {
for (i, name) in source_map.names().enumerate() {
on_name(i as u32, Cow::Borrowed(name));
}
let mut mapping_active_line = 0;
Expand Down Expand Up @@ -431,14 +431,14 @@ where
generated_column: 0,
};
}
for (i, source) in source_map.sources().iter().enumerate() {
for (i, source) in source_map.sources().enumerate() {
on_source(
i as u32,
get_source(source_map, source),
source_map.get_source_content(i).map(Rope::from),
)
}
for (i, name) in source_map.names().iter().enumerate() {
for (i, name) in source_map.names().enumerate() {
on_name(i as u32, Cow::Borrowed(name));
}
let last_line =
Expand Down Expand Up @@ -581,7 +581,7 @@ where
generated_column: 0,
};
}
for (i, source) in source_map.sources().iter().enumerate() {
for (i, source) in source_map.sources().enumerate() {
on_source(
i as u32,
get_source(source_map, source),
Expand Down Expand Up @@ -629,7 +629,7 @@ where
generated_column: 0,
};
}
for (i, source) in source_map.sources().iter().enumerate() {
for (i, source) in source_map.sources().enumerate() {
on_source(
i as u32,
get_source(source_map, source),
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod raw_source;
mod replace_source;
mod rope;
mod source;
mod source_map;
mod source_map_source;
mod with_indices;

Expand All @@ -24,8 +25,8 @@ pub use replace_source::{ReplaceSource, ReplacementEnforce};
pub use rope::Rope;
pub use source::{
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
SourceMap,
};
pub use source_map::SourceMap;
pub use source_map_source::{
SourceMapSource, SourceMapSourceOptions, WithoutOriginalOptions,
};
Expand Down
14 changes: 10 additions & 4 deletions src/original_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,20 @@ mod tests {
let result_list_map = source.map(&MapOptions::new(false)).unwrap();

assert_eq!(result_text, "Line1\n\nLine3\n");
assert_eq!(result_map.sources(), &["file.js".to_string()]);
assert_eq!(result_list_map.sources(), ["file.js".to_string()]);
assert_eq!(
result_map.sources_content(),
result_map.sources().collect::<Vec<_>>(),
["file.js".to_string()]
);
assert_eq!(
result_list_map.sources().collect::<Vec<_>>(),
["file.js".to_string()]
);
assert_eq!(
result_map.sources_content().collect::<Vec<_>>(),
["Line1\n\nLine3\n".to_string()],
);
assert_eq!(
result_list_map.sources_content(),
result_list_map.sources_content().collect::<Vec<_>>(),
["Line1\n\nLine3\n".to_string()],
);
assert_eq!(result_map.mappings(), "AAAA;;AAEA");
Expand Down
6 changes: 3 additions & 3 deletions src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
self.source().len()
}

fn map(&self, options: &crate::MapOptions) -> Option<SourceMap> {
fn map(&self, options: &MapOptions) -> Option<SourceMap> {
let replacements = &self.replacements;
if replacements.is_empty() {
return self.inner.map(options);
Expand Down Expand Up @@ -1043,15 +1043,15 @@ Line 2"#

#[test]
fn should_allow_replacements_at_the_start() {
let map = SourceMap::from_slice(
let map = SourceMap::from_json(
r#"{
"version":3,
"sources":["abc"],
"names":["StaticPage","data","foo"],
"mappings":";;AAAA,eAAe,SAASA,UAAT,OAA8B;AAAA,MAARC,IAAQ,QAARA,IAAQ;AAC3C,sBAAO;AAAA,cAAMA,IAAI,CAACC;AAAX,IAAP;AACD",
"sourcesContent":["export default function StaticPage({ data }) {\nreturn <div>{data.foo}</div>\n}\n"],
"file":"x"
}"#.as_bytes(),
}"#.to_string(),
).unwrap();

let code = r#"import { jsx as _jsx } from "react/jsx-runtime";
Expand Down
Loading