Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions crates/oxc_formatter/src/parentheses/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, IdentifierReference<'a>> {
}
name => {
// <https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/language-js/needs-parens.js#L123-L133>
matches!(
self.parent,
AstNodes::TSSatisfiesExpression(_) | AstNodes::TSAsExpression(_)
) && matches!(
if !matches!(
name,
"await"
| "interface"
Expand All @@ -112,6 +109,29 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, IdentifierReference<'a>> {
| "component"
| "hook"
| "type"
) {
return false;
}

let mut parent = self.parent;
while matches!(
parent,
AstNodes::TSSatisfiesExpression(_) | AstNodes::TSAsExpression(_)
) {
parent = parent.parent();
}

// Early return if the parent isn't a `TSSatisfiesExpression` or `TSAsExpression`
if core::ptr::eq(self.parent, parent) {
return false;
}

matches!(
parent, AstNodes::ExpressionStatement(stmt) if
!matches!(
stmt.parent.parent(), AstNodes::ArrowFunctionExpression(arrow)
if arrow.expression()
)
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/assignments/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(type) as unknown;
(type) satisfies unknown;

() => (type) as unknown;
() => (type) satisfies unknown;

((type) as any)['t'];
((type) satisfies any)['t'];
24 changes: 24 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/assignments/type.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
(type) as unknown;
(type) satisfies unknown;

() => (type) as unknown;
() => (type) satisfies unknown;

((type) as any)['t'];
((type) satisfies any)['t'];

==================== Output ====================
(type) as unknown;
(type) satisfies unknown;

() => type as unknown;
() => type satisfies unknown;

(type as any)["t"];
(type satisfies any)["t"];

===================== End =====================
Loading