Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sourcemap): expose string_wizard's sourceMap #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/magic_string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod append;
pub mod indent;
pub mod movement;
pub mod prepend;
pub mod token;
#[cfg(feature = "source_map")]
pub mod source_map;
pub mod update;
Expand Down Expand Up @@ -211,3 +212,5 @@ impl<'a> Iterator for IterChunks<'a> {
}
}
}


87 changes: 87 additions & 0 deletions src/magic_string/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{
MagicString,
};

use super::token::{Token, TokenChunk};

#[derive(Debug)]
pub struct SourceMapOptions {
pub include_content: bool,
Expand All @@ -18,6 +20,71 @@ impl Default for SourceMapOptions {
}
}


#[derive(Debug, Clone, Default)]
pub struct SourceMap {
pub file: Option<Arc<str>>,
pub names: Vec<Arc<str>>,
pub source_root: Option<String>,
pub sources: Vec<Arc<str>>,
pub source_contents: Option<Vec<Arc<str>>>,
pub tokens: Vec<Token>,
pub token_chunks: Option<Vec<TokenChunk>>,
pub x_google_ignore_list: Option<Vec<u32>>,
}

impl SourceMap {

pub fn from_oxc_sourcemap(source_map: oxc_sourcemap::SourceMap) -> Self {
Self {
file:source_map.get_file().map(Into::into),
names:source_map.get_names().map(Into::into).collect::<Vec<_>>(),
source_root:source_map.get_source_root().map(str::to_string),
sources:source_map.get_sources().map(Into::into).collect::<Vec<_>>(),
source_contents:source_map.get_source_contents().map(|x| x.map(Into::into).collect::<Vec<_>>()),
tokens: source_map.get_tokens()
.map( |token|
Token::new(
token.get_dst_line(),
token.get_dst_col(),
token.get_src_line(),
token.get_src_col(),
token.get_source_id(),
token.get_name_id()
) ).collect::<Vec<_>>(),
token_chunks: None,
x_google_ignore_list: None}
}

pub fn get_file(&self)-> Option<&str> {
self.file.as_deref()

}

pub fn get_names(&self) -> impl Iterator<Item = &str> {
self.names.iter().map(AsRef::as_ref)

}

pub fn get_source_root(&self) -> Option<&str> {
self.source_root.as_deref()
}

pub fn get_sources(&self) -> impl Iterator<Item = &str> {
self.sources.iter().map(AsRef::as_ref)
}

pub fn get_source_contents(&self) -> Option<impl Iterator<Item = &str>> {
self.source_contents.as_ref().map(|v| v.iter().map(AsRef::as_ref))

}

pub fn get_tokens(&self) -> impl Iterator<Item = &Token> {
self.tokens.iter()
}

}

impl<'s> MagicString<'s> {
pub fn source_map(&self, opts: SourceMapOptions) -> oxc_sourcemap::SourceMap {
let mut source_builder = SourcemapBuilder::new(opts.hires);
Expand Down Expand Up @@ -50,4 +117,24 @@ impl<'s> MagicString<'s> {

source_builder.into_source_map()
}

pub fn create_own_sourcemap(&self,opts: SourceMapOptions) -> SourceMap {
let oxc_source_map = self.source_map(opts);
SourceMap::from_oxc_sourcemap(oxc_source_map)
}
}


impl Token {
pub fn new(
dst_line: u32,
dst_col: u32,
src_line: u32,
src_col: u32,
source_id: Option<u32>,
name_id: Option<u32>,
) -> Self {
Self { dst_line, dst_col, src_line, src_col, source_id, name_id }
}

}
22 changes: 22 additions & 0 deletions src/magic_string/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Token {
pub dst_line: u32,
pub dst_col: u32,
pub src_line: u32,
pub src_col: u32,
pub source_id: Option<u32>,
pub name_id: Option<u32>,
}


#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TokenChunk {
pub start: u32,
pub end: u32,
pub prev_dst_line: u32,
pub prev_dst_col: u32,
pub prev_src_line: u32,
pub prev_src_col: u32,
pub prev_name_id: u32,
pub prev_source_id: u32,
}