Skip to content

Commit 54f8a82

Browse files
committed
fix(formatter): print comments before fat arrow as-is for ArrowFunctionExpression
1 parent e454ee7 commit 54f8a82

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

crates/oxc_formatter/src/write/arrow_function_expression.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use crate::{
1010
Buffer, Comments, Format, FormatError, FormatResult, Formatter, SourceText,
1111
buffer::RemoveSoftLinesBuffer,
1212
prelude::*,
13-
trivia::{FormatLeadingComments, format_trailing_comments},
13+
trivia::{
14+
DanglingIndentMode, FormatDanglingComments, FormatLeadingComments,
15+
FormatTrailingComments, format_trailing_comments,
16+
},
1417
},
1518
options::FormatTrailingCommas,
1619
utils::{assignment_like::AssignmentLikeLayout, expression::ExpressionLeftSide},
@@ -559,7 +562,12 @@ impl<'a> Format<'a> for ArrowChain<'a, '_> {
559562
Ok(())
560563
});
561564

562-
write!(f, [group(&join_signatures).should_expand(*expand_signatures)])
565+
FormatContentWithCacheMode::new(
566+
self.head.span(),
567+
group(&join_signatures).should_expand(*expand_signatures),
568+
self.options.cache_mode,
569+
)
570+
.fmt(f)
563571
});
564572

565573
let format_tail_body_inner = format_with(|f| {
@@ -758,12 +766,12 @@ fn format_signature<'a, 'b>(
758766
)?;
759767
}
760768

761-
// TODO: for case `a = (x: any): x is string /* comment */ => {}`
762-
// if f.comments().has_dangling_comments(arrow.span()) {
763-
// write!(f, [space(), format_dangling_comments(arrow.span())])?;
764-
// }
765-
766-
Ok(())
769+
// Print comments before the fat arrow (`=>`)
770+
let comments_before_fat_arrow =
771+
f.context().comments().comments_before_character(arrow.params.span().end, b'=');
772+
let content =
773+
format_once(|f| FormatTrailingComments::Comments(comments_before_fat_arrow).fmt(f));
774+
write!(f, [FormatContentWithCacheMode::new(arrow.span, content, cache_mode)])
767775
})
768776
}
769777

crates/oxc_formatter/tests/fixtures/js/comments/arrow.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,24 @@
44

55
() =>
66
// comment
7-
({})
7+
({});
8+
9+
() /* comment1 */ => /* comment2 */ { };
10+
11+
() /**/ => //
12+
() /**/ => /**/
13+
() /**/ => /**/ {
14+
//
15+
}
16+
17+
call(
18+
() /* comment1 */ => /* comment2 */ { }
19+
);
20+
21+
call(
22+
() /**/ => //
23+
() /**/ => /**/
24+
() /**/ => /**/ {
25+
//
26+
}
27+
);

crates/oxc_formatter/tests/fixtures/js/comments/arrow.js.snap

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,27 @@ source: crates/oxc_formatter/tests/fixtures/mod.rs
88

99
() =>
1010
// comment
11-
({})
11+
({});
12+
13+
() /* comment1 */ => /* comment2 */ { };
14+
15+
() /**/ => //
16+
() /**/ => /**/
17+
() /**/ => /**/ {
18+
//
19+
}
20+
21+
call(
22+
() /* comment1 */ => /* comment2 */ { }
23+
);
24+
25+
call(
26+
() /**/ => //
27+
() /**/ => /**/
28+
() /**/ => /**/ {
29+
//
30+
}
31+
);
1232

1333
==================== Output ====================
1434
() =>
@@ -19,4 +39,24 @@ source: crates/oxc_formatter/tests/fixtures/mod.rs
1939
// comment
2040
({});
2141

42+
() /* comment1 */ => /* comment2 */ {};
43+
44+
() /**/ =>
45+
//
46+
() /**/ =>
47+
/**/
48+
() /**/ => /**/ {
49+
//
50+
};
51+
52+
call(() /* comment1 */ => /* comment2 */ {});
53+
54+
call(() /**/ =>
55+
//
56+
() /**/ =>
57+
/**/
58+
() /**/ => /**/ {
59+
//
60+
});
61+
2262
===================== End =====================

0 commit comments

Comments
 (0)