Skip to content

Commit 56d1880

Browse files
committed
1 parent 3be5d94 commit 56d1880

3 files changed

Lines changed: 85 additions & 16 deletions

File tree

‎crates/oxc_formatter/src/formatter/trivia.rs‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,23 @@ use crate::{JsLabels, write};
8888

8989
use super::prelude::*;
9090

91-
/// Returns true if:
92-
/// - `next_comment` is Some, and
93-
/// - both comments are documentation comments, and
94-
/// - both comments are multiline, and
95-
/// - the two comments are immediately adjacent to each other, with no characters between them.
91+
/// Returns true if both comments are alignable and immediately adjacent, with no characters between them.
9692
///
9793
/// In this case, the comments are considered "nestled" - a pattern that JSDoc uses to represent
9894
/// overloaded types, which get merged together to create the final type for the subject. The
9995
/// comments must be kept immediately adjacent after formatting to preserve this behavior.
10096
///
10197
/// There isn't much documentation about this behavior, but it is mentioned on the JSDoc repo
102-
/// for documentation: <https://github.com/jsdoc/jsdoc.github.io/issues/40>. Prettier also
103-
/// implements the same behavior: <https://github.com/prettier/prettier/pull/13445/files#diff-3d5eaa2a1593372823589e6e55e7ca905f7c64203ecada0aa4b3b0cdddd5c3ddR160-R178>
104-
fn should_nestle_adjacent_doc_comments(current: &Comment, next: &Comment) -> bool {
105-
matches!(current.content, CommentContent::Jsdoc)
106-
&& matches!(next.content, CommentContent::Jsdoc)
107-
&& current.is_multiline_block()
108-
&& next.is_multiline_block()
109-
&& current.span.end == next.span.start
98+
/// for documentation: <https://github.com/jsdoc/jsdoc.github.io/issues/40>.
99+
/// Like Prettier's `mergeNestledJsdocComments`, alignable is the condition, not JSDoc.
100+
fn should_nestle_adjacent_comments(
101+
current: &Comment,
102+
next: &Comment,
103+
source_text: SourceText,
104+
) -> bool {
105+
current.span.end == next.span.start
106+
&& is_alignable_block_comment(current, source_text)
107+
&& is_alignable_block_comment(next, source_text)
110108
}
111109

112110
/// Formats the leading comments of `node`
@@ -166,7 +164,11 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatLeadingComments<'a> {
166164
0 if is_block => {
167165
let should_nestle =
168166
leading_comments_iter.peek().is_some_and(|next_comment| {
169-
should_nestle_adjacent_doc_comments(comment, next_comment)
167+
should_nestle_adjacent_comments(
168+
comment,
169+
next_comment,
170+
f.source_text(),
171+
)
170172
});
171173

172174
write!(f, [maybe_space(!should_nestle)]);
@@ -270,7 +272,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatTrailingComments<'a> {
270272
total_lines_before += lines_before;
271273

272274
let should_nestle = previous_comment.is_some_and(|previous_comment| {
273-
should_nestle_adjacent_doc_comments(previous_comment, comment)
275+
should_nestle_adjacent_comments(previous_comment, comment, f.source_text())
274276
});
275277

276278
// An own-line comment at the end of a nested structure:
@@ -449,7 +451,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatDanglingComments<'a> {
449451
f.context_mut().comments_mut().increment_printed_count();
450452

451453
let should_nestle = previous_comment.is_some_and(|previous_comment| {
452-
should_nestle_adjacent_doc_comments(previous_comment, comment)
454+
should_nestle_adjacent_comments(previous_comment, comment, f.source_text())
453455
});
454456

455457
write!(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* a
3+
*//*
4+
* b
5+
*/
6+
foo();
7+
8+
/**
9+
* @license a
10+
*//**
11+
* b
12+
*/
13+
bar();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
/*
6+
* a
7+
*//*
8+
* b
9+
*/
10+
foo();
11+
12+
/**
13+
* @license a
14+
*//**
15+
* b
16+
*/
17+
bar();
18+
19+
==================== Output ====================
20+
------------------
21+
{ printWidth: 80 }
22+
------------------
23+
/*
24+
* a
25+
*//*
26+
* b
27+
*/
28+
foo();
29+
30+
/**
31+
* @license a
32+
*//**
33+
* b
34+
*/
35+
bar();
36+
37+
-------------------
38+
{ printWidth: 100 }
39+
-------------------
40+
/*
41+
* a
42+
*//*
43+
* b
44+
*/
45+
foo();
46+
47+
/**
48+
* @license a
49+
*//**
50+
* b
51+
*/
52+
bar();
53+
54+
===================== End =====================

0 commit comments

Comments
 (0)