diff --git a/crates/oxc_formatter/src/write/mod.rs b/crates/oxc_formatter/src/write/mod.rs index 993b1e00ed2e6..fabe789224569 100644 --- a/crates/oxc_formatter/src/write/mod.rs +++ b/crates/oxc_formatter/src/write/mod.rs @@ -45,7 +45,7 @@ use oxc_ast::{AstKind, ast::*}; use oxc_span::GetSpan; use crate::{ - format_args, + best_fitting, format_args, formatter::{ Buffer, Format, FormatResult, Formatter, prelude::*, @@ -1968,13 +1968,37 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMappedType<'a>> { impl<'a> FormatWrite<'a> for AstNode<'a, TSTypeAssertion<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - write!(f, "<")?; - // var r = < (x: T) => T > ((x) => { return null; }); - // ^ make sure space is printed here. - if matches!(**self.type_annotation(), TSType::TSFunctionType(_)) { - write!(f, space())?; + let break_after_cast = !matches!( + self.expression, + Expression::ArrayExpression(_) | Expression::ObjectExpression(_) + ); + + let format_cast = format_with(|f| { + write!(f, ["<", group(&soft_block_indent(&self.type_annotation())), ">",]) + }); + + if break_after_cast { + let format_cast = format_cast.memoized(); + let format_expression = self.expression().memoized(); + + write!( + f, + [best_fitting![ + format_args!(format_cast, format_expression), + format_args!( + format_cast, + group(&format_args!( + text("("), + block_indent(&format_expression), + text(")") + )) + ), + format_args!(format_cast, format_expression) + ]] + ) + } else { + write!(f, [format_cast, self.expression()]) } - write!(f, [self.type_annotation(), ">", self.expression()]) } } diff --git a/tasks/coverage/snapshots/formatter_typescript.snap b/tasks/coverage/snapshots/formatter_typescript.snap index 4df8bdf2308b6..eadf1f8b975b2 100644 --- a/tasks/coverage/snapshots/formatter_typescript.snap +++ b/tasks/coverage/snapshots/formatter_typescript.snap @@ -2,7 +2,7 @@ commit: 261630d6 formatter_typescript Summary: AST Parsed : 8816/8816 (100.00%) -Positive Passed: 8789/8816 (99.69%) +Positive Passed: 8788/8816 (99.68%) Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts @@ -17,6 +17,8 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReus Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitShadowingInferNotRenamed.ts +Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAssertions3.ts +Unexpected token Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexport.tsx Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexportMissingAliasTarget.tsx diff --git a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md index c9c0dc01331b1..40a1df72c7e0f 100644 --- a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md +++ b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md @@ -1,4 +1,4 @@ -ts compatibility: 417/573 (72.77%) +ts compatibility: 419/573 (73.12%) # Failed @@ -21,7 +21,7 @@ ts compatibility: 417/573 (72.77%) | typescript/assignment/issue-10846.ts | 💥 | 57.14% | | typescript/assignment/issue-10848.tsx | 💥 | 52.12% | | typescript/assignment/issue-10850.ts | 💥 | 50.00% | -| typescript/cast/generic-cast.ts | 💥 | 39.24% | +| typescript/cast/generic-cast.ts | 💥 | 81.48% | | typescript/cast/tuple-and-record.ts | 💥 | 0.00% | | typescript/chain-expression/call-expression.ts | 💥 | 68.75% | | typescript/chain-expression/member-expression.ts | 💥 | 65.67% | @@ -42,7 +42,6 @@ ts compatibility: 417/573 (72.77%) | typescript/comments/type_literals.ts | 💥 | 68.97% | | typescript/comments/union.ts | 💥 | 75.00% | | typescript/compiler/anyIsAssignableToObject.ts | 💥 | 75.00% | -| typescript/compiler/castTest.ts | 💥 | 96.67% | | typescript/compiler/contextualSignatureInstantiation2.ts | 💥 | 88.89% | | typescript/compiler/indexSignatureWithInitializer.ts | 💥 | 75.00% | | typescript/compiler/mappedTypeWithCombinedTypeMappers.ts | 💥 | 91.89% | @@ -147,7 +146,6 @@ ts compatibility: 417/573 (72.77%) | typescript/type-arguments-bit-shift-left-like/3.ts | 💥 | 0.00% | | typescript/type-arguments-bit-shift-left-like/5.tsx | 💥 | 0.00% | | typescript/typeof/typeof.ts | 💥 | 25.00% | -| typescript/typeparams/class-method.ts | 💥 | 96.61% | | typescript/typeparams/const.ts | 💥 | 86.15% | | typescript/typeparams/line-breaking-after-extends-2.ts | 💥 | 20.00% | | typescript/typeparams/line-breaking-after-extends.ts | 💥 | 17.14% |