Skip to content

Commit b169e52

Browse files
committed
fix(formatter): correct printing of AsExpression and SatisfiesExpression (#16325)
Address https://github.com/prettier/prettier/tree/3.7.0/tests/format/typescript/as/comments/18160.ts As you may have guessed, this is a collaboration with Claude... I'll assign for review it after I understand, simplify the code a bit more and add some comments.
1 parent 82f8b9f commit b169e52

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

crates/oxc_formatter/src/formatter/comments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a> Comments<'a> {
209209
/// Returns comments that fall between the given start and end positions.
210210
pub fn comments_in_range(&self, start: u32, end: u32) -> &'a [Comment] {
211211
let comments = self.comments_after(start);
212-
let end_index = comments.iter().take_while(|c| c.span.end < end).count();
212+
let end_index = comments.iter().take_while(|c| c.span.end <= end).count();
213213
&comments[..end_index]
214214
}
215215

crates/oxc_formatter/src/write/as_or_satisfies_expression.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use oxc_span::GetSpan;
33

44
use crate::{
55
ast_nodes::{AstNode, AstNodes},
6-
formatter::{Formatter, prelude::*},
6+
formatter::{Formatter, prelude::*, trivia::FormatTrailingComments},
77
write,
8-
write::FormatWrite,
8+
write::{FormatNodeWithoutTrailingComments, FormatWrite},
99
};
1010

1111
impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> {
@@ -42,8 +42,31 @@ fn format_as_or_satisfies_expression<'a>(
4242
f: &mut Formatter<'_, 'a>,
4343
) {
4444
let format_inner = format_with(|f| {
45-
write!(f, [expression, space(), token(operation)]);
46-
write!(f, [space(), type_annotation]);
45+
let type_start = type_annotation.span().start;
46+
47+
// Check for block comments between expression and type.
48+
// Prettier's `handleBinaryCastExpressionComment()` handles these specially.
49+
// https://github.com/prettier/prettier/blob/fdfa6701767f5140a85902ecc9fb6444f5b4e3f8/src/language-js/comments/handle-comments.js#L1131
50+
// See also https://github.com/prettier/prettier/blob/3.7.3/tests/format/typescript/as/comments/18160.ts
51+
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));
54+
let block_comments =
55+
if let Some(pos) = multiline_comment_position { &comments[..pos] } else { comments };
56+
57+
if !comments.is_empty()
58+
&& let AstNodes::TSTypeReference(reference) = type_annotation.as_ast_nodes()
59+
&& reference.type_name.is_const()
60+
{
61+
write!(f, [FormatNodeWithoutTrailingComments(expression)]);
62+
write!(f, [FormatTrailingComments::Comments(block_comments)]);
63+
write!(f, [space(), token(operation), space(), token("const")]);
64+
} else if block_comments.is_empty() {
65+
write!(f, [FormatNodeWithoutTrailingComments(expression)]);
66+
write!(f, [space(), token(operation), space(), type_annotation]);
67+
} else {
68+
write!(f, [expression, space(), token(operation), space(), type_annotation]);
69+
}
4770
});
4871

4972
if is_callee_or_object {

tasks/prettier_conformance/snapshots/prettier.ts.snap.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 575/604 (95.20%)
1+
ts compatibility: 577/604 (95.53%)
22

33
# Failed
44

@@ -9,9 +9,7 @@ ts compatibility: 575/604 (95.20%)
99
| jsx/jsx/quotes.js | 💥💥💥💥 | 79.41% |
1010
| typescript/arrow/comments.ts | 💥✨ | 44.44% |
1111
| typescript/arrow/comments/issue-11100.ts | 💥 | 84.00% |
12-
| typescript/as/as-const/as-const.ts | 💥 | 90.91% |
1312
| typescript/as/break-after-keyword/18148.ts | 💥 | 82.22% |
14-
| typescript/as/comments/18160.ts | 💥 | 71.58% |
1513
| typescript/chain-expression/call-expression.ts | 💥 | 68.75% |
1614
| typescript/chain-expression/member-expression.ts | 💥 | 65.67% |
1715
| typescript/chain-expression/test.ts | 💥 | 0.00% |

0 commit comments

Comments
 (0)