Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions crates/oxc_ast/src/ast/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ bitflags! {
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
/// State of newlines around a comment.
pub struct CommentNewlines: u8 {
/// No newlines before or after
const NONE = 0;
/// Preceded by a newline
const LEADING = 1 << 0;
const Leading = 1 << 0;
/// Followed by a newline
const TRAILING = 1 << 1;
/// Preceded and followed by a newline
const LEADING_AND_TRAILING = Self::LEADING.bits() | Self::TRAILING.bits();
const Trailing = 1 << 1;
/// No newlines before or after
const None = 0;
}
}

Expand Down Expand Up @@ -161,7 +159,7 @@ impl Comment {
attached_to: 0,
kind,
position: CommentPosition::Trailing,
newlines: CommentNewlines::NONE,
newlines: CommentNewlines::None,
annotation: CommentAnnotation::None,
}
}
Expand Down Expand Up @@ -255,31 +253,31 @@ impl Comment {
#[inline]
pub fn set_followed_by_newline(&mut self, followed_by_newline: bool) {
if followed_by_newline {
self.newlines.insert(CommentNewlines::TRAILING);
self.newlines.insert(CommentNewlines::Trailing);
} else {
self.newlines.remove(CommentNewlines::TRAILING);
self.newlines.remove(CommentNewlines::Trailing);
}
}

/// Sets the state of `newlines` to include/exclude a newline before the comment.
#[inline]
pub fn set_preceded_by_newline(&mut self, preceded_by_newline: bool) {
if preceded_by_newline {
self.newlines.insert(CommentNewlines::LEADING);
self.newlines.insert(CommentNewlines::Leading);
} else {
self.newlines.remove(CommentNewlines::LEADING);
self.newlines.remove(CommentNewlines::Leading);
}
}

/// Returns `true` if this comment is preceded by a newline.
#[inline]
pub fn preceded_by_newline(self) -> bool {
self.newlines.contains(CommentNewlines::LEADING)
self.newlines.contains(CommentNewlines::Leading)
}

/// Returns `true` if this comment is followed by a newline.
#[inline]
pub fn followed_by_newline(self) -> bool {
self.newlines.contains(CommentNewlines::TRAILING)
self.newlines.contains(CommentNewlines::Trailing)
}
}
28 changes: 14 additions & 14 deletions crates/oxc_parser/src/lexer/trivia_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,47 +268,47 @@ mod test {
kind: CommentKind::Block,
position: CommentPosition::Leading,
attached_to: 70,
newlines: CommentNewlines::LEADING_AND_TRAILING,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(33, 45),
kind: CommentKind::Line,
position: CommentPosition::Leading,
attached_to: 70,
newlines: CommentNewlines::LEADING_AND_TRAILING,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(54, 69),
kind: CommentKind::Block,
position: CommentPosition::Leading,
attached_to: 70,
newlines: CommentNewlines::LEADING,
newlines: CommentNewlines::Leading,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(76, 92),
kind: CommentKind::Block,
position: CommentPosition::Trailing,
attached_to: 0,
newlines: CommentNewlines::NONE,
newlines: CommentNewlines::None,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(93, 106),
kind: CommentKind::Line,
position: CommentPosition::Trailing,
attached_to: 0,
newlines: CommentNewlines::TRAILING,
newlines: CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(115, 138),
kind: CommentKind::Line,
position: CommentPosition::Leading,
attached_to: 147,
newlines: CommentNewlines::LEADING_AND_TRAILING,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
];
Expand All @@ -332,15 +332,15 @@ token /* Trailing 1 */
kind: CommentKind::Block,
position: CommentPosition::Leading,
attached_to: 36,
newlines: CommentNewlines::LEADING_AND_TRAILING,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(42, 58),
kind: CommentKind::Block,
position: CommentPosition::Trailing,
attached_to: 0,
newlines: CommentNewlines::TRAILING,
newlines: CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
];
Expand All @@ -365,15 +365,15 @@ token /* Trailing 1 */
kind: CommentKind::Block,
position: CommentPosition::Leading,
attached_to: 28,
newlines: CommentNewlines::LEADING_AND_TRAILING,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(14, 26),
kind: CommentKind::Block,
position: CommentPosition::Leading,
attached_to: 28,
newlines: CommentNewlines::LEADING_AND_TRAILING,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
];
Expand All @@ -396,15 +396,15 @@ token /* Trailing 1 */
kind: CommentKind::Line,
position: CommentPosition::Leading,
attached_to: 57,
newlines: CommentNewlines::TRAILING,
newlines: CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(96, 116),
kind: CommentKind::Line,
position: CommentPosition::Leading,
attached_to: 129,
newlines: CommentNewlines::TRAILING,
newlines: CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
];
Expand All @@ -426,15 +426,15 @@ token /* Trailing 1 */
kind: CommentKind::Line,
position: CommentPosition::Leading,
attached_to: 55,
newlines: CommentNewlines::TRAILING,
newlines: CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
Comment {
span: Span::new(79, 99),
kind: CommentKind::Line,
position: CommentPosition::Leading,
attached_to: 116,
newlines: CommentNewlines::TRAILING,
newlines: CommentNewlines::Trailing,
annotation: CommentAnnotation::None,
},
];
Expand Down
Loading