Skip to main content

SourceView

Struct SourceView 

Source
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

Source

pub fn new(source: Arc<str>) -> Self

Create a new SourceView from a shared source string.

Source

pub fn from_string(source: String) -> Self

Create a new SourceView from an owned String.

Source

pub fn source(&self) -> &str

Return the full source string.

Source

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).

Source

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.

Source

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.

Source

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
  1. Find the mapping at the token’s generated position
  2. Look backwards from that position in the generated line to find patterns like name(, name:, name =, .name
  3. 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

Source§

fn clone(&self) -> SourceView

Returns a duplicate 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 SourceView

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.