Skip to content

Commit

Permalink
feat(parser,codegen): parse and print leading comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 15, 2024
1 parent 91bb06e commit f431e04
Show file tree
Hide file tree
Showing 12 changed files with 5,278 additions and 5,145 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use crate::{
ast_builder::AstBuilder,
ast_builder_impl::NONE,
ast_kind::{AstKind, AstType},
trivia::{Comment, CommentKind, SortedComments, Trivias},
trivia::{Comment, CommentKind, CommentPosition, SortedComments, Trivias},
visit::{Visit, VisitMut},
};

Expand Down
52 changes: 46 additions & 6 deletions crates/oxc_ast/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,39 @@ use std::{

use oxc_span::Span;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CommentPosition {
Leading,
Trailing,
Dangling,
}

/// Single or multiline comment
#[derive(Debug, Clone, Copy)]
pub struct Comment {
pub kind: CommentKind,
/// The span of the comment text (without leading/trailing delimiters).
pub span: Span,

pub position: CommentPosition,
pub preceded_by_newline: Option<bool>,
pub followed_by_newline: Option<bool>,

pub attached_to: u32,
}

impl Comment {
#[inline]
pub fn new(start: u32, end: u32, kind: CommentKind) -> Self {
let span = Span::new(start, end);
Self { kind, span }
Self {
kind,
span,
position: CommentPosition::Dangling,
preceded_by_newline: None,
followed_by_newline: None,
attached_to: 0,
}
}

pub fn real_span(&self) -> Span {
Expand All @@ -40,6 +60,26 @@ impl Comment {
CommentKind::SingleLine | CommentKind::MultiLine => self.span.start - 2,
}
}

#[inline]
pub fn is_single_line(self) -> bool {
self.kind == CommentKind::SingleLine
}

#[inline]
pub fn is_multi_line(self) -> bool {
self.kind == CommentKind::MultiLine
}

#[inline]
pub fn is_leading(&self) -> bool {
self.position == CommentPosition::Leading
}

#[inline]
pub fn is_trailing(&self) -> bool {
self.position == CommentPosition::Trailing
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -192,11 +232,11 @@ mod test {
#[test]
fn test_comments_range() {
let comments: SortedComments = vec![
Comment { span: Span::new(0, 4), kind: CommentKind::SingleLine },
Comment { span: Span::new(5, 9), kind: CommentKind::SingleLine },
Comment { span: Span::new(10, 13), kind: CommentKind::SingleLine },
Comment { span: Span::new(14, 17), kind: CommentKind::SingleLine },
Comment { span: Span::new(18, 23), kind: CommentKind::SingleLine },
Comment::new(0, 4, CommentKind::SingleLine),
Comment::new(5, 9, CommentKind::SingleLine),
Comment::new(10, 13, CommentKind::SingleLine),
Comment::new(14, 17, CommentKind::SingleLine),
Comment::new(18, 23, CommentKind::SingleLine),
]
.into_boxed_slice();
let full_len = comments.len();
Expand Down
Loading

0 comments on commit f431e04

Please sign in to comment.