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
68 changes: 68 additions & 0 deletions crates/oxc_formatter/src/write/as_or_satisfies_expression.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
13 changes: 1 addition & 12 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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, "<")?;
Expand Down
4 changes: 1 addition & 3 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: 404/573 (70.51%)
ts compatibility: 406/573 (70.86%)

# Failed

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