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
21 changes: 7 additions & 14 deletions crates/oxc_formatter/src/utils/assignment_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl<'a> AssignmentLike<'a, '_> {
return AssignmentLikeLayout::BreakAfterOperator;
}

if self.should_break_left_hand_side() {
if self.should_break_left_hand_side(left_may_break) {
return AssignmentLikeLayout::BreakLeftHandSide;
}

Expand Down Expand Up @@ -518,7 +518,7 @@ impl<'a> AssignmentLike<'a, '_> {

/// Particular function that checks if the left hand side of a [AssignmentLike] should
/// be broken on multiple lines
fn should_break_left_hand_side(&self) -> bool {
fn should_break_left_hand_side(&self, left_may_break: bool) -> bool {
if self.is_complex_destructuring() {
return true;
}
Expand All @@ -534,9 +534,11 @@ impl<'a> AssignmentLike<'a, '_> {
let type_annotation = declarator.id.type_annotation.as_ref();

type_annotation.is_some_and(|ann| is_complex_type_annotation(ann))
|| (self.get_right_expression().is_some_and(|expr| {
matches!(expr.as_ref(), Expression::ArrowFunctionExpression(_))
}) && type_annotation.is_some_and(|ann| is_annotation_breakable(ann)))
|| (left_may_break
&& declarator
.init
.as_ref()
.is_some_and(|expr| matches!(expr, Expression::ArrowFunctionExpression(_))))
}

/// Checks if the current assignment is eligible for [AssignmentLikeLayout::BreakAfterOperator]
Expand Down Expand Up @@ -1009,15 +1011,6 @@ fn is_complex_type_arguments(type_arguments: &TSTypeParameterInstantiation) -> b
false
}

/// Checks if the annotation is breakable
fn is_annotation_breakable(annotation: &TSTypeAnnotation) -> bool {
matches!(
&annotation.type_annotation,
TSType::TSTypeReference(reference_type)
if reference_type.type_arguments.as_ref().is_some_and(|type_args| !type_args.params.is_empty())
)
}

/// [Prettier applies]: <https://github.com/prettier/prettier/blob/fde0b49d7866e203ca748c306808a87b7c15548f/src/language-js/print/assignment.js#L278>
pub fn is_complex_type_annotation(annotation: &TSTypeAnnotation) -> bool {
match &annotation.type_annotation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
const onPanning: ComponenASDtProps<
typeof TransformWrapper
>["onPanning"] = () => {
}
}
const onPanning: ComponenASDtProps<
typeof TransformWrapper
>["onPanning"] = () => {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
{
const onPanning: ComponenASDtProps<
typeof TransformWrapper
>["onPanning"] = () => {
}
}
const onPanning: ComponenASDtProps<
typeof TransformWrapper
>["onPanning"] = () => {
}

==================== Output ====================
{
const onPanning: ComponenASDtProps<
typeof TransformWrapper
>["onPanning"] = () => {};
}
const onPanning: ComponenASDtProps<
typeof TransformWrapper
>["onPanning"] = () => {};

===================== End =====================
Loading