diff --git a/src/magic_string/mod.rs b/src/magic_string/mod.rs index f127cbd..7ef70a2 100644 --- a/src/magic_string/mod.rs +++ b/src/magic_string/mod.rs @@ -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; @@ -211,3 +212,5 @@ impl<'a> Iterator for IterChunks<'a> { } } } + + diff --git a/src/magic_string/source_map.rs b/src/magic_string/source_map.rs index 6f48925..276b6ee 100644 --- a/src/magic_string/source_map.rs +++ b/src/magic_string/source_map.rs @@ -5,6 +5,8 @@ use crate::{ MagicString, }; +use super::token::{Token, TokenChunk}; + #[derive(Debug)] pub struct SourceMapOptions { pub include_content: bool, @@ -18,6 +20,71 @@ impl Default for SourceMapOptions { } } + +#[derive(Debug, Clone, Default)] +pub struct SourceMap { + pub file: Option>, + pub names: Vec>, + pub source_root: Option, + pub sources: Vec>, + pub source_contents: Option>>, + pub tokens: Vec, + pub token_chunks: Option>, + pub x_google_ignore_list: Option>, +} + +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::>(), + source_root:source_map.get_source_root().map(str::to_string), + sources:source_map.get_sources().map(Into::into).collect::>(), + source_contents:source_map.get_source_contents().map(|x| x.map(Into::into).collect::>()), + 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::>(), + 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 { + 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 { + self.sources.iter().map(AsRef::as_ref) + } + + pub fn get_source_contents(&self) -> Option> { + self.source_contents.as_ref().map(|v| v.iter().map(AsRef::as_ref)) + + } + + pub fn get_tokens(&self) -> impl Iterator { + 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); @@ -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, + name_id: Option, + ) -> Self { + Self { dst_line, dst_col, src_line, src_col, source_id, name_id } + } + +} \ No newline at end of file diff --git a/src/magic_string/token.rs b/src/magic_string/token.rs new file mode 100644 index 0000000..e61bda8 --- /dev/null +++ b/src/magic_string/token.rs @@ -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, + pub name_id: Option, +} + + + #[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, + }