1
- use rustc_hash:: FxHashMap ;
1
+ use rustc_hash:: { FxHashMap , FxHashSet } ;
2
+ use std:: borrow:: Cow ;
2
3
3
- use oxc_ast:: { Comment , CommentKind } ;
4
+ use oxc_ast:: { Comment , CommentKind , ast :: Program } ;
4
5
use oxc_syntax:: identifier:: is_line_terminator;
5
6
6
7
use crate :: { Codegen , LegalComment } ;
@@ -15,23 +16,14 @@ impl Codegen<'_> {
15
16
{
16
17
return ;
17
18
}
18
- let move_legal_comments = {
19
- let legal_comments = & self . options . legal_comments ;
20
- matches ! (
21
- legal_comments,
22
- LegalComment :: Eof | LegalComment :: Linked ( _) | LegalComment :: External
23
- )
24
- } ;
25
19
for comment in comments {
26
20
// Omit pure comments because they are handled separately.
27
21
if comment. is_pure ( ) || comment. is_no_side_effects ( ) {
28
22
continue ;
29
23
}
30
24
let mut add = false ;
31
25
if comment. is_legal ( ) {
32
- if move_legal_comments {
33
- self . legal_comments . push ( * comment) ;
34
- } else if self . options . print_legal_comment ( ) {
26
+ if self . options . print_legal_comment ( ) {
35
27
add = true ;
36
28
}
37
29
} else if comment. is_leading ( ) {
@@ -142,8 +134,7 @@ impl Codegen<'_> {
142
134
}
143
135
CommentKind :: Block => {
144
136
// Print block comments with our own indentation.
145
- let lines = comment_source. split ( is_line_terminator) ;
146
- for line in lines {
137
+ for line in comment_source. split ( is_line_terminator) {
147
138
if !line. starts_with ( "/*" ) {
148
139
self . print_indent ( ) ;
149
140
}
@@ -156,25 +147,66 @@ impl Codegen<'_> {
156
147
}
157
148
}
158
149
159
- pub ( crate ) fn try_print_eof_legal_comments ( & mut self ) {
160
- match self . options . legal_comments . clone ( ) {
161
- LegalComment :: Eof => {
162
- let comments = self . legal_comments . drain ( ..) . collect :: < Vec < _ > > ( ) ;
163
- if !comments. is_empty ( ) {
164
- self . print_hard_newline ( ) ;
150
+ /// Handle Eof / Linked / External Comments.
151
+ /// Return a list of comments of linked or external.
152
+ pub ( crate ) fn handle_eof_linked_or_external_comments (
153
+ & mut self ,
154
+ program : & Program < ' _ > ,
155
+ ) -> Vec < Comment > {
156
+ let legal_comments = & self . options . legal_comments ;
157
+ if matches ! ( legal_comments, LegalComment :: None | LegalComment :: Inline ) {
158
+ return vec ! [ ] ;
159
+ }
160
+
161
+ // Dedupe legal comments for smaller output size.
162
+ let mut set = FxHashSet :: default ( ) ;
163
+ let mut comments = vec ! [ ] ;
164
+
165
+ let source_text = program. source_text ;
166
+ for comment in program. comments . iter ( ) . filter ( |c| c. is_legal ( ) ) {
167
+ let mut text = Cow :: Borrowed ( comment. span . source_text ( source_text) ) ;
168
+ if comment. is_block ( ) && text. contains ( is_line_terminator) {
169
+ let mut buffer = String :: with_capacity ( text. len ( ) ) ;
170
+ // Print block comments with our own indentation.
171
+ for line in text. split ( is_line_terminator) {
172
+ if !line. starts_with ( "/*" ) {
173
+ buffer. push ( '\t' ) ;
174
+ }
175
+ buffer. push_str ( line. trim_start ( ) ) ;
176
+ if !line. ends_with ( "*/" ) {
177
+ buffer. push ( '\n' ) ;
178
+ }
165
179
}
180
+ text = Cow :: Owned ( buffer) ;
181
+ }
182
+ if set. insert ( text) {
183
+ comments. push ( * comment) ;
184
+ }
185
+ }
186
+
187
+ if comments. is_empty ( ) {
188
+ return vec ! [ ] ;
189
+ }
190
+
191
+ match legal_comments {
192
+ LegalComment :: Eof => {
193
+ self . print_hard_newline ( ) ;
166
194
for c in comments {
167
195
self . print_comment ( & c) ;
168
196
self . print_hard_newline ( ) ;
169
197
}
198
+ vec ! [ ]
170
199
}
171
200
LegalComment :: Linked ( path) => {
201
+ let path = path. clone ( ) ;
172
202
self . print_hard_newline ( ) ;
173
203
self . print_str ( "/*! For license information please see " ) ;
174
204
self . print_str ( & path) ;
175
205
self . print_str ( " */" ) ;
206
+ comments
176
207
}
177
- _ => { }
208
+ LegalComment :: External => comments,
209
+ LegalComment :: None | LegalComment :: Inline => unreachable ! ( ) ,
178
210
}
179
211
}
180
212
}
0 commit comments