diff --git a/crates/oxc_formatter/src/write/as_or_satisfies_expression.rs b/crates/oxc_formatter/src/write/as_or_satisfies_expression.rs new file mode 100644 index 0000000000000..f70fc0f4e83e0 --- /dev/null +++ b/crates/oxc_formatter/src/write/as_or_satisfies_expression.rs @@ -0,0 +1,68 @@ +use oxc_ast::ast::*; +use oxc_span::GetSpan; + +use crate::{ + format_args, + formatter::{FormatResult, Formatter, prelude::*}, + generated::ast_nodes::{AstNode, AstNodes}, + write, + write::FormatWrite, +}; + +impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> { + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { + let is_callee_or_object = is_callee_or_object_context(self.span(), self.parent); + format_as_or_satisfies_expression( + self.expression(), + self.type_annotation(), + is_callee_or_object, + "as", + f, + ) + } +} + +impl<'a> FormatWrite<'a> for AstNode<'a, TSSatisfiesExpression<'a>> { + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { + let is_callee_or_object = is_callee_or_object_context(self.span(), self.parent); + format_as_or_satisfies_expression( + self.expression(), + self.type_annotation(), + is_callee_or_object, + "satisfies", + f, + ) + } +} + +fn format_as_or_satisfies_expression<'a>( + expression: &AstNode<'a, Expression>, + type_annotation: &AstNode<'a, TSType>, + is_callee_or_object: bool, + operation: &'static str, + f: &mut Formatter<'_, 'a>, +) -> FormatResult<()> { + let format_inner = format_with(|f| { + write!(f, [expression, space(), text(operation)])?; + write!(f, [space(), type_annotation]) + }); + + if is_callee_or_object { + write!(f, [group(&soft_block_indent(&format_inner))]) + } else { + write!(f, [format_inner]) + } +} + +fn is_callee_or_object_context(span: Span, parent: &AstNodes<'_>) -> bool { + match parent { + // Callee + AstNodes::CallExpression(_) | AstNodes::NewExpression(_) + // Static member + | AstNodes::StaticMemberExpression(_) => true, + AstNodes::ComputedMemberExpression(member) => { + member.object.span() == span + } + _ => false, + } +} diff --git a/crates/oxc_formatter/src/write/mod.rs b/crates/oxc_formatter/src/write/mod.rs index 4f6d0d242baa6..bbeb4d96d0ed7 100644 --- a/crates/oxc_formatter/src/write/mod.rs +++ b/crates/oxc_formatter/src/write/mod.rs @@ -1,6 +1,7 @@ mod array_element_list; mod array_expression; mod arrow_function_expression; +mod as_or_satisfies_expression; mod assignment_pattern_property_list; mod binary_like_expression; mod binding_property_list; @@ -2004,18 +2005,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMappedType<'a>> { } } -impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> { - fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - write!(f, [self.expression(), " as ", self.type_annotation()]) - } -} - -impl<'a> FormatWrite<'a> for AstNode<'a, TSSatisfiesExpression<'a>> { - fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - write!(f, [self.expression(), " satisfies ", self.type_annotation()]) - } -} - impl<'a> FormatWrite<'a> for AstNode<'a, TSTypeAssertion<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { write!(f, "<")?; diff --git a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md index 93f9fa52dcfb1..a02648ecf98ea 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: 404/573 (70.51%) +ts compatibility: 406/573 (70.86%) # Failed @@ -19,7 +19,6 @@ ts compatibility: 404/573 (70.51%) | typescript/as/as.ts | 💥 | 85.04% | | typescript/as/assignment2.ts | 💥 | 94.12% | | typescript/as/expression-statement.ts | 💥 | 75.00% | -| typescript/as/nested-await-and-as.ts | 💥 | 42.86% | | typescript/assignment/issue-10846.ts | 💥 | 63.16% | | typescript/assignment/issue-10848.tsx | 💥 | 52.12% | | typescript/assignment/issue-10850.ts | 💥 | 50.00% | @@ -141,7 +140,6 @@ ts compatibility: 404/573 (70.51%) | typescript/satisfies-operators/assignment.ts | 💥💥 | 90.91% | | typescript/satisfies-operators/expression-statement.ts | 💥💥 | 78.38% | | typescript/satisfies-operators/lhs.ts | 💥✨ | 35.00% | -| typescript/satisfies-operators/nested-await-and-satisfies.ts | 💥💥 | 42.86% | | typescript/template-literal-types/template-literal-types.ts | 💥 | 80.00% | | typescript/test-declarations/test_declarations.ts | 💥💥 | 66.67% | | typescript/trailing-comma/arrow-functions.tsx | 💥💥💥 | 25.00% |