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
11 changes: 6 additions & 5 deletions crates/oxc_ast/src/ast/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub enum CommentKind {
/// Line comment
#[default]
Line = 0,
/// Block comment
Block = 1,
/// Singleline comment
#[estree(rename = "Block")]
SinglelineBlock = 1,
/// Multiline block comment (contains line breaks)
#[estree(rename = "Block")]
MultilineBlock = 2,
Expand Down Expand Up @@ -175,7 +176,7 @@ impl Comment {
pub fn content_span(&self) -> Span {
match self.kind {
CommentKind::Line => Span::new(self.span.start + 2, self.span.end),
CommentKind::Block | CommentKind::MultilineBlock => {
CommentKind::SinglelineBlock | CommentKind::MultilineBlock => {
Span::new(self.span.start + 2, self.span.end - 2)
}
}
Expand All @@ -187,10 +188,10 @@ impl Comment {
self.kind == CommentKind::Line
}

/// Returns `true` if this is a block comment.
/// Returns `true` if this is a singleline or multiline block comment.
#[inline]
pub fn is_block(self) -> bool {
matches!(self.kind, CommentKind::Block | CommentKind::MultilineBlock)
matches!(self.kind, CommentKind::SinglelineBlock | CommentKind::MultilineBlock)
}

/// Returns `true` if this is a multi-line block comment.
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3233,7 +3233,7 @@ impl ESTree for CommentKind {
fn serialize<S: Serializer>(&self, serializer: S) {
match self {
Self::Line => JsonSafeString("Line").serialize(serializer),
Self::Block => JsonSafeString("Block").serialize(serializer),
Self::SinglelineBlock => JsonSafeString("Block").serialize(serializer),
Self::MultilineBlock => JsonSafeString("Block").serialize(serializer),
}
}
Expand Down
16 changes: 10 additions & 6 deletions crates/oxc_ast/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,11 @@ mod test {

#[test]
fn test_is_inside_comment() {
let comments =
vec![Comment::new(0, 4, CommentKind::Line), Comment::new(10, 20, CommentKind::Block)]
.into_boxed_slice();
let comments = vec![
Comment::new(0, 4, CommentKind::Line),
Comment::new(10, 20, CommentKind::SinglelineBlock),
]
.into_boxed_slice();

assert!(is_inside_comment(&comments, 2));
assert!(!is_inside_comment(&comments, 5));
Expand All @@ -237,9 +239,11 @@ mod test {

#[test]
fn test_get_comment_at() {
let comments =
vec![Comment::new(0, 4, CommentKind::Line), Comment::new(10, 20, CommentKind::Block)]
.into_boxed_slice();
let comments = vec![
Comment::new(0, 4, CommentKind::Line),
Comment::new(10, 20, CommentKind::SinglelineBlock),
]
.into_boxed_slice();

// Inside first comment
let comment = get_comment_at(&comments, 2);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Codegen<'_> {
};
let comment_source = comment.span.source_text(source_text);
match comment.kind {
CommentKind::Line | CommentKind::Block => {
CommentKind::Line | CommentKind::SinglelineBlock => {
self.print_str_escaping_script_close_tag(comment_source);
}
CommentKind::MultilineBlock => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/formatter/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a> Format<'a> for FormatLeadingComments<'a> {
write!(f, comment);

match comment.kind {
CommentKind::Block | CommentKind::MultilineBlock => {
CommentKind::SinglelineBlock | CommentKind::MultilineBlock => {
match f.source_text().lines_after(comment.span.end) {
0 => {
let should_nestle =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
let single_line_comment: String = format!("//{comment}\n");
comments_vec.push(single_line_comment);
}
CommentKind::Block | CommentKind::MultilineBlock => {
CommentKind::SinglelineBlock | CommentKind::MultilineBlock => {
let multi_line_comment: String = format!("/*{comment}*/\n");
comments_vec.push(multi_line_comment);
}
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub fn convert_utf8_to_utf16(
Comment {
r#type: match comment.kind {
CommentKind::Line => String::from("Line"),
CommentKind::Block | CommentKind::MultilineBlock => String::from("Block"),
CommentKind::SinglelineBlock | CommentKind::MultilineBlock => {
String::from("Block")
}
},
value,
start: span.start,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/lexer/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<'a> Lexer<'a> {
self.trivia_builder.add_block_comment(
self.token.start(),
self.offset(),
CommentKind::Block,
CommentKind::SinglelineBlock,
self.source.whole(),
);
Kind::Skip
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_parser/src/lexer/trivia_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ mod test {
let expected = [
Comment {
span: Span::new(9, 24),
kind: CommentKind::Block,
kind: CommentKind::SinglelineBlock,
position: CommentPosition::Leading,
attached_to: 70,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
Expand All @@ -350,15 +350,15 @@ mod test {
},
Comment {
span: Span::new(54, 69),
kind: CommentKind::Block,
kind: CommentKind::SinglelineBlock,
position: CommentPosition::Leading,
attached_to: 70,
newlines: CommentNewlines::Leading,
content: CommentContent::None,
},
Comment {
span: Span::new(76, 92),
kind: CommentKind::Block,
kind: CommentKind::SinglelineBlock,
position: CommentPosition::Trailing,
attached_to: 0,
newlines: CommentNewlines::None,
Expand Down Expand Up @@ -398,15 +398,15 @@ token /* Trailing 1 */
let expected = vec![
Comment {
span: Span::new(20, 35),
kind: CommentKind::Block,
kind: CommentKind::SinglelineBlock,
position: CommentPosition::Leading,
attached_to: 36,
newlines: CommentNewlines::Leading | CommentNewlines::Trailing,
content: CommentContent::None,
},
Comment {
span: Span::new(42, 58),
kind: CommentKind::Block,
kind: CommentKind::SinglelineBlock,
position: CommentPosition::Trailing,
attached_to: 0,
newlines: CommentNewlines::Trailing,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,10 @@ mod test {
let source_type = SourceType::default().with_typescript(true);
let sources = [
("// line comment", CommentKind::Line),
("/* line comment */", CommentKind::Block),
("/* line comment */", CommentKind::SinglelineBlock),
(
"type Foo = ( /* Require properties which are not generated automatically. */ 'bar')",
CommentKind::Block,
CommentKind::SinglelineBlock,
),
];
for (source, kind) in sources {
Expand Down
Loading