Skip to content

Commit 9998c8a

Browse files
nnnnoelDunqing
authored andcommitted
fix(formatter): Add parentheses inside TSIntersectionType
1 parent 7d0b8a1 commit 9998c8a

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

crates/oxc_formatter/src/parentheses/ts_type.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSType<'a>> {
1616
AstNodes::TSInferType(it) => it.needs_parentheses(f),
1717
AstNodes::TSConstructorType(it) => it.needs_parentheses(f),
1818
AstNodes::TSUnionType(it) => it.needs_parentheses(f),
19+
AstNodes::TSIntersectionType(it) => it.needs_parentheses(f),
1920
AstNodes::TSConditionalType(it) => it.needs_parentheses(f),
2021
_ => {
2122
// TODO: incomplete
@@ -32,7 +33,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSFunctionType<'a>> {
3233
AstNodes::TSConditionalType(ty) => {
3334
ty.extends_type().span() == self.span() || ty.check_type().span() == self.span()
3435
}
35-
AstNodes::TSUnionType(_) => true,
36+
AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true,
3637
_ => false,
3738
}
3839
}
@@ -50,7 +51,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSConstructorType<'a>> {
5051
AstNodes::TSConditionalType(ty) => {
5152
ty.extends_type().span() == self.span() || ty.check_type().span() == self.span()
5253
}
53-
AstNodes::TSUnionType(_) => true,
54+
AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true,
5455
_ => false,
5556
}
5657
}
@@ -62,12 +63,19 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSUnionType<'a>> {
6263
}
6364
}
6465

66+
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSIntersectionType<'a>> {
67+
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
68+
matches!(self.parent, AstNodes::TSArrayType(_))
69+
}
70+
}
71+
6572
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSConditionalType<'a>> {
6673
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
6774
match self.parent {
6875
AstNodes::TSConditionalType(ty) => {
6976
ty.extends_type().span() == self.span() || ty.check_type().span() == self.span()
7077
}
78+
AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true,
7179
_ => false,
7280
}
7381
}

tasks/coverage/snapshots/formatter_typescript.snap

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

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

8-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/aliasInstantiationExpressionGenericIntersectionNoCrash1.ts
9-
108
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
119

1210
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/callOfConditionalTypeWithConcreteBranches.ts
@@ -15,14 +13,10 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/castExpressionPa
1513

1614
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/castFunctionExpressionShouldBeParenthesized.ts
1715

18-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/classNonUniqueSymbolMethodHasSymbolIndexer.ts
19-
2016
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/coAndContraVariantInferences3.ts
2117

2218
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/complexNarrowingWithAny.ts
2319

24-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/contextualTypeBasedOnIntersectionWithAnyInTheMix3.ts
25-
2620
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReusesTypeNode4.ts
2721

2822
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPartialReuseComputedProperty.ts
@@ -35,8 +29,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitS
3529

3630
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/exportDefaultParenthesizeES6.ts
3731

38-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/intersectionWithConstructSignaturePrototypeResult.ts
39-
4032
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexport.tsx
4133

4234
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexportMissingAliasTarget.tsx
@@ -45,14 +37,10 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceImpl
4537

4638
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/keyRemappingKeyofResult.ts
4739

48-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/multiSignatureTypeInference.ts
49-
5040
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/narrowingByTypeofInSwitch.ts
5141

5242
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts
5343

54-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/ramdaToolsNoInfinite2.ts
55-
5644
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/readonlyAssignmentInSubclassOfClassExpression.ts
5745

5846
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reducibleIndexedAccessTypes.ts
@@ -61,8 +49,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/referenceSatisfi
6149

6250
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reverseMappedTypeInferenceSameSource1.ts
6351

64-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/signatureCombiningRestParameters1.ts
65-
6652
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationClasses.ts
6753

6854
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/styledComponentsInstantiaionLimitNotReached.ts
@@ -75,14 +61,8 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/tryStatementInte
7561

7662
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typeName1.ts
7763

78-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typePredicateFreshLiteralWidening.ts
79-
8064
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/unionSignaturesWithThisParameter.ts
8165

82-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/mixinAbstractClasses.ts
83-
84-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty9.ts
85-
8666
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticAutoAccessorsWithDecorators.ts
8767

8868
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/controlFlow/controlFlowAssignmentPatternOrder.ts
@@ -129,8 +109,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/pedantic/noUn
129109

130110
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts
131111

132-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/literal/templateLiteralTypes4.ts
133-
134112
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/mapped/mappedTypeModifiers.ts
135113

136114
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/objectTypeLiteral/propertySignatures/propertyNamesOfReservedWords.ts

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ ts compatibility: 350/573 (61.08%)
145145
| typescript/interface2/comments.ts | 💥 | 78.87% |
146146
| typescript/interface2/module.ts | 💥 | 80.00% |
147147
| typescript/interface2/break/break.ts | 💥💥💥 | 68.93% |
148-
| typescript/intersection/intersection-parens.ts | 💥💥 | 54.84% |
148+
| typescript/intersection/intersection-parens.ts | 💥💥 | 55.91% |
149149
| typescript/intersection/type-arguments.ts | 💥💥 | 46.67% |
150150
| typescript/intersection/consistent-with-flow/comment.ts | 💥 | 0.00% |
151151
| typescript/intersection/consistent-with-flow/intersection-parens.ts | 💥 | 60.47% |

0 commit comments

Comments
 (0)