Skip to content

Commit

Permalink
perf(ast): faster Comment::is_jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 14, 2024
1 parent f0a8d8a commit ae99624
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions crates/oxc_ast/src/ast/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Comment {
pub fn is_jsdoc(&self, source_text: &str) -> bool {
self.is_leading()
&& self.is_block()
&& self.content_span().source_text(source_text).starts_with('*')
&& source_text.as_bytes()[self.content_span_start() as usize] == b'*'
}

/// Legal comments
Expand All @@ -126,9 +126,20 @@ impl Comment {

/// Gets the span of the comment content.
pub fn content_span(&self) -> Span {
Span::new(self.content_span_start(), self.content_span_end())
}

/// Gets the start span of the comment content.
pub fn content_span_start(&self) -> u32 {
self.span.start + 2
}

/// Gets the start span of the comment content.
pub fn content_span_end(&self) -> u32 {
let end = self.span.end;
match self.kind {
CommentKind::Line => Span::new(self.span.start + 2, self.span.end),
CommentKind::Block => Span::new(self.span.start + 2, self.span.end - 2),
CommentKind::Line => end,
CommentKind::Block => end - 2,
}
}
}

0 comments on commit ae99624

Please sign in to comment.