pub struct SourceMapIndex { /* private fields */ }
Expand description

Represents a sourcemap index in memory

Implementations§

source§

impl SourceMapIndex

source

pub fn from_reader<R: Read>(rdr: R) -> Result<SourceMapIndex>

Creates a sourcemap index from a reader over a JSON stream in UTF-8 format. Optionally a “garbage header” as defined by the sourcemap draft specification is supported. In case a regular sourcemap is encountered an error is returned.

Examples found in repository?
examples/split_ram_bundle.rs (line 36)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<_> = env::args().collect();
    if args.len() < 4 {
        println!("{USAGE}");
        std::process::exit(1);
    }

    let bundle_path = Path::new(&args[1]);
    let ram_bundle = RamBundle::parse_indexed_from_path(bundle_path)
        .or_else(|_| RamBundle::parse_unbundle_from_path(bundle_path))?;

    match ram_bundle.bundle_type() {
        RamBundleType::Indexed => println!("Indexed RAM Bundle detected"),
        RamBundleType::Unbundle => println!("File RAM Bundle detected"),
    }

    let sourcemap_file = File::open(&args[2])?;
    let ism = SourceMapIndex::from_reader(sourcemap_file).unwrap();

    let output_directory = Path::new(&args[3]);
    if !output_directory.exists() {
        panic!("Directory {} does not exist!", output_directory.display());
    }

    println!(
        "Ouput directory: {}",
        output_directory.canonicalize()?.display()
    );
    let ram_bundle_iter = split_ram_bundle(&ram_bundle, &ism).unwrap();
    for result in ram_bundle_iter {
        let (name, sv, sm) = result.unwrap();
        println!("Writing down source: {name}");
        fs::write(output_directory.join(name.clone()), sv.source())?;

        let sourcemap_name = format!("{name}.map");
        println!("Writing down sourcemap: {sourcemap_name}");
        let out_sm = File::create(output_directory.join(sourcemap_name))?;
        sm.to_writer(out_sm)?;
    }
    println!("Done.");

    Ok(())
}
source

pub fn to_writer<W: Write>(&self, w: W) -> Result<()>

Writes a sourcemap index into a writer.

source

pub fn from_slice(slice: &[u8]) -> Result<SourceMapIndex>

Creates a sourcemap index from a reader over a JSON byte slice in UTF-8 format. Optionally a “garbage header” as defined by the sourcemap draft specification is supported. In case a regular sourcemap is encountered an error is returned.

source

pub fn new( file: Option<String>, sections: Vec<SourceMapSection> ) -> SourceMapIndex

Constructs a new sourcemap index from raw components.

  • file: an optional filename of the index
  • sections: a vector of source map index sections
source

pub fn new_ram_bundle_compatible( file: Option<String>, sections: Vec<SourceMapSection>, x_facebook_offsets: Option<Vec<Option<u32>>>, x_metro_module_paths: Option<Vec<String>> ) -> SourceMapIndex

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
source

pub fn get_file(&self) -> Option<&str>

Returns the embedded filename in case there is one.

source

pub fn set_file(&mut self, value: Option<&str>)

Sets a new value for the file.

source

pub fn get_section_count(&self) -> u32

Returns the number of sections in this index

source

pub fn get_section(&self, idx: u32) -> Option<&SourceMapSection>

Looks up a single section and returns it

source

pub fn get_section_mut(&mut self, idx: u32) -> Option<&mut SourceMapSection>

Looks up a single section and returns it as a mutable ref

source

pub fn sections(&self) -> SourceMapSectionIter<'_>

Iterates over all sections

source

pub fn get_original_function_name<'a>( &self, line: u32, col: u32, minified_name: &str, sv: &'a SourceView<'a> ) -> Option<&str>

Given a location, name and minified source file resolve a minified name to an original function name.

This invokes some guesswork and requires access to the original minified source. This will not yield proper results for anonymous functions or functions that do not have clear function names. (For instance it’s recommended that dotted function names are not passed to this function).

source

pub fn lookup_token(&self, line: u32, col: u32) -> Option<Token<'_>>

Looks up the closest token to a given line and column.

This requires that the referenced sourcemaps are actually loaded. If a sourcemap is encountered that is not embedded but just externally referenced it is silently skipped.

source

pub fn flatten(&self) -> Result<SourceMap>

Flattens an indexed sourcemap into a regular one. This requires that all referenced sourcemaps are attached.

source

pub fn flatten_and_rewrite( self, options: &RewriteOptions<'_> ) -> Result<SourceMap>

Flattens an indexed sourcemap into a regular one and automatically rewrites it. This is more useful than plain flattening as this will cause the sourcemap to be properly deduplicated.

Examples found in repository?
examples/read.rs (lines 11-14)
7
8
9
10
11
12
13
14
15
16
17
18
fn load_from_reader<R: Read>(mut rdr: R) -> SourceMap {
    match decode(&mut rdr).unwrap() {
        DecodedMap::Regular(sm) => sm,
        DecodedMap::Index(idx) => idx
            .flatten_and_rewrite(&RewriteOptions {
                load_local_source_contents: true,
                ..Default::default()
            })
            .unwrap(),
        _ => panic!("unexpected sourcemap format"),
    }
}
More examples
Hide additional examples
examples/rewrite.rs (lines 28-31)
24
25
26
27
28
29
30
31
32
33
34
35
fn load_from_reader<R: Read>(mut rdr: R) -> SourceMap {
    match decode(&mut rdr).unwrap() {
        DecodedMap::Regular(sm) => sm,
        DecodedMap::Index(idx) => idx
            .flatten_and_rewrite(&RewriteOptions {
                load_local_source_contents: true,
                ..Default::default()
            })
            .unwrap(),
        _ => panic!("unexpected sourcemap format"),
    }
}
source

pub fn is_for_ram_bundle(&self) -> bool

Returns true if this sourcemap is for a RAM bundle.

source

pub fn x_facebook_offsets(&self) -> Option<&[Option<u32>]>

Returns embeded x-facebook-offset values.

source

pub fn x_metro_module_paths(&self) -> Option<&[String]>

Returns embedded metro module paths.

Trait Implementations§

source§

impl Clone for SourceMapIndex

source§

fn clone(&self) -> SourceMapIndex

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SourceMapIndex

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.