From fdd1e1ead7053d17bf51581bad0af2b6d0581a0c Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Mon, 12 Jan 2026 02:54:37 +0000 Subject: [PATCH] fix(formatter): don't wrap parenthesis for type assertion when it's an declaration of export default (#17878) close: #17872 --- .../src/parentheses/expression.rs | 3 +-- .../fixtures/ts/parenthesis/type-assertion.ts | 1 + .../ts/parenthesis/type-assertion.ts.snap | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts create mode 100644 crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts.snap diff --git a/crates/oxc_formatter/src/parentheses/expression.rs b/crates/oxc_formatter/src/parentheses/expression.rs index 9afb6c45751bc..881fbdb50d964 100644 --- a/crates/oxc_formatter/src/parentheses/expression.rs +++ b/crates/oxc_formatter/src/parentheses/expression.rs @@ -738,8 +738,7 @@ impl NeedsParentheses<'_> for AstNode<'_, TSTypeAssertion<'_>> { fn type_cast_like_needs_parens(span: Span, parent: &AstNodes<'_>) -> bool { #[expect(clippy::match_same_arms)] // for better readability match parent { - AstNodes::ExportDefaultDeclaration(_) - | AstNodes::TSTypeAssertion(_) + AstNodes::TSTypeAssertion(_) | AstNodes::UnaryExpression(_) | AstNodes::AwaitExpression(_) | AstNodes::TSNonNullExpression(_) diff --git a/crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts b/crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts new file mode 100644 index 0000000000000..a5bee976165a0 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts @@ -0,0 +1 @@ +export default []; diff --git a/crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts.snap new file mode 100644 index 0000000000000..8119f71f80d43 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/parenthesis/type-assertion.ts.snap @@ -0,0 +1,18 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +export default []; + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +export default []; + +------------------- +{ printWidth: 100 } +------------------- +export default []; + +===================== End =====================