Skip to content

Commit b132bd4

Browse files
authored
Handle special characters before double underscores (rollup#5178)
1 parent fcab1f6 commit b132bd4

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

rust/parse_ast/src/convert_ast/annotations.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ pub struct SequentialComments {
88
annotations: RefCell<Vec<AnnotationWithType>>,
99
}
1010

11+
const ASCII_AT: u8 = '@' as u8;
12+
const ASCII_HASH: u8 = '#' as u8;
13+
1114
impl SequentialComments {
1215
pub fn add_comment(&self, comment: Comment) {
1316
if comment.text.starts_with('#') && comment.text.contains("sourceMappingURL=") {
@@ -17,12 +20,18 @@ impl SequentialComments {
1720
});
1821
return;
1922
}
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);
2129
while let Some(Some(match_position)) = comment.text.get(search_position..).map(|s| s.find("__"))
2230
{
2331
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 => {
2635
let annotation_slice = &comment.text[search_position..];
2736
if annotation_slice.starts_with("__PURE__") {
2837
self.annotations.borrow_mut().push(AnnotationWithType {
@@ -41,7 +50,7 @@ impl SequentialComments {
4150
}
4251
_ => {}
4352
}
44-
search_position += 3;
53+
search_position += 2;
4554
}
4655
}
4756

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = defineTest({
2+
description: 'does not fail on certain comments (#5174)'
3+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// “__
2+
console.log('main');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// “__
2+
console.log('main');

0 commit comments

Comments
 (0)