From 3a15a92aae3edb38f347bd9f813fc63825d25500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Wed, 28 Sep 2022 16:59:18 +0200 Subject: [PATCH 1/2] feat: Add source_root handling support --- src/builder.rs | 24 ++++++++++++++++++++---- src/decoder.rs | 11 ++++------- src/encoder.rs | 3 +-- src/types.rs | 20 ++++++++++++++++---- tests/test_builder.rs | 19 +++++++++++++++++++ 5 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 tests/test_builder.rs diff --git a/src/builder.rs b/src/builder.rs index 02eae5a5..ea1fefbc 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -23,6 +23,7 @@ pub struct SourceMapBuilder { names: Vec, tokens: Vec, source_map: HashMap, + source_root: Option, sources: Vec, source_contents: Vec>, } @@ -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>(&mut self, value: Option) { + 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>(&mut self, value: Option) { + 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. @@ -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.as_deref()); + + sm } } diff --git a/src/decoder.rs b/src/decoder.rs index 854bd25d..3c010d81 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -227,13 +227,10 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { _ => "".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 { diff --git a/src/encoder.rs b/src/encoder.rs index ac8942db..919b9f57 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -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()), diff --git a/src/types.rs b/src/types.rs index 0433f72d..504592ba 100644 --- a/src/types.rs +++ b/src/types.rs @@ -458,6 +458,7 @@ pub struct SourceMap { tokens: Vec, index: Vec<(u32, u32, u32)>, names: Vec, + source_root: Option, sources: Vec, sources_content: Vec>>, } @@ -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. /// @@ -561,6 +562,7 @@ impl SourceMap { tokens, index, names, + source_root: None, sources, sources_content: sources_content .unwrap_or_default() @@ -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>(&mut self, value: Option) { + 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>(&mut self, value: Option) { + self.source_root = value.map(Into::into); } /// Looks up a token by its index. diff --git a/tests/test_builder.rs b/tests/test_builder.rs new file mode 100644 index 00000000..8fbfbe8e --- /dev/null +++ b/tests/test_builder.rs @@ -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 = vec![]; + sm.to_writer(&mut output).unwrap(); + assert_eq!(output, expected); +} From 1a2c85d4388afe293ac964d4310dc891c0a684f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Wed, 28 Sep 2022 17:47:10 +0200 Subject: [PATCH 2/2] Remove as_deref() --- src/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index ea1fefbc..888e2baf 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -258,7 +258,7 @@ impl SourceMapBuilder { }; let mut sm = SourceMap::new(self.file, self.tokens, self.names, self.sources, contents); - sm.set_source_root(self.source_root.as_deref()); + sm.set_source_root(self.source_root); sm }