diff --git a/crates/biome_js_formatter/src/ts/expressions/as_expression.rs b/crates/biome_js_formatter/src/ts/expressions/as_expression.rs index 84fd847a0df2..4ca10bd6ba29 100644 --- a/crates/biome_js_formatter/src/ts/expressions/as_expression.rs +++ b/crates/biome_js_formatter/src/ts/expressions/as_expression.rs @@ -7,7 +7,7 @@ use crate::ts::expressions::type_assertion_expression::type_cast_like_needs_pare use biome_formatter::{format_args, write}; use biome_js_syntax::{AnyJsExpression, JsSyntaxKind, JsSyntaxNode, JsSyntaxToken, TsAsExpression}; use biome_js_syntax::{AnyTsType, TsSatisfiesExpression}; -use biome_rowan::{declare_node_union, SyntaxResult}; +use biome_rowan::{declare_node_union, SyntaxNodeOptionExt, SyntaxResult}; #[derive(Debug, Clone, Default)] pub struct FormatTsAsExpression; @@ -62,9 +62,27 @@ impl Format for TsAsOrSatisfiesExpression { let expression = self.expression(); let operation_token = self.operation_token()?; let ty = self.ty()?; - + // edge case: `(type) as T;` + // This check could be implemented in `JsIdentifierExpression::needs_parentheses_with_parent`, + // however this could incurs a perf penality. + let expression_needs_parens = self.syntax().parent().kind() + == Some(JsSyntaxKind::JS_EXPRESSION_STATEMENT) + && expression + .as_ref() + .ok() + .and_then(|expr| expr.as_js_reference_identifier()?.value_token().ok()) + .map_or(false, |name| name.text_trimmed() == "type"); let format_inner = format_with(|f| { - write!(f, [expression.format(), space(), operation_token.format()])?; + write!( + f, + [ + expression_needs_parens.then_some(text("(")), + expression.format(), + expression_needs_parens.then_some(text(")")), + space(), + operation_token.format() + ] + )?; if f.comments().has_leading_own_line_comment(ty.syntax()) { write!(f, [indent(&format_args![hard_line_break(), &ty.format()])]) diff --git a/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts b/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts index 6799787a850b..abdb2ed74da4 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts +++ b/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts @@ -1,6 +1,4 @@ // expression statemnt of "as" expression hardly ever makes sense, but it's still valid. const [type, x] = [0, 0]; -// FIXME -// TODO: parse issue -// (type) as unknown; +(type) as unknown; x as unknown; diff --git a/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts.snap b/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts.snap index 7a7dd34f2b09..4e5b5e0fac06 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts.snap +++ b/crates/biome_js_formatter/tests/specs/prettier/typescript/as/expression-statement.ts.snap @@ -8,9 +8,7 @@ info: typescript/as/expression-statement.ts ```ts // expression statemnt of "as" expression hardly ever makes sense, but it's still valid. const [type, x] = [0, 0]; -// FIXME -// TODO: parse issue -// (type) as unknown; +(type) as unknown; x as unknown; ``` @@ -21,13 +19,11 @@ x as unknown; ```diff --- Prettier +++ Biome -@@ -1,4 +1,6 @@ +@@ -1,4 +1,4 @@ // expression statemnt of "as" expression hardly ever makes sense, but it's still valid. const [type, x] = [0, 0]; -type as unknown; -+// FIXME -+// TODO: parse issue -+// (type) as unknown; ++(type) as unknown; x as unknown; ``` @@ -36,9 +32,7 @@ x as unknown; ```ts // expression statemnt of "as" expression hardly ever makes sense, but it's still valid. const [type, x] = [0, 0]; -// FIXME -// TODO: parse issue -// (type) as unknown; +(type) as unknown; x as unknown; ``` diff --git a/crates/biome_js_formatter/tests/specs/prettier/typescript/satisfies-operators/expression-statement.ts.snap b/crates/biome_js_formatter/tests/specs/prettier/typescript/satisfies-operators/expression-statement.ts.snap index 2ab4cd88eccd..5f6bb218527d 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/typescript/satisfies-operators/expression-statement.ts.snap +++ b/crates/biome_js_formatter/tests/specs/prettier/typescript/satisfies-operators/expression-statement.ts.snap @@ -53,7 +53,14 @@ satisfies satisfies satisfies satisfies satisfies; ```diff --- Prettier +++ Biome -@@ -15,12 +15,12 @@ +@@ -9,18 +9,18 @@ + return 2; + default: + // exhaustiveness check idiom +- type satisfies never; ++ (type) satisfies never; + throw new Error("unreachable"); + } }; function needParens() { @@ -86,7 +93,7 @@ const _ = () => { return 2; default: // exhaustiveness check idiom - type satisfies never; + (type) satisfies never; throw new Error("unreachable"); } };