Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_formatter): add parens to match Prettier #3552

Merged
merged 1 commit into from
Jul 31, 2024
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
19 changes: 6 additions & 13 deletions crates/biome_js_formatter/report.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## Overall Metrics

**Average compatibility**: 96.79
**Average compatibility**: 96.88

<details>
<summary>Definition</summary>

$$average = \frac\{\sum_{file}^\{files}compatibility_\{file}}\{files}$$
</details>

**Compatible lines**: 98.06
**Compatible lines**: 98.07

<details>
<summary>Definition</summary>
Expand Down Expand Up @@ -6915,11 +6915,9 @@
+) => token is infer U extends TSESTree.Token
? U
: TSESTree.Token;
-type Test<T> = T extends ((
+type Test<T> = T extends (
type Test<T> = 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> = T extends (new (
Expand All @@ -6934,7 +6932,7 @@

```

**Prettier Similarity**: 56.00%
**Prettier Similarity**: 64.00%


### typescript/conformance/ambient/ambientDeclarations.ts
Expand Down Expand Up @@ -9675,13 +9673,8 @@


### typescript/type-arguments-bit-shift-left-like/1.ts
```diff
-f << (<T>x);
+f << <T>x;

```

**Prettier Similarity**: 0.00%
**Prettier Similarity**: 100.00%


### typescript/type-arguments-bit-shift-left-like/2.ts
Expand Down
22 changes: 5 additions & 17 deletions crates/biome_js_formatter/report_incompatible.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## Overall Metrics

**Average compatibility**: 96.79
**Average compatibility**: 96.88

<details>
<summary>Definition</summary>

$$average = \frac\{\sum_{file}^\{files}compatibility_\{file}}\{files}$$
</details>

**Compatible lines**: 98.06
**Compatible lines**: 98.07

<details>
<summary>Definition</summary>
Expand Down Expand Up @@ -2840,11 +2840,9 @@
+) => token is infer U extends TSESTree.Token
? U
: TSESTree.Token;
-type Test<T> = T extends ((
+type Test<T> = T extends (
type Test<T> = 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> = T extends (new (
Expand All @@ -2859,7 +2857,7 @@

```

**Prettier Similarity**: 56.00%
**Prettier Similarity**: 64.00%


### typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations2.ts
Expand Down Expand Up @@ -3939,16 +3937,6 @@
**Prettier Similarity**: 41.67%


### typescript/type-arguments-bit-shift-left-like/1.ts
```diff
-f << (<T>x);
+f << <T>x;

```

**Prettier Similarity**: 0.00%


### typescript/typeparams/const.ts
```diff
function a<const T>() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Test<T> = T extends (new (
```diff
--- Prettier
+++ Biome
@@ -1,22 +1,25 @@
@@ -1,13 +1,15 @@
// #13275
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
-type Foo<T> = T extends (new (...a: any[]) => infer R extends string)
Expand All @@ -52,11 +52,8 @@ type Test<T> = T extends (new (
+) => token is infer U extends TSESTree.Token
? U
: TSESTree.Token;
-type Test<T> = T extends ((
+type Test<T> = T extends (
token: TSESTree.Token,
-) => asserts token is infer U extends TSESTree.Token)
+) => asserts token is infer U extends TSESTree.Token
type Test<T> = T extends ((
@@ -16,7 +18,8 @@
? U
: TSESTree.Token;
type Test<T> = T extends (new (
Expand Down Expand Up @@ -87,9 +84,9 @@ type Test<T> = T extends (
) => token is infer U extends TSESTree.Token
? U
: TSESTree.Token;
type Test<T> = T extends (
type Test<T> = 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> = T extends (new (
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions crates/biome_js_syntax/src/parentheses/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
14 changes: 8 additions & 6 deletions crates/biome_js_syntax/src/parentheses/tstype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Loading