pub struct SourceMap { /* private fields */ }
Expand description
Represents a sourcemap in memory
This is always represents a regular “non-indexed” sourcemap. Particularly
in case the from_reader
method is used an index sourcemap will be
rejected with an error on reading.
Implementations§
source§impl SourceMap
impl SourceMap
sourcepub fn from_reader<R: Read>(rdr: R) -> Result<SourceMap>
pub fn from_reader<R: Read>(rdr: R) -> Result<SourceMap>
Creates a sourcemap 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 an indexed sourcemap is encountered an error is returned.
use sourcemap::SourceMap;
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let sm = SourceMap::from_reader(input).unwrap();
While sourcemaps objects permit some modifications, it’s generally
not possible to modify tokens after they have been added. For
creating sourcemaps from scratch or for general operations for
modifying a sourcemap have a look at the SourceMapBuilder
.
sourcepub fn to_writer<W: Write>(&self, w: W) -> Result<()>
pub fn to_writer<W: Write>(&self, w: W) -> Result<()>
Writes a sourcemap into a writer.
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
be set as upon parsing of the sourcemap the sources will already be
expanded.
let sm = SourceMap::from_reader(input).unwrap();
let mut output : Vec<u8> = vec![];
sm.to_writer(&mut output).unwrap();
sourcepub fn to_data_url(&self) -> Result<String>
pub fn to_data_url(&self) -> Result<String>
Encode a sourcemap into a data url.
let sm = SourceMap::from_reader(input).unwrap();
sm.to_data_url().unwrap();
sourcepub fn from_slice(slice: &[u8]) -> Result<SourceMap>
pub fn from_slice(slice: &[u8]) -> Result<SourceMap>
Creates a sourcemap 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 an indexed sourcemap is encountered an error is returned.
use sourcemap::SourceMap;
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let sm = SourceMap::from_slice(input).unwrap();
sourcepub fn new(
file: Option<Arc<str>>,
tokens: Vec<RawToken>,
names: Vec<Arc<str>>,
sources: Vec<Arc<str>>,
sources_content: Option<Vec<Option<Arc<str>>>>
) -> SourceMap
pub fn new( file: Option<Arc<str>>, tokens: Vec<RawToken>, names: Vec<Arc<str>>, sources: Vec<Arc<str>>, sources_content: Option<Vec<Option<Arc<str>>>> ) -> SourceMap
Constructs a new sourcemap from raw components.
file
: an optional filename of the sourcemaptokens
: a list of raw tokensnames
: a vector of namessources
a vector of source filenamessources_content
optional source contents
sourcepub fn get_debug_id(&self) -> Option<DebugId>
pub fn get_debug_id(&self) -> Option<DebugId>
Returns the embedded debug id.
sourcepub fn set_debug_id(&mut self, debug_id: Option<DebugId>)
pub fn set_debug_id(&mut self, debug_id: Option<DebugId>)
Sets a new value for the debug id.
sourcepub fn get_source_root(&self) -> Option<&str>
pub fn get_source_root(&self) -> Option<&str>
Returns the embedded source_root in case there is one.
sourcepub fn set_source_root<T: Into<Arc<str>>>(&mut self, value: Option<T>)
pub fn set_source_root<T: Into<Arc<str>>>(&mut self, value: Option<T>)
Sets a new value for the source_root.
sourcepub fn get_token_count(&self) -> u32
pub fn get_token_count(&self) -> u32
Returns the number of tokens in the sourcemap.
sourcepub fn lookup_token(&self, line: u32, col: u32) -> Option<Token<'_>>
pub fn lookup_token(&self, line: u32, col: u32) -> Option<Token<'_>>
Looks up the closest token to a given 0-indexed line and column.
sourcepub fn get_original_function_name(
&self,
line: u32,
col: u32,
minified_name: &str,
sv: &SourceView
) -> Option<&str>
pub fn get_original_function_name( &self, line: u32, col: u32, minified_name: &str, sv: &SourceView ) -> 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).
sourcepub fn get_source_count(&self) -> u32
pub fn get_source_count(&self) -> u32
Returns the number of sources in the sourcemap.
sourcepub fn get_source(&self, idx: u32) -> Option<&str>
pub fn get_source(&self, idx: u32) -> Option<&str>
Looks up a source for a specific index.
sourcepub fn set_source(&mut self, idx: u32, value: &str)
pub fn set_source(&mut self, idx: u32, value: &str)
Sets a new source value for an index. This cannot add new sources.
This panics if a source is set that does not exist.
sourcepub fn sources(&self) -> SourceIter<'_> ⓘ
pub fn sources(&self) -> SourceIter<'_> ⓘ
Iterates over all sources
sourcepub fn get_source_view(&self, idx: u32) -> Option<&SourceView>
pub fn get_source_view(&self, idx: u32) -> Option<&SourceView>
Returns the sources content as source view.
sourcepub fn get_source_contents(&self, idx: u32) -> Option<&str>
pub fn get_source_contents(&self, idx: u32) -> Option<&str>
Looks up the content for a source.
sourcepub fn set_source_contents(&mut self, idx: u32, value: Option<&str>)
pub fn set_source_contents(&mut self, idx: u32, value: Option<&str>)
Sets source contents for a source.
sourcepub fn source_contents(&self) -> SourceContentsIter<'_> ⓘ
pub fn source_contents(&self) -> SourceContentsIter<'_> ⓘ
Iterates over all source contents
sourcepub fn get_name_count(&self) -> u32
pub fn get_name_count(&self) -> u32
Returns the number of names in the sourcemap.
sourcepub fn remove_names(&mut self)
pub fn remove_names(&mut self)
Removes all names from the sourcemap.
sourcepub fn get_index_size(&self) -> usize
pub fn get_index_size(&self) -> usize
Returns the number of items in the index
sourcepub fn index_iter(&self) -> IndexIter<'_> ⓘ
pub fn index_iter(&self) -> IndexIter<'_> ⓘ
Returns the number of items in the index
sourcepub fn rewrite(self, options: &RewriteOptions<'_>) -> Result<SourceMap>
pub fn rewrite(self, options: &RewriteOptions<'_>) -> Result<SourceMap>
This rewrites the sourcemap according to the provided rewrite options.
The default behavior is to just deduplicate the sourcemap, something that automatically takes place. This for instance can be used to slightly compress sourcemaps if certain data is not wanted.
use sourcemap::{SourceMap, RewriteOptions};
let sm = SourceMap::from_slice(input).unwrap();
let new_sm = sm.rewrite(&RewriteOptions {
with_names: false,
..Default::default()
});
sourcepub fn adjust_mappings(&mut self, adjustment: &Self)
pub fn adjust_mappings(&mut self, adjustment: &Self)
Adjusts the mappings in self
using the mappings in adjustment
.
Here is the intended use case for this function:
- You have a source file (for example, minified JS)
foo.js
and a corresponding sourcemapfoo.js.map
. - You modify
foo.js
in some way and generate a sourcemaptransform.js.map
representing this modification. This can be done usingmagic-string
, for example. - You want a sourcemap that is “like”
foo.js.map
, but takes the changes you made tofoo.js
into account. Thenfoo.js.map.adjust_mappings(transform.js.map)
is the desired sourcemap.
This function assumes that adjustment
contains no relevant information except for mappings.
All information about sources and names is copied from self
.
Note that the resulting sourcemap will be at most as fine-grained as self.
.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SourceMap
impl RefUnwindSafe for SourceMap
impl Send for SourceMap
impl Sync for SourceMap
impl Unpin for SourceMap
impl UnwindSafe for SourceMap
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.