use oxc_ast::ast::Comment;
use rustc_hash::FxHashMap;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CommentAnnotation {
Hoist,
Scope,
Const,
}
pub const HOIST_TEXT: &str = "@__HOIST__";
pub const CONST_TEXT: &str = "@__CONST__";
pub const SCOPE_TEXT: &str = "@__SCOPE__";
pub fn annotation_for_content(content: &str) -> Option<CommentAnnotation> {
if content.contains(HOIST_TEXT) {
Some(CommentAnnotation::Hoist)
} else if content.contains(SCOPE_TEXT) {
Some(CommentAnnotation::Scope)
} else if content.contains(CONST_TEXT) {
Some(CommentAnnotation::Const)
} else {
None
}
}
pub fn is_annotation_content(content: &str) -> bool {
annotation_for_content(content).is_some()
}
pub fn build_comment_annotations(
source_text: &str,
comments: &[Comment],
) -> FxHashMap<u32, CommentAnnotation> {
let mut map = FxHashMap::default();
for comment in comments {
if !comment.is_leading() {
continue;
}
let content = comment.content_span().source_text(source_text);
if let Some(annotation) = annotation_for_content(content) {
map.insert(comment.attached_to, annotation);
}
}
map
}