File tree 4 files changed +20
-4
lines changed
rust/parse_ast/src/convert_ast
test/form/samples/handles-special-comments
4 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,9 @@ pub struct SequentialComments {
8
8
annotations : RefCell < Vec < AnnotationWithType > > ,
9
9
}
10
10
11
+ const ASCII_AT : u8 = '@' as u8 ;
12
+ const ASCII_HASH : u8 = '#' as u8 ;
13
+
11
14
impl SequentialComments {
12
15
pub fn add_comment ( & self , comment : Comment ) {
13
16
if comment. text . starts_with ( '#' ) && comment. text . contains ( "sourceMappingURL=" ) {
@@ -17,12 +20,18 @@ impl SequentialComments {
17
20
} ) ;
18
21
return ;
19
22
}
20
- let mut search_position = 1 ;
23
+ let mut search_position = comment
24
+ . text
25
+ . chars ( )
26
+ . nth ( 0 )
27
+ . map ( |first_char| first_char. len_utf8 ( ) )
28
+ . unwrap_or ( 0 ) ;
21
29
while let Some ( Some ( match_position) ) = comment. text . get ( search_position..) . map ( |s| s. find ( "__" ) )
22
30
{
23
31
search_position += match_position;
24
- match & comment. text [ search_position - 1 ..search_position] {
25
- "@" | "#" => {
32
+ // Using a byte reference avoids UTF8 character boundary checks
33
+ match & comment. text . as_bytes ( ) [ search_position - 1 ] {
34
+ & ASCII_AT | & ASCII_HASH => {
26
35
let annotation_slice = & comment. text [ search_position..] ;
27
36
if annotation_slice. starts_with ( "__PURE__" ) {
28
37
self . annotations . borrow_mut ( ) . push ( AnnotationWithType {
@@ -41,7 +50,7 @@ impl SequentialComments {
41
50
}
42
51
_ => { }
43
52
}
44
- search_position += 3 ;
53
+ search_position += 2 ;
45
54
}
46
55
}
47
56
Original file line number Diff line number Diff line change
1
+ module . exports = defineTest ( {
2
+ description : 'does not fail on certain comments (#5174)'
3
+ } ) ;
Original file line number Diff line number Diff line change
1
+ // “__
2
+ console . log ( 'main' ) ;
Original file line number Diff line number Diff line change
1
+ // “__
2
+ console . log ( 'main' ) ;
You can’t perform that action at this time.
0 commit comments