Skip to content

Commit 77c20fe

Browse files
committed
refactor(formatter): simplify checking multiline block comment
1 parent 4c95de1 commit 77c20fe

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

crates/oxc_formatter/src/formatter/trivia.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use oxc_span::Span;
6161

6262
use crate::write;
6363

64-
use super::{SourceText, prelude::*};
64+
use super::prelude::*;
6565

6666
/// Returns true if:
6767
/// - `next_comment` is Some, and
@@ -76,18 +76,12 @@ use super::{SourceText, prelude::*};
7676
/// There isn't much documentation about this behavior, but it is mentioned on the JSDoc repo
7777
/// for documentation: <https://github.com/jsdoc/jsdoc.github.io/issues/40>. Prettier also
7878
/// implements the same behavior: <https://github.com/prettier/prettier/pull/13445/files#diff-3d5eaa2a1593372823589e6e55e7ca905f7c64203ecada0aa4b3b0cdddd5c3ddR160-R178>
79-
#[expect(clippy::suspicious_operation_groupings)]
80-
// `current.span.end == next.span.start` is correct, which checks whether the next comment starts exactly where the current comment ends.
81-
fn should_nestle_adjacent_doc_comments(
82-
current: &Comment,
83-
next: &Comment,
84-
source_text: SourceText,
85-
) -> bool {
79+
fn should_nestle_adjacent_doc_comments(current: &Comment, next: &Comment) -> bool {
8680
matches!(current.content, CommentContent::Jsdoc)
8781
&& matches!(next.content, CommentContent::Jsdoc)
82+
&& current.is_multiline_block()
83+
&& next.is_multiline_block()
8884
&& current.span.end == next.span.start
89-
&& source_text.contains_newline(current.span)
90-
&& source_text.contains_newline(next.span)
9185
}
9286

9387
/// Formats the leading comments of `node`
@@ -120,11 +114,7 @@ impl<'a> Format<'a> for FormatLeadingComments<'a> {
120114
0 => {
121115
let should_nestle =
122116
leading_comments_iter.peek().is_some_and(|next_comment| {
123-
should_nestle_adjacent_doc_comments(
124-
comment,
125-
next_comment,
126-
f.source_text(),
127-
)
117+
should_nestle_adjacent_doc_comments(comment, next_comment)
128118
});
129119

130120
write!(f, [maybe_space(!should_nestle)]);
@@ -200,7 +190,7 @@ impl<'a> Format<'a> for FormatTrailingComments<'a> {
200190
total_lines_before += lines_before;
201191

202192
let should_nestle = previous_comment.is_some_and(|previous_comment| {
203-
should_nestle_adjacent_doc_comments(previous_comment, comment, f.source_text())
193+
should_nestle_adjacent_doc_comments(previous_comment, comment)
204194
});
205195

206196
// This allows comments at the end of nested structures:
@@ -371,11 +361,7 @@ impl<'a> Format<'a> for FormatDanglingComments<'a> {
371361
f.context_mut().comments_mut().increment_printed_count();
372362

373363
let should_nestle = previous_comment.is_some_and(|previous_comment| {
374-
should_nestle_adjacent_doc_comments(
375-
previous_comment,
376-
comment,
377-
f.source_text(),
378-
)
364+
should_nestle_adjacent_doc_comments(previous_comment, comment)
379365
});
380366

381367
write!(
@@ -432,7 +418,7 @@ impl<'a> Format<'a> for Comment {
432418
#[expect(clippy::cast_possible_truncation)]
433419
fn fmt(&self, f: &mut Formatter<'_, 'a>) {
434420
let source_text = f.source_text().text_for(&self.span).trim_end();
435-
if is_alignable_comment(source_text) {
421+
if is_alignable_comment(self, source_text) {
436422
let mut source_offset = self.span.start;
437423

438424
let mut lines = source_text.lines();
@@ -489,8 +475,8 @@ impl<'a> Format<'a> for Comment {
489475
/// */
490476
/// "#)));
491477
/// ```
492-
pub fn is_alignable_comment(source_text: &str) -> bool {
493-
if !source_text.contains('\n') {
478+
pub fn is_alignable_comment(comment: &Comment, source_text: &str) -> bool {
479+
if !comment.is_multiline_block() {
494480
return false;
495481
}
496482
source_text.lines().enumerate().all(|(index, line)| {

crates/oxc_formatter/src/write/as_or_satisfies_expression.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ fn format_as_or_satisfies_expression<'a>(
4949
// https://github.com/prettier/prettier/blob/fdfa6701767f5140a85902ecc9fb6444f5b4e3f8/src/language-js/comments/handle-comments.js#L1131
5050
// See also https://github.com/prettier/prettier/blob/3.7.3/tests/format/typescript/as/comments/18160.ts
5151
let comments = f.context().comments().comments_in_range(expression.span().end, type_start);
52-
let multiline_comment_position =
53-
comments.iter().position(|c| c.is_block() && f.source_text().contains_newline(c.span));
52+
let multiline_comment_position = comments.iter().position(|c| c.is_multiline_block());
5453
let block_comments =
5554
if let Some(pos) = multiline_comment_position { &comments[..pos] } else { comments };
5655

crates/oxc_formatter/src/write/return_or_throw_statement.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,22 @@ impl<'a> Format<'a> for FormatAdjacentArgument<'a, '_> {
114114
/// Traversing the left nodes is necessary in case the first node is parenthesized because
115115
/// parentheses will be removed (and be re-added by the return statement, but only if the argument breaks)
116116
fn has_argument_leading_comments(argument: &AstNode<Expression>, f: &Formatter<'_, '_>) -> bool {
117-
let source_text = f.source_text();
118-
119117
for left_side in ExpressionLeftSide::from(argument).iter() {
120118
let start = left_side.span().start;
121119
let comments = f.context().comments();
122120
let leading_comments = comments.comments_before(start);
123121

124-
if leading_comments.iter().any(|comment| {
125-
(comment.is_block() && source_text.contains_newline(comment.span))
126-
|| comment.followed_by_newline()
127-
}) {
122+
if leading_comments
123+
.iter()
124+
.any(|comment| comment.is_multiline_block() || comment.followed_by_newline())
125+
{
128126
return true;
129127
}
130128

131129
let is_own_line_comment_or_multi_line_comment = |leading_comments: &[Comment]| {
132-
leading_comments.iter().any(|comment| {
133-
comment.preceded_by_newline()
134-
|| (comment.is_block() && source_text.contains_newline(comment.span))
135-
})
130+
leading_comments
131+
.iter()
132+
.any(|comment| comment.is_multiline_block() || comment.preceded_by_newline())
136133
};
137134

138135
// Yield expressions only need to check the leading comments on the left side.

0 commit comments

Comments
 (0)