@@ -12,18 +12,20 @@ mod sourcemap_builder;
1212
1313use std:: { borrow:: Cow , ops:: Range } ;
1414
15+ use rustc_hash:: FxHashMap ;
16+
1517use oxc_ast:: {
1618 ast:: { BlockStatement , Directive , Expression , Program , Statement } ,
1719 Comment , Trivias ,
1820} ;
19- use oxc_span:: Span ;
21+ use oxc_mangler:: Mangler ;
22+ use oxc_span:: { CompactStr , Span } ;
2023use oxc_syntax:: {
2124 identifier:: is_identifier_part,
2225 operator:: { BinaryOperator , UnaryOperator , UpdateOperator } ,
2326 precedence:: Precedence ,
2427 symbol:: SymbolId ,
2528} ;
26- use rustc_hash:: FxHashMap ;
2729
2830pub use crate :: {
2931 context:: Context ,
@@ -55,6 +57,8 @@ pub struct Codegen<'a, const MINIFY: bool> {
5557
5658 trivias : Trivias ,
5759
60+ mangler : Option < Mangler > ,
61+
5862 /// Output Code
5963 code : Vec < u8 > ,
6064
@@ -111,6 +115,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
111115 comment_options : CommentOptions :: default ( ) ,
112116 source_text : "" ,
113117 trivias : Trivias :: default ( ) ,
118+ mangler : None ,
114119 code : vec ! [ ] ,
115120 needs_semicolon : false ,
116121 need_space_before_dot : 0 ,
@@ -127,6 +132,15 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
127132 }
128133 }
129134
135+ /// Initialize the output code buffer to reduce memory reallocation.
136+ /// Minification will reduce by at least half of the original size.
137+ #[ must_use]
138+ pub fn with_capacity ( mut self , source_text_len : usize ) -> Self {
139+ let capacity = if MINIFY { source_text_len / 2 } else { source_text_len } ;
140+ self . code = Vec :: with_capacity ( capacity) ;
141+ self
142+ }
143+
130144 #[ must_use]
131145 pub fn enable_comment (
132146 mut self ,
@@ -148,12 +162,9 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
148162 self
149163 }
150164
151- /// Initialize the output code buffer to reduce memory reallocation.
152- /// Minification will reduce by at least half of the original size.
153165 #[ must_use]
154- pub fn with_capacity ( mut self , source_text_len : usize ) -> Self {
155- let capacity = if MINIFY { source_text_len / 2 } else { source_text_len } ;
156- self . code = Vec :: with_capacity ( capacity) ;
166+ pub fn with_mangler ( mut self , mangler : Option < Mangler > ) -> Self {
167+ self . mangler = mangler;
157168 self
158169 }
159170
@@ -180,7 +191,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
180191 self . code . push ( ch) ;
181192 }
182193
183- /// Push a single character into the buffer
194+ /// Push str into the buffer
184195 #[ inline]
185196 pub fn print_str ( & mut self , s : & str ) {
186197 self . code . extend ( s. as_bytes ( ) ) ;
@@ -398,14 +409,16 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
398409 }
399410
400411 #[ allow( clippy:: needless_pass_by_value) ]
401- fn print_symbol ( & mut self , span : Span , _symbol_id : Option < SymbolId > , fallback : & str ) {
402- // if let Some(mangler) = &self.mangler {
403- // if let Some(symbol_id) = symbol_id {
404- // let name = mangler.get_symbol_name(symbol_id);
405- // self.print_str(name.clone());
406- // return;
407- // }
408- // }
412+ fn print_symbol ( & mut self , span : Span , symbol_id : Option < SymbolId > , fallback : & str ) {
413+ if let Some ( mangler) = & self . mangler {
414+ if let Some ( symbol_id) = symbol_id {
415+ let name = mangler. get_symbol_name ( symbol_id) ;
416+ let name = CompactStr :: new ( name) ;
417+ self . add_source_mapping_for_name ( span, & name) ;
418+ self . print_str ( & name) ;
419+ return ;
420+ }
421+ }
409422 self . add_source_mapping_for_name ( span, fallback) ;
410423 self . print_str ( fallback) ;
411424 }
0 commit comments