From 9998c8a64f1c903c96966c65577cba98a89bc0d2 Mon Sep 17 00:00:00 2001 From: Noel Kim Date: Tue, 16 Sep 2025 19:03:56 +0900 Subject: [PATCH] fix(formatter): Add parentheses inside `TSIntersectionType` --- .../oxc_formatter/src/parentheses/ts_type.rs | 12 ++++++++-- .../snapshots/formatter_typescript.snap | 24 +------------------ .../snapshots/prettier.ts.snap.md | 2 +- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/crates/oxc_formatter/src/parentheses/ts_type.rs b/crates/oxc_formatter/src/parentheses/ts_type.rs index 20038ed83c7db..1431e43f8487c 100644 --- a/crates/oxc_formatter/src/parentheses/ts_type.rs +++ b/crates/oxc_formatter/src/parentheses/ts_type.rs @@ -16,6 +16,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSType<'a>> { AstNodes::TSInferType(it) => it.needs_parentheses(f), AstNodes::TSConstructorType(it) => it.needs_parentheses(f), AstNodes::TSUnionType(it) => it.needs_parentheses(f), + AstNodes::TSIntersectionType(it) => it.needs_parentheses(f), AstNodes::TSConditionalType(it) => it.needs_parentheses(f), _ => { // TODO: incomplete @@ -32,7 +33,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSFunctionType<'a>> { AstNodes::TSConditionalType(ty) => { ty.extends_type().span() == self.span() || ty.check_type().span() == self.span() } - AstNodes::TSUnionType(_) => true, + AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true, _ => false, } } @@ -50,7 +51,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSConstructorType<'a>> { AstNodes::TSConditionalType(ty) => { ty.extends_type().span() == self.span() || ty.check_type().span() == self.span() } - AstNodes::TSUnionType(_) => true, + AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true, _ => false, } } @@ -62,12 +63,19 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSUnionType<'a>> { } } +impl<'a> NeedsParentheses<'a> for AstNode<'a, TSIntersectionType<'a>> { + fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool { + matches!(self.parent, AstNodes::TSArrayType(_)) + } +} + impl<'a> NeedsParentheses<'a> for AstNode<'a, TSConditionalType<'a>> { fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool { match self.parent { AstNodes::TSConditionalType(ty) => { ty.extends_type().span() == self.span() || ty.check_type().span() == self.span() } + AstNodes::TSUnionType(_) | AstNodes::TSIntersectionType(_) => true, _ => false, } } diff --git a/tasks/coverage/snapshots/formatter_typescript.snap b/tasks/coverage/snapshots/formatter_typescript.snap index 8d363e43c47b4..2dde527ac4768 100644 --- a/tasks/coverage/snapshots/formatter_typescript.snap +++ b/tasks/coverage/snapshots/formatter_typescript.snap @@ -2,11 +2,9 @@ commit: 261630d6 formatter_typescript Summary: AST Parsed : 8816/8816 (100.00%) -Positive Passed: 8746/8816 (99.21%) +Positive Passed: 8757/8816 (99.33%) Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/APISample_jsdoc.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/aliasInstantiationExpressionGenericIntersectionNoCrash1.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts 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 Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/castFunctionExpressionShouldBeParenthesized.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/classNonUniqueSymbolMethodHasSymbolIndexer.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/coAndContraVariantInferences3.ts Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/complexNarrowingWithAny.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/contextualTypeBasedOnIntersectionWithAnyInTheMix3.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReusesTypeNode4.ts 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 Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/exportDefaultParenthesizeES6.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/intersectionWithConstructSignaturePrototypeResult.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexport.tsx 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 Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/keyRemappingKeyofResult.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/multiSignatureTypeInference.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/narrowingByTypeofInSwitch.ts Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/ramdaToolsNoInfinite2.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/readonlyAssignmentInSubclassOfClassExpression.ts 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 Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reverseMappedTypeInferenceSameSource1.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/signatureCombiningRestParameters1.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationClasses.ts 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 Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typeName1.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typePredicateFreshLiteralWidening.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/unionSignaturesWithThisParameter.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/mixinAbstractClasses.ts - -Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty9.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticAutoAccessorsWithDecorators.ts 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 Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts -Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/literal/templateLiteralTypes4.ts - Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/mapped/mappedTypeModifiers.ts Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/types/objectTypeLiteral/propertySignatures/propertyNamesOfReservedWords.ts diff --git a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md index e8e61fa7dcb7b..b54a435bf3202 100644 --- a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md +++ b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md @@ -145,7 +145,7 @@ ts compatibility: 350/573 (61.08%) | typescript/interface2/comments.ts | 💥 | 78.87% | | typescript/interface2/module.ts | 💥 | 80.00% | | typescript/interface2/break/break.ts | 💥💥💥 | 68.93% | -| typescript/intersection/intersection-parens.ts | 💥💥 | 54.84% | +| typescript/intersection/intersection-parens.ts | 💥💥 | 55.91% | | typescript/intersection/type-arguments.ts | 💥💥 | 46.67% | | typescript/intersection/consistent-with-flow/comment.ts | 💥 | 0.00% | | typescript/intersection/consistent-with-flow/intersection-parens.ts | 💥 | 60.47% |