Skip to content

Commit 620dbac

Browse files
committed
fix(formatter): no need to wrap a parenthesis for TSConditionalType and TSFunctionType when its parent is TSUnionType and it only has one element (#14540)
1 parent 97bb964 commit 620dbac

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

crates/oxc_formatter/src/parentheses/ts_type.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ fn function_like_type_needs_parentheses<'a>(
104104
}
105105
false
106106
}
107-
AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true,
107+
AstNodes::TSUnionType(union) => union.types.len() > 1,
108+
AstNodes::TSIntersectionType(intersection) => intersection.types.len() > 1,
108109
_ => operator_type_or_higher_needs_parens(span, parent),
109110
}
110111
}
@@ -142,7 +143,8 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSConditionalType<'a>> {
142143
AstNodes::TSConditionalType(ty) => {
143144
ty.extends_type().span() == self.span() || ty.check_type().span() == self.span()
144145
}
145-
AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true,
146+
AstNodes::TSUnionType(union) => union.types.len() > 1,
147+
AstNodes::TSIntersectionType(intersection) => intersection.types.len() > 1,
146148
_ => operator_type_or_higher_needs_parens(self.span, self.parent),
147149
}
148150
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type T1<B> = | (B extends any ? number : string);
2+
type T2 = | (() => void);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
type T1<B> = | (B extends any ? number : string);
6+
type T2 = | (() => void);
7+
8+
==================== Output ====================
9+
type T1<B> = B extends any ? number : string;
10+
type T2 = () => void;
11+
12+
===================== End =====================

0 commit comments

Comments
 (0)