From 673fdbf98d42339eca405d1cfd8a5e5a0b7c6f69 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Wed, 31 Jul 2024 12:07:15 +0200 Subject: [PATCH] fix(js_formatter): add parens to match Prettier --- crates/biome_js_formatter/report.md | 19 +++++-------- .../biome_js_formatter/report_incompatible.md | 22 ++++----------- .../conditional-types/parentheses.ts.snap | 13 ++++----- .../1.ts.snap | 27 ------------------- .../src/parentheses/expression.rs | 4 +++ .../biome_js_syntax/src/parentheses/tstype.rs | 14 +++++----- 6 files changed, 28 insertions(+), 71 deletions(-) delete mode 100644 crates/biome_js_formatter/tests/specs/prettier/typescript/type-arguments-bit-shift-left-like/1.ts.snap diff --git a/crates/biome_js_formatter/report.md b/crates/biome_js_formatter/report.md index fc0bddfaa706..78822768b42a 100644 --- a/crates/biome_js_formatter/report.md +++ b/crates/biome_js_formatter/report.md @@ -1,6 +1,6 @@ ## Overall Metrics -**Average compatibility**: 96.79 +**Average compatibility**: 96.88
Definition @@ -8,7 +8,7 @@ $$average = \frac\{\sum_{file}^\{files}compatibility_\{file}}\{files}$$
-**Compatible lines**: 98.06 +**Compatible lines**: 98.07
Definition @@ -6915,11 +6915,9 @@ +) => token is infer U extends TSESTree.Token ? U : TSESTree.Token; --type Test = T extends (( -+type Test = T extends ( + type Test = T extends (( token: TSESTree.Token, --) => asserts token is infer U extends TSESTree.Token) -+) => asserts token is infer U extends TSESTree.Token + ) => asserts token is infer U extends TSESTree.Token) ? U : TSESTree.Token; type Test = T extends (new ( @@ -6934,7 +6932,7 @@ ``` -**Prettier Similarity**: 56.00% +**Prettier Similarity**: 64.00% ### typescript/conformance/ambient/ambientDeclarations.ts @@ -9675,13 +9673,8 @@ ### typescript/type-arguments-bit-shift-left-like/1.ts -```diff --f << (x); -+f << x; -``` - -**Prettier Similarity**: 0.00% +**Prettier Similarity**: 100.00% ### typescript/type-arguments-bit-shift-left-like/2.ts diff --git a/crates/biome_js_formatter/report_incompatible.md b/crates/biome_js_formatter/report_incompatible.md index 89d1578e5d35..6aebc1817089 100644 --- a/crates/biome_js_formatter/report_incompatible.md +++ b/crates/biome_js_formatter/report_incompatible.md @@ -1,6 +1,6 @@ ## Overall Metrics -**Average compatibility**: 96.79 +**Average compatibility**: 96.88
Definition @@ -8,7 +8,7 @@ $$average = \frac\{\sum_{file}^\{files}compatibility_\{file}}\{files}$$
-**Compatible lines**: 98.06 +**Compatible lines**: 98.07
Definition @@ -2840,11 +2840,9 @@ +) => token is infer U extends TSESTree.Token ? U : TSESTree.Token; --type Test = T extends (( -+type Test = T extends ( + type Test = T extends (( token: TSESTree.Token, --) => asserts token is infer U extends TSESTree.Token) -+) => asserts token is infer U extends TSESTree.Token + ) => asserts token is infer U extends TSESTree.Token) ? U : TSESTree.Token; type Test = T extends (new ( @@ -2859,7 +2857,7 @@ ``` -**Prettier Similarity**: 56.00% +**Prettier Similarity**: 64.00% ### typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations2.ts @@ -3939,16 +3937,6 @@ **Prettier Similarity**: 41.67% -### typescript/type-arguments-bit-shift-left-like/1.ts -```diff --f << (x); -+f << x; - -``` - -**Prettier Similarity**: 0.00% - - ### typescript/typeparams/const.ts ```diff function a() {} diff --git a/crates/biome_js_formatter/tests/specs/prettier/typescript/conditional-types/parentheses.ts.snap b/crates/biome_js_formatter/tests/specs/prettier/typescript/conditional-types/parentheses.ts.snap index d7d81ae9951c..8c6c9c8263b0 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/typescript/conditional-types/parentheses.ts.snap +++ b/crates/biome_js_formatter/tests/specs/prettier/typescript/conditional-types/parentheses.ts.snap @@ -34,7 +34,7 @@ type Test = T extends (new ( ```diff --- Prettier +++ Biome -@@ -1,22 +1,25 @@ +@@ -1,13 +1,15 @@ // #13275 type Foo = T extends ((...a: any[]) => infer R extends string) ? R : never; -type Foo = T extends (new (...a: any[]) => infer R extends string) @@ -52,11 +52,8 @@ type Test = T extends (new ( +) => token is infer U extends TSESTree.Token ? U : TSESTree.Token; --type Test = T extends (( -+type Test = T extends ( - token: TSESTree.Token, --) => asserts token is infer U extends TSESTree.Token) -+) => asserts token is infer U extends TSESTree.Token + type Test = T extends (( +@@ -16,7 +18,8 @@ ? U : TSESTree.Token; type Test = T extends (new ( @@ -87,9 +84,9 @@ type Test = T extends ( ) => token is infer U extends TSESTree.Token ? U : TSESTree.Token; -type Test = T extends ( +type Test = T extends (( token: TSESTree.Token, -) => asserts token is infer U extends TSESTree.Token +) => asserts token is infer U extends TSESTree.Token) ? U : TSESTree.Token; type Test = T extends (new ( diff --git a/crates/biome_js_formatter/tests/specs/prettier/typescript/type-arguments-bit-shift-left-like/1.ts.snap b/crates/biome_js_formatter/tests/specs/prettier/typescript/type-arguments-bit-shift-left-like/1.ts.snap deleted file mode 100644 index c6da529d84a8..000000000000 --- a/crates/biome_js_formatter/tests/specs/prettier/typescript/type-arguments-bit-shift-left-like/1.ts.snap +++ /dev/null @@ -1,27 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -info: typescript/type-arguments-bit-shift-left-like/1.ts ---- -# Input - -```ts -f<<(x) - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Biome -@@ -1 +1 @@ --f << (x); -+f << x; -``` - -# Output - -```ts -f << x; -``` diff --git a/crates/biome_js_syntax/src/parentheses/expression.rs b/crates/biome_js_syntax/src/parentheses/expression.rs index 26ef8b90387c..e11a23a32751 100644 --- a/crates/biome_js_syntax/src/parentheses/expression.rs +++ b/crates/biome_js_syntax/src/parentheses/expression.rs @@ -732,6 +732,10 @@ impl NeedsParentheses for TsTypeAssertionExpression { match parent.kind() { JsSyntaxKind::TS_AS_EXPRESSION => true, JsSyntaxKind::TS_SATISFIES_EXPRESSION => true, + JsSyntaxKind::JS_BINARY_EXPRESSION => { + JsBinaryExpression::unwrap_cast(parent).operator() + == Ok(JsBinaryOperator::LeftShift) + } _ => type_cast_like_needs_parens(self.syntax(), parent), } } diff --git a/crates/biome_js_syntax/src/parentheses/tstype.rs b/crates/biome_js_syntax/src/parentheses/tstype.rs index 9f4efaccb88a..2238278777e3 100644 --- a/crates/biome_js_syntax/src/parentheses/tstype.rs +++ b/crates/biome_js_syntax/src/parentheses/tstype.rs @@ -160,12 +160,14 @@ fn function_like_type_needs_parentheses(node: &JsSyntaxNode) -> bool { if is_not_extends_type { return false; } - let ret_type = AnyTsFunctionType::unwrap_cast(node.clone()).return_type(); - // Tests if `node` includes inferred return types with extends constraints - if let Ok(AnyTsReturnType::AnyTsType(AnyTsType::TsInferType(infer_type))) = ret_type { - infer_type.constraint().is_some() - } else { - false + match AnyTsFunctionType::unwrap_cast(node.clone()).return_type() { + Ok(AnyTsReturnType::AnyTsType(AnyTsType::TsInferType(infer_type))) => { + infer_type.constraint().is_some() + } + Ok(AnyTsReturnType::TsAssertsReturnType(asserts_type)) => { + asserts_type.predicate().is_some() + } + _ => false, } } JsSyntaxKind::TS_UNION_TYPE_VARIANT_LIST => {