Skip to content

Commit

Permalink
Handle pure annotations that are separated by a comment (#5332)
Browse files Browse the repository at this point in the history
Handles pure annotations that are separated by a comment
  • Loading branch information
lukastaegert committed Jan 6, 2024
1 parent 4ab3ad3 commit c249413
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
19 changes: 15 additions & 4 deletions rust/parse_ast/src/convert_ast/annotations.rs
Expand Up @@ -13,7 +13,7 @@ impl SequentialComments {
if comment.text.starts_with('#') && comment.text.contains("sourceMappingURL=") {
self.annotations.borrow_mut().push(AnnotationWithType {
comment,
kind: AnnotationKind::SourceMappingUrl,
kind: CommentKind::Annotation(AnnotationKind::SourceMappingUrl),
});
return;
}
Expand All @@ -33,14 +33,14 @@ impl SequentialComments {
if annotation_slice.starts_with("__PURE__") {
self.annotations.borrow_mut().push(AnnotationWithType {
comment,
kind: AnnotationKind::Pure,
kind: CommentKind::Annotation(AnnotationKind::Pure),
});
return;
}
if annotation_slice.starts_with("__NO_SIDE_EFFECTS__") {
self.annotations.borrow_mut().push(AnnotationWithType {
comment,
kind: AnnotationKind::NoSideEffects,
kind: CommentKind::Annotation(AnnotationKind::NoSideEffects),
});
return;
}
Expand All @@ -49,6 +49,10 @@ impl SequentialComments {
}
search_position += 2;
}
self.annotations.borrow_mut().push(AnnotationWithType {
comment,
kind: CommentKind::Comment,
});
}

pub fn take_annotations(self) -> Vec<AnnotationWithType> {
Expand Down Expand Up @@ -122,9 +126,16 @@ impl Comments for SequentialComments {
}
}

#[derive(Debug)]
pub struct AnnotationWithType {
pub comment: Comment,
pub kind: AnnotationKind,
pub kind: CommentKind,
}

#[derive(Clone, Debug)]
pub enum CommentKind {
Annotation(AnnotationKind),
Comment,
}

#[derive(Clone, PartialEq, Debug)]
Expand Down
17 changes: 10 additions & 7 deletions rust/parse_ast/src/convert_ast/converter/utf16_positions.rs
@@ -1,6 +1,7 @@
use std::slice::Iter;
use std::str::Chars;

use crate::convert_ast::annotations::CommentKind::Annotation;
use crate::convert_ast::annotations::{AnnotationKind, AnnotationWithType};

pub struct Utf8ToUtf16ByteIndexConverterAndAnnotationHandler<'a> {
Expand Down Expand Up @@ -71,20 +72,22 @@ impl<'a> Utf8ToUtf16ByteIndexConverterAndAnnotationHandler<'a> {
while self.current_utf8_index < utf8_index {
if self.current_utf8_index == self.next_annotation_start {
let start = self.current_utf16_index;
let (next_annotation_end, next_annotation_kind) = self
let (next_comment_end, next_comment_kind) = self
.next_annotation
.map(|a| (a.comment.span.hi.0 - 1, a.kind.clone()))
.unwrap();
while self.current_utf8_index < next_annotation_end {
while self.current_utf8_index < next_comment_end {
let character = self.character_iterator.next().unwrap();
self.current_utf8_index += character.len_utf8() as u32;
self.current_utf16_index += character.len_utf16() as u32;
}
self.collected_annotations.push(ConvertedAnnotation {
start,
end: self.current_utf16_index,
kind: next_annotation_kind,
});
if let Annotation(kind) = next_comment_kind {
self.collected_annotations.push(ConvertedAnnotation {
start,
end: self.current_utf16_index,
kind,
});
}
self.next_annotation = self.annotation_iterator.next();
self.next_annotation_start = get_annotation_start(self.next_annotation);
} else {
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/pure-comment-not-first/_config.js
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'recognizes pure comments when there are other comments in between'
});
3 changes: 3 additions & 0 deletions test/form/samples/pure-comment-not-first/_expected.js
@@ -0,0 +1,3 @@
const five = /* #__PURE__ */ /* foo */ add(2, 3);

export { five };
2 changes: 2 additions & 0 deletions test/form/samples/pure-comment-not-first/main.js
@@ -0,0 +1,2 @@
const four = /* #__PURE__ */ /* foo */ add(2, 2);
export const five = /* #__PURE__ */ /* foo */ add(2, 3);

0 comments on commit c249413

Please sign in to comment.