Skip to content

Commit e4ae613

Browse files
committed
feat(formatter): implement formatting for TSAsExpression and TSSatisfiesExpression
1 parent 8b7f7c3 commit e4ae613

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use oxc_ast::ast::*;
2+
use oxc_span::GetSpan;
3+
4+
use crate::{
5+
format_args,
6+
formatter::{FormatResult, Formatter, prelude::*},
7+
generated::ast_nodes::{AstNode, AstNodes},
8+
write,
9+
write::FormatWrite,
10+
};
11+
12+
impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> {
13+
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
14+
let is_callee_or_object = is_callee_or_object_context(self.span(), self.parent);
15+
format_as_or_satisfies_expression(
16+
self.expression(),
17+
self.type_annotation(),
18+
is_callee_or_object,
19+
"as",
20+
f,
21+
)
22+
}
23+
}
24+
25+
impl<'a> FormatWrite<'a> for AstNode<'a, TSSatisfiesExpression<'a>> {
26+
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
27+
let is_callee_or_object = is_callee_or_object_context(self.span(), self.parent);
28+
format_as_or_satisfies_expression(
29+
self.expression(),
30+
self.type_annotation(),
31+
is_callee_or_object,
32+
"satisfies",
33+
f,
34+
)
35+
}
36+
}
37+
38+
fn format_as_or_satisfies_expression<'a>(
39+
expression: &AstNode<'a, Expression>,
40+
type_annotation: &AstNode<'a, TSType>,
41+
is_callee_or_object: bool,
42+
operation: &'static str,
43+
f: &mut Formatter<'_, 'a>,
44+
) -> FormatResult<()> {
45+
let format_inner = format_with(|f| {
46+
write!(f, [expression, space(), text(operation)])?;
47+
write!(f, [space(), type_annotation])
48+
});
49+
50+
if is_callee_or_object {
51+
write!(f, [group(&soft_block_indent(&format_inner))])
52+
} else {
53+
write!(f, [format_inner])
54+
}
55+
}
56+
57+
fn is_callee_or_object_context(span: Span, parent: &AstNodes<'_>) -> bool {
58+
match parent {
59+
// Callee
60+
AstNodes::CallExpression(_) | AstNodes::NewExpression(_)
61+
// Static member
62+
| AstNodes::StaticMemberExpression(_) => true,
63+
AstNodes::ComputedMemberExpression(member) => {
64+
member.object.span() == span
65+
}
66+
_ => false,
67+
}
68+
}

crates/oxc_formatter/src/write/mod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod array_element_list;
22
mod array_expression;
33
mod arrow_function_expression;
4+
mod as_or_satisfies_expression;
45
mod assignment_pattern_property_list;
56
mod binary_like_expression;
67
mod binding_property_list;
@@ -2004,18 +2005,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMappedType<'a>> {
20042005
}
20052006
}
20062007

2007-
impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> {
2008-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
2009-
write!(f, [self.expression(), " as ", self.type_annotation()])
2010-
}
2011-
}
2012-
2013-
impl<'a> FormatWrite<'a> for AstNode<'a, TSSatisfiesExpression<'a>> {
2014-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
2015-
write!(f, [self.expression(), " satisfies ", self.type_annotation()])
2016-
}
2017-
}
2018-
20192008
impl<'a> FormatWrite<'a> for AstNode<'a, TSTypeAssertion<'a>> {
20202009
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
20212010
write!(f, "<")?;

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: 404/573 (70.51%)
1+
ts compatibility: 406/573 (70.86%)
22

33
# Failed
44

@@ -19,7 +19,6 @@ ts compatibility: 404/573 (70.51%)
1919
| typescript/as/as.ts | 💥 | 85.04% |
2020
| typescript/as/assignment2.ts | 💥 | 94.12% |
2121
| typescript/as/expression-statement.ts | 💥 | 75.00% |
22-
| typescript/as/nested-await-and-as.ts | 💥 | 42.86% |
2322
| typescript/assignment/issue-10846.ts | 💥 | 63.16% |
2423
| typescript/assignment/issue-10848.tsx | 💥 | 52.12% |
2524
| typescript/assignment/issue-10850.ts | 💥 | 50.00% |
@@ -141,7 +140,6 @@ ts compatibility: 404/573 (70.51%)
141140
| typescript/satisfies-operators/assignment.ts | 💥💥 | 90.91% |
142141
| typescript/satisfies-operators/expression-statement.ts | 💥💥 | 78.38% |
143142
| typescript/satisfies-operators/lhs.ts | 💥✨ | 35.00% |
144-
| typescript/satisfies-operators/nested-await-and-satisfies.ts | 💥💥 | 42.86% |
145143
| typescript/template-literal-types/template-literal-types.ts | 💥 | 80.00% |
146144
| typescript/test-declarations/test_declarations.ts | 💥💥 | 66.67% |
147145
| typescript/trailing-comma/arrow-functions.tsx | 💥💥💥 | 25.00% |

0 commit comments

Comments
 (0)