Skip to content

Commit 950eeb5

Browse files
committed
1 parent 1509cbd commit 950eeb5

File tree

4 files changed

+29
-56
lines changed

4 files changed

+29
-56
lines changed

crates/oxc_formatter/src/parentheses/expression.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,13 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, JSXEmptyExpression> {
626626

627627
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSAsExpression<'a>> {
628628
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
629-
ts_as_or_satisfies_needs_parens(self.parent)
629+
ts_as_or_satisfies_needs_parens(self.span(), &self.expression, self.parent)
630630
}
631631
}
632632

633633
impl<'a> NeedsParentheses<'a> for AstNode<'a, TSSatisfiesExpression<'a>> {
634634
fn needs_parentheses(&self, f: &Formatter<'_, 'a>) -> bool {
635-
ts_as_or_satisfies_needs_parens(self.parent)
635+
ts_as_or_satisfies_needs_parens(self.span(), &self.expression, self.parent)
636636
}
637637
}
638638

@@ -649,7 +649,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, TSTypeAssertion<'a>> {
649649
}
650650

651651
fn type_cast_like_needs_parens(span: Span, parent: &AstNodes<'_>) -> bool {
652-
#[expect(clippy::match_same_arms)]
652+
#[expect(clippy::match_same_arms)] // for better readability
653653
match parent {
654654
AstNodes::ExportDefaultDeclaration(_)
655655
| AstNodes::TSTypeAssertion(_)
@@ -942,16 +942,23 @@ fn await_or_yield_needs_parens(span: Span, node: &AstNodes<'_>) -> bool {
942942
}
943943
}
944944

945-
fn ts_as_or_satisfies_needs_parens(parent: &AstNodes<'_>) -> bool {
946-
matches!(
947-
parent,
948-
AstNodes::ComputedMemberExpression(_)
949-
| AstNodes::StaticMemberExpression(_)
950-
| AstNodes::PrivateFieldExpression(_)
951-
| AstNodes::AssignmentExpression(_)
952-
| AstNodes::AssignmentTargetWithDefault(_)
953-
| AstNodes::UpdateExpression(_)
954-
)
945+
fn ts_as_or_satisfies_needs_parens(
946+
span: Span,
947+
inner: &Expression<'_>,
948+
parent: &AstNodes<'_>,
949+
) -> bool {
950+
match parent {
951+
AstNodes::ConditionalExpression(_)
952+
// Binary-like
953+
| AstNodes::LogicalExpression(_)
954+
| AstNodes::BinaryExpression(_) => true,
955+
// `export default (function foo() {} as bar)` and `export default (class {} as bar)`
956+
AstNodes::ExportDefaultDeclaration(_) =>
957+
matches!(inner, Expression::FunctionExpression(_) | Expression::ClassExpression(_)),
958+
_ => {
959+
type_cast_like_needs_parens(span, parent)
960+
}
961+
}
955962
}
956963

957964
fn is_class_extends(span: Span, parent: &AstNodes<'_>) -> bool {

tasks/coverage/snapshots/formatter_misc.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
formatter_misc Summary:
22
AST Parsed : 49/49 (100.00%)
3-
Positive Passed: 45/49 (91.84%)
3+
Positive Passed: 46/49 (93.88%)
44
Expect to Parse: tasks/coverage/misc/pass/oxc-11487.cjs
55
Cannot use `await` as an identifier in an async context
6-
Mismatch: tasks/coverage/misc/pass/oxc-12612.ts
7-
86
Mismatch: tasks/coverage/misc/pass/oxc-2592.ts
97

108
Expect to Parse: tasks/coverage/misc/pass/oxc-4449.ts

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,31 @@ commit: 261630d6
22

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8775/8816 (99.53%)
5+
Positive Passed: 8785/8816 (99.65%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
99
`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules
1010
Mismatch: tasks/coverage/typescript/tests/cases/compiler/callOfConditionalTypeWithConcreteBranches.ts
1111

12-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/castFunctionExpressionShouldBeParenthesized.ts
13-
Expected a semicolon or an implicit semicolon after a statement, but found none
1412
Mismatch: tasks/coverage/typescript/tests/cases/compiler/coAndContraVariantInferences3.ts
1513

1614
Mismatch: tasks/coverage/typescript/tests/cases/compiler/complexNarrowingWithAny.ts
1715

1816
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitCastReusesTypeNode4.ts
1917

20-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/declarationEmitPromise.ts
21-
Expected a semicolon or an implicit semicolon after a statement, but found none
2218
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitRecursiveConditionalAliasPreserved.ts
2319

2420
Mismatch: tasks/coverage/typescript/tests/cases/compiler/declarationEmitShadowingInferNotRenamed.ts
2521

26-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/exportDefaultParenthesizeES6.ts
27-
Expected a semicolon or an implicit semicolon after a statement, but found none
2822
Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexport.tsx
2923

3024
Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceGlobalReexportMissingAliasTarget.tsx
3125

3226
Mismatch: tasks/coverage/typescript/tests/cases/compiler/jsxNamespaceImplicitImportJSXNamespace.tsx
3327

34-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/narrowingByTypeofInSwitch.ts
35-
Expected a semicolon or an implicit semicolon after a statement, but found none
3628
Mismatch: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts
3729

38-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/readonlyAssignmentInSubclassOfClassExpression.ts
39-
Expected `{` but found `as`
4030
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/reverseMappedTypeInferenceSameSource1.ts
4131
An implementation cannot be declared in ambient contexts.
4232
Mismatch: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationClasses.ts
@@ -45,8 +35,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/compiler/styledComponentsInstant
4535

4636
Mismatch: tasks/coverage/typescript/tests/cases/compiler/substitutionTypeForIndexedAccessType2.ts
4737

48-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/superAccessCastedCall.ts
49-
Expected a semicolon or an implicit semicolon after a statement, but found none
5038
Mismatch: tasks/coverage/typescript/tests/cases/compiler/tryStatementInternalComments.ts
5139

5240
Mismatch: tasks/coverage/typescript/tests/cases/compiler/unionSignaturesWithThisParameter.ts
@@ -57,31 +45,23 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/prope
5745
Classes may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototype
5846
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/controlFlow/controlFlowAssignmentPatternOrder.ts
5947
An implementation cannot be declared in ambient contexts.
60-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/classSuper/esDecorators-classDeclaration-classSuper.2.ts
61-
Expected `{` but found `as`
62-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classExpression/classSuper/esDecorators-classExpression-classSuper.2.ts
63-
Expected `{` but found `as`
6448
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classExpression/esDecorators-classExpression-missingEmitHelpers-classDecorator.3.ts
6549
Decorators are not valid here.Unexpected token
6650
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classExpression/namedEvaluation/esDecorators-classExpression-namedEvaluation.8.ts
6751
Decorators are not valid here.Unexpected token
68-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/asOperator/asOpEmitParens.ts
69-
Expected a semicolon or an implicit semicolon after a statement, but found none
7052
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts
7153
Unexpected token
72-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/optionalChaining/optionalChainingInTypeAssertions.ts
73-
Expected a semicolon or an implicit semicolon after a statement, but found none
7454
Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/typeGuards/typeGuardsInRightOperandOfAndAndOperator.ts
7555

7656
Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/typeGuards/typeGuardsInRightOperandOfOrOrOperator.ts
7757

78-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/generators/yieldStatementNoAsiAfterTransform.ts
79-
Expected a semicolon or an implicit semicolon after a statement, but found none
58+
Mismatch: tasks/coverage/typescript/tests/cases/conformance/generators/yieldStatementNoAsiAfterTransform.ts
59+
8060
Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithPrivates2.ts
8161

8262
Mismatch: tasks/coverage/typescript/tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts
8363

84-
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts
85-
Expected `,` but found `(`
64+
Mismatch: tasks/coverage/typescript/tests/cases/conformance/statements/returnStatements/returnStatementNoAsiAfterTransform.ts
65+
8666
Mismatch: tasks/coverage/typescript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts
8767

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 389/573 (67.89%)
1+
ts compatibility: 401/573 (69.98%)
22

33
# Failed
44

@@ -16,18 +16,13 @@ ts compatibility: 389/573 (67.89%)
1616
| typescript/argument-expansion/arrow-with-return-type.ts | 💥 | 89.47% |
1717
| typescript/arrow/16067.ts | 💥💥 | 95.92% |
1818
| typescript/arrow/comments.ts | 💥✨ | 44.44% |
19-
| typescript/as/as.ts | 💥 | 74.02% |
20-
| typescript/as/assignment.ts | 💥 | 86.67% |
19+
| typescript/as/as.ts | 💥 | 85.04% |
2120
| typescript/as/assignment2.ts | 💥 | 94.12% |
22-
| typescript/as/export_default_as.ts | 💥 | 0.00% |
2321
| typescript/as/expression-statement.ts | 💥 | 75.00% |
24-
| typescript/as/long-identifiers.ts | 💥 | 92.86% |
2522
| typescript/as/nested-await-and-as.ts | 💥 | 42.86% |
26-
| typescript/as/ternary.ts | 💥 | 82.00% |
2723
| typescript/assignment/issue-10846.ts | 💥 | 63.16% |
2824
| typescript/assignment/issue-10848.tsx | 💥 | 52.12% |
2925
| typescript/assignment/issue-10850.ts | 💥 | 50.00% |
30-
| typescript/cast/as-const.ts | 💥 | 60.00% |
3126
| typescript/cast/generic-cast.ts | 💥 | 39.60% |
3227
| typescript/cast/tuple-and-record.ts | 💥 | 0.00% |
3328
| typescript/chain-expression/call-expression.ts | 💥 | 68.75% |
@@ -102,7 +97,6 @@ ts compatibility: 389/573 (67.89%)
10297
| typescript/enum/computed-members.ts | 💥 | 0.00% |
10398
| typescript/export/comment.ts | 💥 | 50.00% |
10499
| typescript/export/export.ts | 💥 | 85.71% |
105-
| typescript/export-default/function_as.ts | 💥 | 0.00% |
106100
| typescript/function-type/consistent.ts | 💥 | 70.83% |
107101
| typescript/function-type/type-annotation.ts | 💥 | 0.00% |
108102
| typescript/generic/arrow-return-type.ts | 💥 | 80.77% |
@@ -134,7 +128,6 @@ ts compatibility: 389/573 (67.89%)
134128
| typescript/module/namespace_function.ts | 💥 | 66.67% |
135129
| typescript/multiparser-css/issue-6259.ts | 💥 | 57.14% |
136130
| typescript/new/new-signature.ts | 💥 | 93.85% |
137-
| typescript/no-semi/non-null.ts | 💥💥 | 66.67% |
138131
| typescript/non-null/optional-chain.ts | 💥 | 72.22% |
139132
| typescript/non-null/parens.ts | 💥 | 96.00% |
140133
| typescript/nosemi/index-signature.ts | 💥 | 75.00% |
@@ -148,15 +141,10 @@ ts compatibility: 389/573 (67.89%)
148141
| typescript/prettier-ignore/prettier-ignore-parenthesized-type.ts | 💥 | 0.00% |
149142
| typescript/rest-type/complex.ts | 💥 | 0.00% |
150143
| typescript/rest-type/infer-type.ts | 💥 | 80.00% |
151-
| typescript/satisfies-operators/assignment.ts | 💥💥 | 72.73% |
152-
| typescript/satisfies-operators/export-default-as.ts | 💥💥 | 0.00% |
144+
| typescript/satisfies-operators/assignment.ts | 💥💥 | 90.91% |
153145
| typescript/satisfies-operators/expression-statement.ts | 💥💥 | 78.38% |
154-
| typescript/satisfies-operators/gt-lt.ts | 💥💥 | 0.00% |
155146
| typescript/satisfies-operators/lhs.ts | 💥✨ | 35.00% |
156147
| typescript/satisfies-operators/nested-await-and-satisfies.ts | 💥💥 | 42.86% |
157-
| typescript/satisfies-operators/non-null.ts | 💥💥 | 66.67% |
158-
| typescript/satisfies-operators/satisfies.ts | 💥💥 | 81.82% |
159-
| typescript/satisfies-operators/ternary.ts | 💥💥 | 82.00% |
160148
| typescript/template-literal-types/template-literal-types.ts | 💥 | 80.00% |
161149
| typescript/test-declarations/test_declarations.ts | 💥💥 | 66.67% |
162150
| typescript/trailing-comma/arrow-functions.tsx | 💥💥💥 | 25.00% |

0 commit comments

Comments
 (0)