Skip to content

Commit

Permalink
refactor(codegen): simplify printing annotation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 24, 2024
1 parent c64a88d commit e82644f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 292 deletions.
161 changes: 0 additions & 161 deletions crates/oxc_codegen/src/annotation_comment.rs

This file was deleted.

82 changes: 59 additions & 23 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
use daachorse::DoubleArrayAhoCorasick;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;

use oxc_ast::{Comment, CommentKind, Trivias};
use oxc_syntax::identifier::is_line_terminator;

use crate::Codegen;

pub static ANNOTATION_MATCHER: Lazy<DoubleArrayAhoCorasick<usize>> = Lazy::new(|| {
let patterns = vec!["#__NO_SIDE_EFFECTS__", "@__NO_SIDE_EFFECTS__", "@__PURE__", "#__PURE__"];

DoubleArrayAhoCorasick::new(patterns).unwrap()
});

pub type CommentsMap = FxHashMap</* attached_to */ u32, Vec<Comment>>;

impl<'a> Codegen<'a> {
pub(crate) fn build_inner_comments(&mut self, trivias: &Trivias) {
let mut inner_comments: CommentsMap = FxHashMap::default();
for comment in trivias.comments().copied().filter(Self::is_inner_comment) {
inner_comments.entry(comment.attached_to).or_default().push(comment);
pub fn preserve_annotate_comments(&self) -> bool {
self.comment_options.preserve_annotate_comments && !self.options.minify
}

pub(crate) fn build_comments(&mut self, trivias: &Trivias) {
for comment in trivias.comments().copied() {
self.comments.entry(comment.attached_to).or_default().push(comment);
}
self.inner_comments = inner_comments;
}

fn is_inner_comment(comment: &Comment) -> bool {
!comment.preceded_by_newline && !comment.followed_by_newline && comment.is_block()
pub fn has_annotation_comments(&self, start: u32) -> bool {
let Some(source_text) = self.source_text else { return false };
self.comments.get(&start).is_some_and(|comments| {
comments.iter().any(|comment| Self::is_annotation_comments(comment, source_text))
})
}

pub(crate) fn print_inner_comments(&mut self, start: u32) {
if self.options.minify {
return;
}
let Some(source_text) = self.source_text else { return };
let Some(comments) = self.inner_comments.remove(&start) else { return };
let Some(comments) = self.comments.remove(&start) else { return };

for comment in comments {
self.print_str(comment.real_span().source_text(source_text));
self.print_hard_space();
}
}

pub(crate) fn build_leading_comments(&mut self, source_text: &str, trivias: &Trivias) {
let mut leading_comments: CommentsMap = FxHashMap::default();
for comment in trivias
.comments()
.copied()
.filter(|comment| Self::is_jsdoc_comment(comment, source_text))
{
leading_comments.entry(comment.attached_to).or_default().push(comment);
}
self.leading_comments = leading_comments;
}

fn is_jsdoc_comment(comment: &Comment, source_text: &str) -> bool {
comment.is_jsdoc(source_text)
/// Weather to keep leading comments.
fn is_leading_comments(comment: &Comment, source_text: &str) -> bool {
(comment.is_jsdoc(source_text) || (comment.is_line() && Self::is_annotation_comments(comment, source_text)))
&& comment.preceded_by_newline
// webpack comment `/*****/`
&& !comment.span.source_text(source_text).chars().all(|c| c == '*')
Expand All @@ -57,7 +59,13 @@ impl<'a> Codegen<'a> {
return;
}
let Some(source_text) = self.source_text else { return };
let Some(comments) = self.leading_comments.remove(&start) else { return };
let Some(comments) = self.comments.remove(&start) else {
return;
};

let (comments, reserved_comments): (Vec<_>, Vec<_>) = comments
.into_iter()
.partition(|comment| Self::is_leading_comments(comment, source_text));

if comments.first().is_some_and(|c| c.preceded_by_newline) {
// Skip printing newline if this comment is already on a newline.
Expand Down Expand Up @@ -98,5 +106,33 @@ impl<'a> Codegen<'a> {
self.print_hard_newline();
self.print_indent();
}

if !reserved_comments.is_empty() {
self.comments.insert(start, reserved_comments);
}
}

fn is_annotation_comments(comment: &Comment, source_text: &str) -> bool {
let comment_content = comment.span.source_text(source_text);
ANNOTATION_MATCHER.find_iter(comment_content).count() != 0
}

pub(crate) fn print_annotation_comments(&mut self, node_start: u32) {
if !self.preserve_annotate_comments() {
return;
}

let start = self.move_comment_start.take().unwrap_or(node_start);

let Some(source_text) = self.source_text else { return };
let Some(comments) = self.comments.remove(&start) else { return };

for comment in comments {
if !Self::is_annotation_comments(&comment, source_text) {
continue;
}
self.print_str(comment.real_span().source_text(source_text));
self.print_hard_space();
}
}
}
Loading

0 comments on commit e82644f

Please sign in to comment.