Skip to content

Commit ad5c18a

Browse files
authored
fix(formatter): Correct parentheses in TSIntersectionType (#14098)
1 parent 864fa0e commit ad5c18a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

crates/oxc_formatter/src/parentheses/ts_type.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ fn operator_type_or_higher_needs_parens(span: Span, parent: &AstNodes) -> bool {
126126

127127
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSIntersectionType<'a>> {
128128
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
129-
matches!(
130-
self.parent,
131-
AstNodes::TSArrayType(_)
132-
| AstNodes::TSTypeOperator(_)
133-
| AstNodes::TSIndexedAccessType(_)
134-
)
129+
match self.parent {
130+
AstNodes::TSUnionType(union) => self.types.len() > 1 && union.types.len() > 1,
131+
AstNodes::TSIntersectionType(intersection) => {
132+
self.types.len() > 1 && intersection.types.len() > 1
133+
}
134+
parent => operator_type_or_higher_needs_parens(self.span(), parent),
135+
}
135136
}
136137
}
137138

tasks/prettier_conformance/snapshots/prettier.ts.snap.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ ts compatibility: 533/573 (93.02%)
3131
| typescript/definite/without-annotation.ts | 💥 | 83.33% |
3232
| typescript/enum/computed-members.ts | 💥 | 0.00% |
3333
| typescript/interface/ignore.ts | 💥✨ | 40.09% |
34-
| typescript/intersection/intersection-parens.ts | 💥💥 | 80.85% |
34+
| typescript/intersection/intersection-parens.ts | 💥💥 | 86.17% |
3535
| typescript/intersection/consistent-with-flow/intersection-parens.ts | 💥 | 69.77% |
3636
| typescript/last-argument-expansion/decorated-function.tsx | 💥 | 29.06% |
3737
| typescript/multiparser-css/issue-6259.ts | 💥 | 57.14% |
3838
| typescript/non-null/optional-chain.ts | 💥 | 72.22% |
3939
| typescript/object-multiline/multiline.ts | 💥✨ | 23.21% |
4040
| typescript/prettier-ignore/mapped-types.ts | 💥 | 63.16% |
41-
| typescript/prettier-ignore/prettier-ignore-nested-unions.ts | 💥 | 44.00% |
41+
| typescript/prettier-ignore/prettier-ignore-nested-unions.ts | 💥 | 68.00% |
4242
| typescript/type-arguments-bit-shift-left-like/3.ts | 💥 | 0.00% |
4343
| typescript/type-arguments-bit-shift-left-like/5.tsx | 💥 | 0.00% |
4444
| typescript/union/union-parens.ts | 💥 | 92.59% |
45-
| typescript/union/consistent-with-flow/prettier-ignore.ts | 💥 | 60.00% |
45+
| typescript/union/consistent-with-flow/prettier-ignore.ts | 💥 | 88.00% |
4646
| typescript/union/single-type/single-type.ts | 💥 | 0.00% |

0 commit comments

Comments
 (0)