Struct sourcemap::SourceMap
[−]
[src]
pub struct SourceMap { /* fields omitted */ }
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.
Methods
impl SourceMap
[src]
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
.
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();
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();
fn new(file: Option<String>,
tokens: Vec<RawToken>,
names: Vec<String>,
sources: Vec<String>,
sources_content: Option<Vec<Option<String>>>)
-> SourceMap
tokens: Vec<RawToken>,
names: Vec<String>,
sources: Vec<String>,
sources_content: Option<Vec<Option<String>>>)
-> 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
fn get_file(&self) -> Option<&str>
Returns the embedded filename in case there is one.
fn set_file(&mut self, value: Option<&str>)
Sets a new value for the file.
fn get_token<'a>(&'a self, idx: u32) -> Option<Token<'a>>
Looks up a token by its index.
fn get_token_count(&self) -> u32
Returns the number of tokens in the sourcemap.
fn tokens<'a>(&'a self) -> TokenIter<'a>
Returns an iterator over the tokens.
fn lookup_token<'a>(&'a self, line: u32, col: u32) -> Option<Token<'a>>
Looks up the closest token to a given line and column.
fn get_source_count(&self) -> u32
Returns the number of sources in the sourcemap.
fn get_source(&self, idx: u32) -> Option<&str>
Looks up a source for a specific index.
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.
fn sources<'a>(&'a self) -> SourceIter<'a>
Iterates over all sources
fn get_source_contents(&self, idx: u32) -> Option<&str>
Looks up the content for a source.
fn set_source_contents(&mut self, idx: u32, value: Option<&str>)
Sets source contents for a source.
fn source_contents<'a>(&'a self) -> SourceContentsIter<'a>
Iterates over all source contents
fn names<'a>(&'a self) -> NameIter<'a>
Returns an iterator over the names.
fn get_name_count(&self) -> u32
Returns the number of names in the sourcemap.
fn has_names(&self) -> bool
Returns true if there are any names in the map.
fn get_name(&self, idx: u32) -> Option<&str>
Looks up a name for a specific index.
fn remove_names(&mut self)
Removes all names from the sourcemap.
fn get_index_size(&self) -> usize
Returns the number of items in the index
fn index_iter<'a>(&'a self) -> IndexIter<'a>
Returns the number of items in the index
fn rewrite(self, options: &RewriteOptions) -> Result<SourceMap>
This rewrites the sourcemap accoridng 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() });