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
16 changes: 6 additions & 10 deletions crates/oxc_formatter/src/write/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,7 @@ impl<'a> Format<'a> for FormatClass<'a, '_> {

let head = format_with(|f| {
if let Some(id) = self.id() {
write!(f, [space()]);

if self.type_parameters.is_some()
|| self.super_class.is_some()
|| !self.implements.is_empty()
{
id.fmt(f);
} else {
id.write(f);
}
write!(f, [space(), id]);
}

if let Some(type_parameters) = &type_parameters {
Expand Down Expand Up @@ -458,6 +449,11 @@ impl<'a> Format<'a> for FormatClass<'a, '_> {
write!(f, [head, format_heritage_clauses, space()]);
}

let leading_comments = f.context().comments().comments_before(self.body.span.start);
if leading_comments.iter().any(|c| !c.is_line()) {
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks if there are any block comments (!c.is_line()), but then formats ALL leading comments, including line comments. This seems inconsistent - either filter to only format block comments, or change the condition to check if there are any comments at all.

Consider one of these approaches:

  1. Filter the comments: write!(f, FormatLeadingComments::Comments(&leading_comments.iter().filter(|c| !c.is_line()).collect::<Vec<_>>()));
  2. Or simplify the condition: if !leading_comments.is_empty() { ... }
Suggested change
if leading_comments.iter().any(|c| !c.is_line()) {
if !leading_comments.is_empty() {

Copilot uses AI. Check for mistakes.
write!(f, FormatLeadingComments::Comments(leading_comments));
}

if body.body.is_empty() {
write!(f, ["{", format_dangling_comments(self.span).with_block_indent(), "}"]);
} else {
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/class/issue-16174.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class Cls /* YES */ {
// class body comment
}


export class Mutliline /* Y
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test class name: "Mutliline" should be "Multiline".

Suggested change
export class Mutliline /* Y
export class Multiline /* Y

Copilot uses AI. Check for mistakes.
E
S */ {
// class body comment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
export class Cls /* YES */ {
// class body comment
}


export class Mutliline /* Y
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test class name: "Mutliline" should be "Multiline".

Copilot uses AI. Check for mistakes.
E
S */ {
// class body comment
}

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
export class Cls /* YES */ {
// class body comment
}

export class Mutliline /* Y
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test class name: "Mutliline" should be "Multiline".

Copilot uses AI. Check for mistakes.
E
S */ {
// class body comment
}

-------------------
{ printWidth: 100 }
-------------------
export class Cls /* YES */ {
// class body comment
}

export class Mutliline /* Y
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test class name: "Mutliline" should be "Multiline".

Copilot uses AI. Check for mistakes.
E
S */ {
// class body comment
}

===================== End =====================
Loading