Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down Expand Up @@ -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 = < <T>(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()])
}
}

Expand Down
4 changes: 3 additions & 1 deletion tasks/coverage/snapshots/formatter_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions tasks/prettier_conformance/snapshots/prettier.ts.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ts compatibility: 417/573 (72.77%)
ts compatibility: 419/573 (73.12%)

# Failed

Expand All @@ -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% |
Expand All @@ -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% |
Expand Down Expand Up @@ -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% |
Expand Down
Loading