Skip to content

Commit f7727c7

Browse files
committed
fix(formatter): ignore comment doesn't work for the expression statement (#14817)
* close #14808
1 parent 9d914a3 commit f7727c7

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

crates/oxc_formatter/src/ast_nodes/generated/format.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,8 +1702,10 @@ impl<'a> Format<'a> for AstNode<'a, EmptyStatement> {
17021702

17031703
impl<'a> Format<'a> for AstNode<'a, ExpressionStatement<'a>> {
17041704
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1705+
let is_suppressed = f.comments().is_suppressed(self.span().start);
17051706
self.format_leading_comments(f)?;
1706-
let result = self.write(f);
1707+
let result =
1708+
if is_suppressed { FormatSuppressedNode(self.span()).fmt(f) } else { self.write(f) };
17071709
self.format_trailing_comments(f)?;
17081710
result
17091711
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// prettier-ignore
2+
Object.defineProperties ( exports , {
3+
})
4+
5+
/* prettier-ignore */
6+
(() =>
7+
c +
8+
b +
9+
d
10+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
// prettier-ignore
6+
Object.defineProperties ( exports , {
7+
})
8+
9+
/* prettier-ignore */
10+
(() =>
11+
c +
12+
b +
13+
d
14+
)
15+
==================== Output ====================
16+
// prettier-ignore
17+
Object.defineProperties ( exports , {
18+
})
19+
20+
/* prettier-ignore */
21+
(() =>
22+
c +
23+
b +
24+
d
25+
)
26+
27+
===================== End =====================

tasks/ast_tools/src/generators/formatter/format.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,12 @@ fn generate_struct_implementation(
168168

169169
// `Program` can't be suppressed.
170170
// `JSXElement` and `JSXFragment` implement suppression formatting in their formatting logic
171-
let suppressed_check = (!matches!(
172-
struct_name,
173-
"Program" | "JSXElement" | "JSXFragment" | "ExpressionStatement"
174-
))
175-
.then(|| {
176-
quote! {
177-
let is_suppressed = f.comments().is_suppressed(self.span().start);
178-
}
179-
});
171+
let suppressed_check = (!matches!(struct_name, "Program" | "JSXElement" | "JSXFragment"))
172+
.then(|| {
173+
quote! {
174+
let is_suppressed = f.comments().is_suppressed(self.span().start);
175+
}
176+
});
180177

181178
let write_implementation = if suppressed_check.is_none() {
182179
write_call

0 commit comments

Comments
 (0)