Skip to content

Commit 9b46dd7

Browse files
committed
feat(formatter): implement formatting for TSTypeAssertion (#13911)
1 parent 5710b13 commit 9b46dd7

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use oxc_ast::{AstKind, ast::*};
4545
use oxc_span::GetSpan;
4646

4747
use crate::{
48-
format_args,
48+
best_fitting, format_args,
4949
formatter::{
5050
Buffer, Format, FormatResult, Formatter,
5151
prelude::*,
@@ -1968,13 +1968,37 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMappedType<'a>> {
19681968

19691969
impl<'a> FormatWrite<'a> for AstNode<'a, TSTypeAssertion<'a>> {
19701970
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1971-
write!(f, "<")?;
1972-
// var r = < <T>(x: T) => T > ((x) => { return null; });
1973-
// ^ make sure space is printed here.
1974-
if matches!(**self.type_annotation(), TSType::TSFunctionType(_)) {
1975-
write!(f, space())?;
1971+
let break_after_cast = !matches!(
1972+
self.expression,
1973+
Expression::ArrayExpression(_) | Expression::ObjectExpression(_)
1974+
);
1975+
1976+
let format_cast = format_with(|f| {
1977+
write!(f, ["<", group(&soft_block_indent(&self.type_annotation())), ">",])
1978+
});
1979+
1980+
if break_after_cast {
1981+
let format_cast = format_cast.memoized();
1982+
let format_expression = self.expression().memoized();
1983+
1984+
write!(
1985+
f,
1986+
[best_fitting![
1987+
format_args!(format_cast, format_expression),
1988+
format_args!(
1989+
format_cast,
1990+
group(&format_args!(
1991+
text("("),
1992+
block_indent(&format_expression),
1993+
text(")")
1994+
))
1995+
),
1996+
format_args!(format_cast, format_expression)
1997+
]]
1998+
)
1999+
} else {
2000+
write!(f, [format_cast, self.expression()])
19762001
}
1977-
write!(f, [self.type_annotation(), ">", self.expression()])
19782002
}
19792003
}
19802004

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 261630d6
22

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8789/8816 (99.69%)
5+
Positive Passed: 8788/8816 (99.68%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
@@ -17,6 +17,8 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReus
1717

1818
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitShadowingInferNotRenamed.ts
1919

20+
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAssertions3.ts
21+
Unexpected token
2022
Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexport.tsx
2123

2224
Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexportMissingAliasTarget.tsx

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 417/573 (72.77%)
1+
ts compatibility: 419/573 (73.12%)
22

33
# Failed
44

@@ -21,7 +21,7 @@ ts compatibility: 417/573 (72.77%)
2121
| typescript/assignment/issue-10846.ts | 💥 | 57.14% |
2222
| typescript/assignment/issue-10848.tsx | 💥 | 52.12% |
2323
| typescript/assignment/issue-10850.ts | 💥 | 50.00% |
24-
| typescript/cast/generic-cast.ts | 💥 | 39.24% |
24+
| typescript/cast/generic-cast.ts | 💥 | 81.48% |
2525
| typescript/cast/tuple-and-record.ts | 💥 | 0.00% |
2626
| typescript/chain-expression/call-expression.ts | 💥 | 68.75% |
2727
| typescript/chain-expression/member-expression.ts | 💥 | 65.67% |
@@ -42,7 +42,6 @@ ts compatibility: 417/573 (72.77%)
4242
| typescript/comments/type_literals.ts | 💥 | 68.97% |
4343
| typescript/comments/union.ts | 💥 | 75.00% |
4444
| typescript/compiler/anyIsAssignableToObject.ts | 💥 | 75.00% |
45-
| typescript/compiler/castTest.ts | 💥 | 96.67% |
4645
| typescript/compiler/contextualSignatureInstantiation2.ts | 💥 | 88.89% |
4746
| typescript/compiler/indexSignatureWithInitializer.ts | 💥 | 75.00% |
4847
| typescript/compiler/mappedTypeWithCombinedTypeMappers.ts | 💥 | 91.89% |
@@ -147,7 +146,6 @@ ts compatibility: 417/573 (72.77%)
147146
| typescript/type-arguments-bit-shift-left-like/3.ts | 💥 | 0.00% |
148147
| typescript/type-arguments-bit-shift-left-like/5.tsx | 💥 | 0.00% |
149148
| typescript/typeof/typeof.ts | 💥 | 25.00% |
150-
| typescript/typeparams/class-method.ts | 💥 | 96.61% |
151149
| typescript/typeparams/const.ts | 💥 | 86.15% |
152150
| typescript/typeparams/line-breaking-after-extends-2.ts | 💥 | 20.00% |
153151
| typescript/typeparams/line-breaking-after-extends.ts | 💥 | 17.14% |

0 commit comments

Comments
 (0)