Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support trailing ignore comments
  • Loading branch information
alubbe authored and Dunqing committed Mar 2, 2026
commit 93e5bc1bf05c1622ebdd1348218c195400c2e105
10 changes: 10 additions & 0 deletions crates/oxc_formatter/src/formatter/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,16 @@ impl<'a> Comments<'a> {
self.comments_before(start).iter().any(|comment| self.is_suppression_comment(comment))
}

/// Checks if there is a trailing line suppression comment on the same line.
///
/// This supports patterns like:
/// `statement(); // prettier-ignore`
pub fn has_line_suppression_comment_at_end_of_line(&self, pos: u32) -> bool {
self.end_of_line_comments_after(pos)
.iter()
.any(|comment| comment.is_line() && self.is_suppression_comment(comment))
}

/// Checks if a comment is a suppression comment (`oxfmt-ignore`).
///
/// `prettier-ignore` is also supported for compatibility.
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_formatter/src/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ use crate::{
object::{format_property_key, should_preserve_quote},
statement_body::FormatStatementBody,
string::{FormatLiteralStringToken, StringLiteralParentKind},
suppressed::FormatSuppressedNode,
tailwindcss::{tailwind_context_for_string_literal, write_tailwind_string_literal},
},
write,
Expand Down Expand Up @@ -556,13 +557,21 @@ fn expression_statement_needs_semicolon<'a>(

impl<'a> FormatWrite<'a> for AstNode<'a, ExpressionStatement<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) {
let span = self.span();
// Check if we need a leading semicolon to prevent ASI issues
if f.options().semicolons == Semicolons::AsNeeded
&& expression_statement_needs_semicolon(self, f)
{
write!(f, ";");
}

if f.comments().has_line_suppression_comment_at_end_of_line(span.end) {
// Preserve original text when the statement has an inline suppression comment:
// `stmt(); // prettier-ignore`
write!(f, [FormatSuppressedNode(span)]);
return;
}

write!(f, [self.expression(), OptionalSemicolon]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ Object.defineProperties ( exports , {
c +
b +
d
)
)

logger({ level: 'debug', message: `Really long debugging message about how ${user} called a certain part of our application with a certain ${payload}` }); // oxfmt-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Object.defineProperties ( exports , {
b +
d
)
logger({ level: 'debug', message: `Really long debugging message about how ${user} called a certain part of our application with a certain ${payload}` }); // oxfmt-ignore
==================== Output ====================
------------------
{ printWidth: 80 }
Expand All @@ -26,6 +27,7 @@ Object.defineProperties ( exports , {
b +
d
)
logger({ level: 'debug', message: `Really long debugging message about how ${user} called a certain part of our application with a certain ${payload}` }); // oxfmt-ignore

-------------------
{ printWidth: 100 }
Expand All @@ -40,5 +42,6 @@ Object.defineProperties ( exports , {
b +
d
)
logger({ level: 'debug', message: `Really long debugging message about how ${user} called a certain part of our application with a certain ${payload}` }); // oxfmt-ignore

===================== End =====================
4 changes: 2 additions & 2 deletions crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function a() {
};

function giveMeSome() {
a(a); // oxfmt-ignore
a( a ); // oxfmt-ignore
// shouldn't I return something? :shrug:
}

Expand Down Expand Up @@ -159,7 +159,7 @@ function a() {
};

function giveMeSome() {
a(a); // oxfmt-ignore
a( a ); // oxfmt-ignore
// shouldn't I return something? :shrug:
}

Expand Down