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 46ab996 commit d30c173
Show file tree
Hide file tree
Showing 2 changed files with 14 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

0 comments on commit d30c173

Please sign in to comment.