@@ -12,13 +12,16 @@ use crate::hermes::SourceMapHermes;
1212use crate :: sourceview:: SourceView ;
1313use crate :: utils:: { find_common_prefix, greatest_lower_bound} ;
1414
15+ use debugid:: DebugId ;
16+
1517/// Controls the `SourceMap::rewrite` behavior
1618///
1719/// Default configuration:
1820///
1921/// * `with_names`: true
2022/// * `with_source_contents`: true
2123/// * `load_local_source_contents`: false
24+ #[ derive( Debug , Clone ) ]
2225pub struct RewriteOptions < ' a > {
2326 /// If enabled, names are kept in the rewritten sourcemap.
2427 pub with_names : bool ,
@@ -461,6 +464,7 @@ pub struct SourceMap {
461464 source_root : Option < String > ,
462465 sources : Vec < String > ,
463466 sources_content : Vec < Option < SourceView < ' static > > > ,
467+ debug_id : Option < DebugId > ,
464468}
465469
466470impl SourceMap {
@@ -569,9 +573,20 @@ impl SourceMap {
569573 . into_iter ( )
570574 . map ( |opt| opt. map ( SourceView :: from_string) )
571575 . collect ( ) ,
576+ debug_id : None ,
572577 }
573578 }
574579
580+ /// Returns the embedded debug id.
581+ pub fn get_debug_id ( & self ) -> Option < DebugId > {
582+ self . debug_id
583+ }
584+
585+ /// Sets a new value for the debug id.
586+ pub fn set_debug_id ( & mut self , debug_id : Option < DebugId > ) {
587+ self . debug_id = debug_id
588+ }
589+
575590 /// Returns the embedded filename in case there is one.
576591 pub fn get_file ( & self ) -> Option < & str > {
577592 self . file . as_deref ( )
@@ -766,6 +781,7 @@ impl SourceMap {
766781 options : & RewriteOptions < ' _ > ,
767782 ) -> Result < ( SourceMap , Vec < u32 > ) > {
768783 let mut builder = SourceMapBuilder :: new ( self . get_file ( ) ) ;
784+ builder. set_debug_id ( self . debug_id ) ;
769785
770786 for token in self . tokens ( ) {
771787 let raw = builder. add_token ( & token, options. with_names ) ;
@@ -1065,3 +1081,33 @@ impl SourceMapSection {
10651081 self . map = sm. map ( Box :: new) ;
10661082 }
10671083}
1084+
1085+ #[ cfg( test) ]
1086+ mod tests {
1087+ use super :: { RewriteOptions , SourceMap } ;
1088+ use debugid:: DebugId ;
1089+
1090+ #[ test]
1091+ fn test_rewrite_debugid ( ) {
1092+ let input: & [ _ ] = br#"{
1093+ "version":3,
1094+ "sources":["coolstuff.js"],
1095+ "names":["x","alert"],
1096+ "mappings":"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM",
1097+ "debug_id":"00000000-0000-0000-0000-000000000000"
1098+ }"# ;
1099+
1100+ let sm = SourceMap :: from_slice ( input) . unwrap ( ) ;
1101+
1102+ assert_eq ! ( sm. debug_id, Some ( DebugId :: default ( ) ) ) ;
1103+
1104+ let new_sm = sm
1105+ . rewrite ( & RewriteOptions {
1106+ with_names : false ,
1107+ ..Default :: default ( )
1108+ } )
1109+ . unwrap ( ) ;
1110+
1111+ assert_eq ! ( new_sm. debug_id, Some ( DebugId :: default ( ) ) ) ;
1112+ }
1113+ }
0 commit comments