From b74aeef922ff765392538a14e013f04f315f381a Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 3 May 2019 17:39:26 +0200 Subject: [PATCH] feat: Slightly refactor public API --- src/decoder.rs | 2 +- src/ram_bundle.rs | 2 +- src/types.rs | 32 +++++++++++++++++++++++++++----- tests/test_index.rs | 4 ++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index be38a1ef..5b19f2c1 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -256,7 +256,7 @@ fn decode_index(rsm: RawSourceMap) -> Result { _ => "".into(), }); - Ok(SourceMapIndex::new( + Ok(SourceMapIndex::new_ram_bundle_compatible( file, sections, rsm.x_facebook_offsets, diff --git a/src/ram_bundle.rs b/src/ram_bundle.rs index 71ad44cf..fc73fa91 100644 --- a/src/ram_bundle.rs +++ b/src/ram_bundle.rs @@ -334,7 +334,7 @@ fn test_basic_ram_bundle_split() -> std::result::Result<(), Box, sections: Vec, + ) -> SourceMapIndex { + SourceMapIndex { + file, + sections, + x_facebook_offsets: None, + x_metro_module_paths: None, + } + } + + /// Constructs a new sourcemap index from raw components including the + /// facebook RAM bundle extensions. + /// + /// - `file`: an optional filename of the index + /// - `sections`: a vector of source map index sections + /// - `x_facebook_offsets`: a vector of facebook offsets + /// - `x_metro_module_paths`: a vector of metro module paths + pub fn new_ram_bundle_compatible( + file: Option, + sections: Vec, x_facebook_offsets: Option>>, x_metro_module_paths: Option>, ) -> SourceMapIndex { @@ -905,16 +924,19 @@ impl SourceMapIndex { self.flatten()?.rewrite(options) } - pub fn is_for_react_native(&self) -> bool { + /// Returns `true` if this sourcemap is for a RAM bundle. + pub fn is_for_ram_bundle(&self) -> bool { self.x_facebook_offsets.is_some() && self.x_metro_module_paths.is_some() } - pub fn x_facebook_offsets(&self) -> Option<&Vec>> { - self.x_facebook_offsets.as_ref() + /// Returns embeded x-facebook-offset values. + pub fn x_facebook_offsets(&self) -> Option<&[Option]> { + self.x_facebook_offsets.as_ref().map(|x| &x[..]) } - pub fn x_metro_module_paths(&self) -> Option<&Vec> { - self.x_metro_module_paths.as_ref() + /// Returns embedded metro module paths. + pub fn x_metro_module_paths(&self) -> Option<&[String]> { + self.x_metro_module_paths.as_ref().map(|x| &x[..]) } } diff --git a/tests/test_index.rs b/tests/test_index.rs index 9b3c9851..39147045 100644 --- a/tests/test_index.rs +++ b/tests/test_index.rs @@ -51,7 +51,7 @@ fn test_basic_indexed_sourcemap() { files.insert("file2.js", f2.lines().collect()); let mut ism = SourceMapIndex::from_reader(input).unwrap(); - assert!(!ism.is_for_react_native()); + assert!(!ism.is_for_ram_bundle()); for section_id in 0..ism.get_section_count() { let section = ism.get_section_mut(section_id).unwrap(); @@ -135,5 +135,5 @@ fn test_indexed_sourcemap_for_react_native() { }"#; let ism = SourceMapIndex::from_reader(input).unwrap(); - assert!(ism.is_for_react_native()); + assert!(ism.is_for_ram_bundle()); }