Skip to content

Commit 2ec0078

Browse files
authored
feat: Slightly refactor public API (#17)
1 parent 56c9c17 commit 2ec0078

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn decode_index(rsm: RawSourceMap) -> Result<SourceMapIndex> {
256256
_ => "<invalid>".into(),
257257
});
258258

259-
Ok(SourceMapIndex::new(
259+
Ok(SourceMapIndex::new_ram_bundle_compatible(
260260
file,
261261
sections,
262262
rsm.x_facebook_offsets,

src/ram_bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ fn test_basic_ram_bundle_split() -> std::result::Result<(), Box<std::error::Erro
334334
let sourcemap_file = File::open("./tests/fixtures/ram_bundle/basic.jsbundle.map")?;
335335
let ism = SourceMapIndex::from_reader(sourcemap_file)?;
336336

337-
assert!(ism.is_for_react_native());
337+
assert!(ism.is_for_ram_bundle());
338338

339339
let x_facebook_offsets = ism.x_facebook_offsets().unwrap();
340340
assert_eq!(x_facebook_offsets.len(), 5);

src/types.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,25 @@ impl SourceMapIndex {
791791
pub fn new(
792792
file: Option<String>,
793793
sections: Vec<SourceMapSection>,
794+
) -> SourceMapIndex {
795+
SourceMapIndex {
796+
file,
797+
sections,
798+
x_facebook_offsets: None,
799+
x_metro_module_paths: None,
800+
}
801+
}
802+
803+
/// Constructs a new sourcemap index from raw components including the
804+
/// facebook RAM bundle extensions.
805+
///
806+
/// - `file`: an optional filename of the index
807+
/// - `sections`: a vector of source map index sections
808+
/// - `x_facebook_offsets`: a vector of facebook offsets
809+
/// - `x_metro_module_paths`: a vector of metro module paths
810+
pub fn new_ram_bundle_compatible(
811+
file: Option<String>,
812+
sections: Vec<SourceMapSection>,
794813
x_facebook_offsets: Option<Vec<Option<u32>>>,
795814
x_metro_module_paths: Option<Vec<String>>,
796815
) -> SourceMapIndex {
@@ -905,16 +924,19 @@ impl SourceMapIndex {
905924
self.flatten()?.rewrite(options)
906925
}
907926

908-
pub fn is_for_react_native(&self) -> bool {
927+
/// Returns `true` if this sourcemap is for a RAM bundle.
928+
pub fn is_for_ram_bundle(&self) -> bool {
909929
self.x_facebook_offsets.is_some() && self.x_metro_module_paths.is_some()
910930
}
911931

912-
pub fn x_facebook_offsets(&self) -> Option<&Vec<Option<u32>>> {
913-
self.x_facebook_offsets.as_ref()
932+
/// Returns embeded x-facebook-offset values.
933+
pub fn x_facebook_offsets(&self) -> Option<&[Option<u32>]> {
934+
self.x_facebook_offsets.as_ref().map(|x| &x[..])
914935
}
915936

916-
pub fn x_metro_module_paths(&self) -> Option<&Vec<String>> {
917-
self.x_metro_module_paths.as_ref()
937+
/// Returns embedded metro module paths.
938+
pub fn x_metro_module_paths(&self) -> Option<&[String]> {
939+
self.x_metro_module_paths.as_ref().map(|x| &x[..])
918940
}
919941
}
920942

tests/test_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn test_basic_indexed_sourcemap() {
5151
files.insert("file2.js", f2.lines().collect());
5252

5353
let mut ism = SourceMapIndex::from_reader(input).unwrap();
54-
assert!(!ism.is_for_react_native());
54+
assert!(!ism.is_for_ram_bundle());
5555

5656
for section_id in 0..ism.get_section_count() {
5757
let section = ism.get_section_mut(section_id).unwrap();
@@ -135,5 +135,5 @@ fn test_indexed_sourcemap_for_react_native() {
135135
}"#;
136136

137137
let ism = SourceMapIndex::from_reader(input).unwrap();
138-
assert!(ism.is_for_react_native());
138+
assert!(ism.is_for_ram_bundle());
139139
}

0 commit comments

Comments
 (0)