Skip to content

Commit a1ad9c5

Browse files
committed
fix(formatter): add parentheses for TSUnionType inside TSArrayType (#13792)
1 parent 34e7000 commit a1ad9c5

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

crates/oxc_formatter/src/parentheses/ts_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSType<'a>> {
1414
AstNodes::TSFunctionType(it) => it.needs_parentheses(f),
1515
AstNodes::TSInferType(it) => it.needs_parentheses(f),
1616
AstNodes::TSConstructorType(it) => it.needs_parentheses(f),
17+
AstNodes::TSUnionType(it) => it.needs_parentheses(f),
1718
_ => {
1819
// TODO: incomplete
1920
false
@@ -40,3 +41,9 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSConstructorType<'a>> {
4041
matches!(self.parent, AstNodes::TSUnionType(_))
4142
}
4243
}
44+
45+
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSUnionType<'a>> {
46+
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
47+
matches!(self.parent, AstNodes::TSArrayType(_))
48+
}
49+
}

crates/oxc_formatter/src/write/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,13 +1199,18 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConditionalType<'a>> {
11991199
impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
12001200
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
12011201
let mut types = self.types().iter();
1202+
if self.needs_parentheses(f) {
1203+
write!(f, "(")?;
1204+
}
12021205
if let Some(item) = types.next() {
12031206
write!(f, item)?;
12041207

12051208
for item in types {
12061209
write!(f, [" | ", item])?;
12071210
}
1208-
return Ok(());
1211+
}
1212+
if self.needs_parentheses(f) {
1213+
write!(f, ")")?;
12091214
}
12101215
Ok(())
12111216
}

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 261630d6
22

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8732/8816 (99.05%)
5+
Positive Passed: 8733/8816 (99.06%)
66
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/APISample_jsdoc.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/aliasInstantiationExpressionGenericIntersectionNoCrash1.ts
@@ -163,8 +163,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/objectT
163163

164164
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/rest/genericObjectRest.ts
165165

166-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/specifyingTypes/typeLiterals/parenthesizedTypes.ts
167-
168166
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts
169167

170168
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbols.ts

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 349/573 (60.91%)
1+
ts compatibility: 350/573 (61.08%)
22

33
# Failed
44

@@ -9,7 +9,7 @@ ts compatibility: 349/573 (60.91%)
99
| jsx/jsx/quotes.js | 💥💥💥💥 | 79.41% |
1010
| jsx/single-attribute-per-line/single-attribute-per-line.js | 💥✨ | 43.37% |
1111
| jsx/text-wrap/test.js | 💥 | 99.56% |
12-
| typescript/ambient/ambient.ts | 💥 | 88.24% |
12+
| typescript/ambient/ambient.ts | 💥 | 94.12% |
1313
| typescript/angular-component-examples/15934-computed.component.ts | 💥💥 | 76.92% |
1414
| typescript/angular-component-examples/15934.component.ts | 💥💥 | 53.85% |
1515
| typescript/angular-component-examples/test.component.ts | 💥💥 | 41.18% |
@@ -219,9 +219,8 @@ ts compatibility: 349/573 (60.91%)
219219
| typescript/typeparams/trailing-comma/type-paramters.ts | 💥💥💥 | 28.57% |
220220
| typescript/union/comments.ts | 💥 | 15.38% |
221221
| typescript/union/inlining.ts | 💥 | 36.22% |
222-
| typescript/union/union-parens.ts | 💥 | 55.00% |
222+
| typescript/union/union-parens.ts | 💥 | 56.00% |
223223
| typescript/union/with-type-params.ts | 💥 | 0.00% |
224-
| typescript/union/consistent-with-flow/comment.ts | 💥 | 0.00% |
225224
| typescript/union/consistent-with-flow/prettier-ignore.ts | 💥 | 19.05% |
226225
| typescript/union/consistent-with-flow/single-type.ts | 💥 | 3.39% |
227226
| typescript/union/consistent-with-flow/within-tuple.ts | 💥 | 16.51% |

0 commit comments

Comments
 (0)