1- use std:: sync:: Arc ;
1+ use std:: { path :: Path , sync:: Arc } ;
22
33use nonmax:: NonMaxU32 ;
44use oxc_index:: { Idx , IndexVec } ;
@@ -73,27 +73,23 @@ pub struct SourcemapBuilder {
7373 generated_column : u32 ,
7474}
7575
76- impl Default for SourcemapBuilder {
77- fn default ( ) -> Self {
76+ impl SourcemapBuilder {
77+ pub fn new ( path : & Path , source_text : & str ) -> Self {
78+ let mut sourcemap_builder = oxc_sourcemap:: SourceMapBuilder :: default ( ) ;
79+ let line_offset_tables = Self :: generate_line_offset_tables ( source_text) ;
80+ let source_id =
81+ sourcemap_builder. set_source_and_content ( path. to_string_lossy ( ) . as_ref ( ) , source_text) ;
7882 Self {
79- source_id : 0 ,
80- original_source : "" . into ( ) ,
83+ source_id,
84+ original_source : Arc :: from ( source_text ) ,
8185 last_generated_update : 0 ,
8286 last_position : None ,
83- line_offset_tables : LineOffsetTables :: default ( ) ,
84- sourcemap_builder : oxc_sourcemap :: SourceMapBuilder :: default ( ) ,
87+ line_offset_tables,
88+ sourcemap_builder,
8589 generated_line : 0 ,
8690 generated_column : 0 ,
8791 }
8892 }
89- }
90-
91- impl SourcemapBuilder {
92- pub fn with_name_and_source ( & mut self , name : & str , source : & str ) {
93- self . line_offset_tables = Self :: generate_line_offset_tables ( source) ;
94- self . source_id = self . sourcemap_builder . set_source_and_content ( name, source) ;
95- self . original_source = source. into ( ) ;
96- }
9793
9894 pub fn into_sourcemap ( self ) -> oxc_sourcemap:: SourceMap {
9995 self . sourcemap_builder . into_sourcemap ( )
@@ -392,8 +388,7 @@ mod test {
392388 }
393389
394390 fn assert_mapping ( source : & str , mappings : & [ ( u32 , u32 , u32 ) ] ) {
395- let mut builder = SourcemapBuilder :: default ( ) ;
396- builder. with_name_and_source ( "x.js" , source) ;
391+ let mut builder = SourcemapBuilder :: new ( Path :: new ( "x.js" ) , source) ;
397392 for ( position, expected_line, expected_col) in mappings. iter ( ) . copied ( ) {
398393 let ( line, col) = builder. search_original_line_and_column ( position) ;
399394 assert_eq ! (
@@ -407,8 +402,7 @@ mod test {
407402 #[ test]
408403 fn add_source_mapping ( ) {
409404 fn create_mappings ( source : & str , line : u32 , column : u32 ) {
410- let mut builder = SourcemapBuilder :: default ( ) ;
411- builder. with_name_and_source ( "x.js" , source) ;
405+ let mut builder = SourcemapBuilder :: new ( Path :: new ( "x.js" ) , source) ;
412406 let output: Vec < u8 > = source. as_bytes ( ) . into ( ) ;
413407 for ( i, _ch) in source. char_indices ( ) {
414408 #[ allow( clippy:: cast_possible_truncation) ]
@@ -444,8 +438,7 @@ mod test {
444438 #[ test]
445439 fn add_source_mapping_for_name ( ) {
446440 let output = "ac" . as_bytes ( ) ;
447- let mut builder = SourcemapBuilder :: default ( ) ;
448- builder. with_name_and_source ( "x.js" , "ab" ) ;
441+ let mut builder = SourcemapBuilder :: new ( Path :: new ( "x.js" ) , "ab" ) ;
449442 builder. add_source_mapping_for_name ( output, Span :: new ( 0 , 1 ) , "a" ) ;
450443 builder. add_source_mapping_for_name ( output, Span :: new ( 1 , 2 ) , "c" ) ;
451444 let sm = builder. into_sourcemap ( ) ;
@@ -464,8 +457,7 @@ mod test {
464457 #[ test]
465458 fn add_source_mapping_for_unordered_position ( ) {
466459 let output = "" . as_bytes ( ) ;
467- let mut builder = SourcemapBuilder :: default ( ) ;
468- builder. with_name_and_source ( "x.js" , "ab" ) ;
460+ let mut builder = SourcemapBuilder :: new ( Path :: new ( "x.js" ) , "ab" ) ;
469461 builder. add_source_mapping ( output, 1 , None ) ;
470462 builder. add_source_mapping ( output, 0 , None ) ;
471463 let sm = builder. into_sourcemap ( ) ;
0 commit comments