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): keep parens around infer in unions/intersections #3451

Merged
merged 1 commit into from
Jul 16, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Formatter

#### Bug fixes

- Keep the parentheses around `infer` declarations in type unions and type intersections ([#3419](https://github.com/biomejs/biome/issues/3419)). Contributed by @Conaclos

### JavaScript APIs

### Linter
Expand Down
10 changes: 10 additions & 0 deletions crates/biome_js_formatter/src/ts/types/infer_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ impl NeedsParentheses for TsInferType {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
if parent.kind() == JsSyntaxKind::TS_REST_TUPLE_TYPE_ELEMENT {
false
} else if matches!(
parent.kind(),
JsSyntaxKind::TS_INTERSECTION_TYPE_ELEMENT_LIST
| JsSyntaxKind::TS_UNION_TYPE_VARIANT_LIST
) {
true
} else {
operator_type_or_higher_needs_parens(self.syntax(), parent)
}
Expand Down Expand Up @@ -64,6 +70,10 @@ mod tests {
"type A = T extends [(infer string)?] ? string : never",
TsInferType
);
assert_needs_parentheses!(
"type A = T extends [(infer string) | undefined] ? string : never",
TsInferType
);

assert_needs_parentheses!(
"type A = T extends (infer string)[a] ? string : never",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Type<T> = [T] extends [(infer S extends string) & {}] ? S : T;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: ts/type/injfer_in_intersection.ts
---
# Input

```ts
type Type<T> = [T] extends [(infer S extends string) & {}] ? S : T;
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing commas: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
-----

```ts
type Type<T> = [T] extends [(infer S extends string) & {}] ? S : T;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Type<T> = [T] extends [(infer S extends string) | undefined] ? S : T;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: ts/type/injfer_in_union.ts
---
# Input

```ts
type Type<T> = [T] extends [(infer S extends string) | undefined] ? S : T;
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing commas: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
-----

```ts
type Type<T> = [T] extends [(infer S extends string) | undefined] ? S : T;
```