Skip to content
Merged
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
24 changes: 20 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct SourceMapBuilder {
names: Vec<String>,
tokens: Vec<RawToken>,
source_map: HashMap<String, u32>,
source_root: Option<String>,
sources: Vec<String>,
source_contents: Vec<Option<String>>,
}
Expand Down Expand Up @@ -53,19 +54,30 @@ impl SourceMapBuilder {
names: vec![],
tokens: vec![],
source_map: HashMap::new(),
source_root: None,
sources: vec![],
source_contents: vec![],
}
}

/// Sets the file for the sourcemap (optional)
pub fn set_file(&mut self, value: Option<&str>) {
self.file = value.map(str::to_owned);
pub fn set_file<T: Into<String>>(&mut self, value: Option<T>) {
self.file = value.map(Into::into);
}

/// Returns the currently set file.
pub fn get_file(&self) -> Option<&str> {
self.file.as_ref().map(|x| &x[..])
self.file.as_deref()
}

/// Sets a new value for the source_root.
pub fn set_source_root<T: Into<String>>(&mut self, value: Option<T>) {
self.source_root = value.map(Into::into);
}

/// Returns the embedded source_root in case there is one.
pub fn get_source_root(&self) -> Option<&str> {
self.source_root.as_deref()
}

/// Registers a new source with the builder and returns the source ID.
Expand Down Expand Up @@ -244,6 +256,10 @@ impl SourceMapBuilder {
} else {
None
};
SourceMap::new(self.file, self.tokens, self.names, self.sources, contents)

let mut sm = SourceMap::new(self.file, self.tokens, self.names, self.sources, contents);
sm.set_source_root(self.source_root);

sm
}
}
11 changes: 4 additions & 7 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,10 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {
_ => "<invalid>".into(),
});

Ok(SourceMap::new(
file,
tokens,
names,
sources,
rsm.sources_content,
))
let mut sm = SourceMap::new(file, tokens, names, sources, rsm.sources_content);
sm.set_source_root(rsm.source_root);

Ok(sm)
}

fn decode_index(rsm: RawSourceMap) -> Result<SourceMapIndex> {
Expand Down
3 changes: 1 addition & 2 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ impl Encodable for SourceMap {
version: Some(3),
file: self.get_file().map(|x| Value::String(x.to_string())),
sources: Some(self.sources().map(|x| Some(x.to_string())).collect()),
// XXX: consider setting this to common root
source_root: None,
source_root: self.get_source_root().map(|x| x.to_string()),
sources_content: if have_contents { Some(contents) } else { None },
sections: None,
names: Some(self.names().map(|x| Value::String(x.to_string())).collect()),
Expand Down
20 changes: 16 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ pub struct SourceMap {
tokens: Vec<RawToken>,
index: Vec<(u32, u32, u32)>,
names: Vec<String>,
source_root: Option<String>,
sources: Vec<String>,
sources_content: Vec<Option<SourceView<'static>>>,
}
Expand Down Expand Up @@ -494,7 +495,7 @@ impl SourceMap {
///
/// Note that this operation will generate an equivalent sourcemap to the
/// one that was generated on load however there might be small differences
/// in the generated JSON and layout. For instance `sourceRoot` will not
/// in the generated JSON and layout. For instance `sourceRoot` will not
/// be set as upon parsing of the sourcemap the sources will already be
/// expanded.
///
Expand Down Expand Up @@ -561,6 +562,7 @@ impl SourceMap {
tokens,
index,
names,
source_root: None,
sources,
sources_content: sources_content
.unwrap_or_default()
Expand All @@ -572,12 +574,22 @@ impl SourceMap {

/// Returns the embedded filename in case there is one.
pub fn get_file(&self) -> Option<&str> {
self.file.as_ref().map(|x| &x[..])
self.file.as_deref()
}

/// Sets a new value for the file.
pub fn set_file(&mut self, value: Option<&str>) {
self.file = value.map(str::to_owned);
pub fn set_file<T: Into<String>>(&mut self, value: Option<T>) {
self.file = value.map(Into::into);
}

/// Returns the embedded source_root in case there is one.
pub fn get_source_root(&self) -> Option<&str> {
self.source_root.as_deref()
}

/// Sets a new value for the source_root.
pub fn set_source_root<T: Into<String>>(&mut self, value: Option<T>) {
self.source_root = value.map(Into::into);
}

/// Looks up a token by its index.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use sourcemap::SourceMapBuilder;

#[test]
fn test_builder_into_sourcemap() {
let mut builder = SourceMapBuilder::new(None);
builder.set_source_root(Some("/foo/bar"));
builder.add_source("baz.js");
builder.add_name("x");

let sm = builder.into_sourcemap();
assert_eq!(sm.get_source_root(), Some("/foo/bar"));
assert_eq!(sm.get_source(0), Some("baz.js"));
assert_eq!(sm.get_name(0), Some("x"));

let expected = b"{\"version\":3,\"sources\":[\"baz.js\"],\"sourceRoot\":\"/foo/bar\",\"names\":[\"x\"],\"mappings\":\"\"}";
let mut output: Vec<u8> = vec![];
sm.to_writer(&mut output).unwrap();
assert_eq!(output, expected);
}