pub struct SourceView { /* private fields */ }Expand description
Efficient view into a JavaScript source string with lazy line indexing.
Stores the source as Arc<str> for cheap cloning. Line boundaries are
computed on first access and cached with OnceLock for lock-free
concurrent reads.
§UTF-16 column support
JavaScript source maps use UTF-16 code-unit offsets for columns. Methods
like get_line_slice accept UTF-16 columns
and convert them to byte offsets internally.
Implementations§
Source§impl SourceView
impl SourceView
Sourcepub fn from_string(source: String) -> Self
pub fn from_string(source: String) -> Self
Create a new SourceView from an owned String.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Return the number of lines in the source.
A trailing newline does NOT produce an extra empty line
(e.g. "a\n" has 1 line, "a\nb" has 2 lines).
Sourcepub fn get_line(&self, idx: u32) -> Option<&str>
pub fn get_line(&self, idx: u32) -> Option<&str>
Get a specific line by 0-based index.
Returns None if idx is out of bounds. The returned slice does NOT
include the line terminator.
Sourcepub fn get_line_slice(&self, line: u32, col: u32, span: u32) -> Option<&str>
pub fn get_line_slice(&self, line: u32, col: u32, span: u32) -> Option<&str>
Get a substring of a line using UTF-16 column offsets.
line and col are 0-based. span is the number of UTF-16 code units
to include. Returns None if the line index is out of bounds or the
column/span extends past the line.
This is necessary because JavaScript source maps encode columns as UTF-16 code unit offsets, but Rust strings are UTF-8.
Sourcepub fn get_original_function_name<'a>(
&self,
token: &OriginalLocation,
minified_name: &str,
sm: &'a SourceMap,
) -> Option<&'a str>
pub fn get_original_function_name<'a>( &self, token: &OriginalLocation, minified_name: &str, sm: &'a SourceMap, ) -> Option<&'a str>
Attempt to infer the original function name for a token.
This is a best-effort heuristic used for error grouping. Given a token’s position in the generated (minified) source and the minified name at that position, it looks at surrounding code patterns to identify the function context.
§Algorithm
- Find the mapping at the token’s generated position
- Look backwards from that position in the generated line to find
patterns like
name(,name:,name =,.name - If the identified name matches
minified_name, look it up in the source map for the original name
Trait Implementations§
Source§impl Clone for SourceView
impl Clone for SourceView
Source§fn clone(&self) -> SourceView
fn clone(&self) -> SourceView
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more