Skip to content

Commit

Permalink
fix(js_formatter): keep parens around infer in unions/intersections (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jul 16, 2024
1 parent 8c2e254 commit 0119290
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
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;
```

0 comments on commit 0119290

Please sign in to comment.