@@ -16,10 +16,6 @@ static ANNOTATION_MATCHER: Lazy<DoubleArrayAhoCorasick<usize>> = Lazy::new(|| {
1616pub ( crate ) type CommentsMap = FxHashMap < /* attached_to */ u32 , Vec < Comment > > ;
1717
1818impl < ' a > Codegen < ' a > {
19- pub ( crate ) fn preserve_annotate_comments ( & self ) -> bool {
20- self . comment_options . preserve_annotate_comments && !self . options . minify
21- }
22-
2319 pub ( crate ) fn build_comments ( & mut self , comments : & [ Comment ] ) {
2420 for comment in comments {
2521 self . comments . entry ( comment. attached_to ) . or_default ( ) . push ( * comment) ;
@@ -31,35 +27,33 @@ impl<'a> Codegen<'a> {
3127 }
3228
3329 pub ( crate ) fn has_annotation_comment ( & self , start : u32 ) -> bool {
34- if !self . preserve_annotate_comments ( ) {
30+ if !self . options . print_annotation_comments ( ) {
3531 return false ;
3632 }
37- let Some ( source_text) = self . source_text else { return false } ;
3833 self . comments . get ( & start) . is_some_and ( |comments| {
39- comments. iter ( ) . any ( |comment| Self :: is_annotation_comment ( comment, source_text ) )
34+ comments. iter ( ) . any ( |comment| self . is_annotation_comment ( comment) )
4035 } )
4136 }
4237
4338 pub ( crate ) fn has_non_annotation_comment ( & self , start : u32 ) -> bool {
44- if !self . preserve_annotate_comments ( ) {
39+ if !self . options . print_annotation_comments ( ) {
4540 return self . has_comment ( start) ;
4641 }
47- let Some ( source_text) = self . source_text else { return false } ;
4842 self . comments . get ( & start) . is_some_and ( |comments| {
49- comments. iter ( ) . any ( |comment| !Self :: is_annotation_comment ( comment, source_text ) )
43+ comments. iter ( ) . any ( |comment| !self . is_annotation_comment ( comment) )
5044 } )
5145 }
5246
5347 /// Weather to keep leading comments.
54- fn is_leading_comments ( comment : & Comment , source_text : & str ) -> bool {
55- ( comment. is_jsdoc ( source_text) || ( comment. is_line ( ) && Self :: is_annotation_comment ( comment, source_text ) ) )
48+ fn is_leading_comments ( & self , comment : & Comment ) -> bool {
49+ ( comment. is_jsdoc ( self . source_text ) || ( comment. is_line ( ) && self . is_annotation_comment ( comment) ) )
5650 && comment. preceded_by_newline
5751 // webpack comment `/*****/`
58- && !comment. span . source_text ( source_text) . chars ( ) . all ( |c| c == '*' )
52+ && !comment. span . source_text ( self . source_text ) . chars ( ) . all ( |c| c == '*' )
5953 }
6054
61- fn print_comment ( & mut self , comment : & Comment , source_text : & str ) {
62- let comment_source = comment. real_span ( ) . source_text ( source_text) ;
55+ fn print_comment ( & mut self , comment : & Comment ) {
56+ let comment_source = comment. real_span ( ) . source_text ( self . source_text ) ;
6357 match comment. kind {
6458 CommentKind :: Line => {
6559 self . print_str ( comment_source) ;
@@ -84,14 +78,12 @@ impl<'a> Codegen<'a> {
8478 if self . options . minify {
8579 return ;
8680 }
87- let Some ( source_text) = self . source_text else { return } ;
8881 let Some ( comments) = self . comments . remove ( & start) else {
8982 return ;
9083 } ;
9184
92- let ( comments, unused_comments) : ( Vec < _ > , Vec < _ > ) = comments
93- . into_iter ( )
94- . partition ( |comment| Self :: is_leading_comments ( comment, source_text) ) ;
85+ let ( comments, unused_comments) : ( Vec < _ > , Vec < _ > ) =
86+ comments. into_iter ( ) . partition ( |comment| self . is_leading_comments ( comment) ) ;
9587
9688 if comments. first ( ) . is_some_and ( |c| c. preceded_by_newline ) {
9789 // Skip printing newline if this comment is already on a newline.
@@ -107,7 +99,7 @@ impl<'a> Codegen<'a> {
10799 self . print_indent ( ) ;
108100 }
109101
110- self . print_comment ( comment, source_text ) ;
102+ self . print_comment ( comment) ;
111103 }
112104
113105 if comments. last ( ) . is_some_and ( |c| c. is_line ( ) || c. followed_by_newline ) {
@@ -120,32 +112,31 @@ impl<'a> Codegen<'a> {
120112 }
121113 }
122114
123- fn is_annotation_comment ( comment : & Comment , source_text : & str ) -> bool {
124- let comment_content = comment. span . source_text ( source_text) ;
115+ fn is_annotation_comment ( & self , comment : & Comment ) -> bool {
116+ let comment_content = comment. span . source_text ( self . source_text ) ;
125117 ANNOTATION_MATCHER . find_iter ( comment_content) . count ( ) != 0
126118 }
127119
128120 pub ( crate ) fn print_annotation_comments ( & mut self , node_start : u32 ) {
129- if !self . preserve_annotate_comments ( ) {
121+ if !self . options . print_annotation_comments ( ) {
130122 return ;
131123 }
132124
133125 // If there is has annotation comments awaiting move to here, print them.
134126 let start = self . start_of_annotation_comment . take ( ) . unwrap_or ( node_start) ;
135127
136- let Some ( source_text) = self . source_text else { return } ;
137128 let Some ( comments) = self . comments . remove ( & start) else { return } ;
138129
139130 for comment in comments {
140- if !Self :: is_annotation_comment ( & comment, source_text ) {
131+ if !self . is_annotation_comment ( & comment) {
141132 continue ;
142133 }
143134 if comment. is_line ( ) {
144135 self . print_str ( "/*" ) ;
145- self . print_str ( comment. span . source_text ( source_text) ) ;
136+ self . print_str ( comment. span . source_text ( self . source_text ) ) ;
146137 self . print_str ( "*/" ) ;
147138 } else {
148- self . print_str ( comment. real_span ( ) . source_text ( source_text) ) ;
139+ self . print_str ( comment. real_span ( ) . source_text ( self . source_text ) ) ;
149140 }
150141 self . print_hard_space ( ) ;
151142 }
@@ -155,12 +146,10 @@ impl<'a> Codegen<'a> {
155146 if self . options . minify {
156147 return false ;
157148 }
158- let Some ( source_text) = self . source_text else { return false } ;
159149 let Some ( comments) = self . comments . remove ( & start) else { return false } ;
160150
161- let ( annotation_comments, comments) : ( Vec < _ > , Vec < _ > ) = comments
162- . into_iter ( )
163- . partition ( |comment| Self :: is_annotation_comment ( comment, source_text) ) ;
151+ let ( annotation_comments, comments) : ( Vec < _ > , Vec < _ > ) =
152+ comments. into_iter ( ) . partition ( |comment| self . is_annotation_comment ( comment) ) ;
164153
165154 if !annotation_comments. is_empty ( ) {
166155 self . comments . insert ( start, annotation_comments) ;
@@ -169,7 +158,7 @@ impl<'a> Codegen<'a> {
169158 for comment in & comments {
170159 self . print_hard_newline ( ) ;
171160 self . print_indent ( ) ;
172- self . print_comment ( comment, source_text ) ;
161+ self . print_comment ( comment) ;
173162 }
174163
175164 if comments. is_empty ( ) {
0 commit comments