Skip to content

Commit e36fd8a

Browse files
henrybrewer00-dotcomhenrybrewer93-designematipico
authored
fix(formatter): keep parens around await/yield in instantiation expressions (#10758)
Co-authored-by: Henry <henrybrewer93@icloud.com> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 5a2e65b commit e36fd8a

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#10697](https://github.com/biomejs/biome/issues/10697): The formatter no longer removes the parentheses around an `await` or `yield` expression used as the target of a TypeScript instantiation expression. For example, `(await makeFactory)<Value>` is no longer reformatted to `await makeFactory<Value>`, which would change the meaning of the code.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const a = makeFactory<Value>;
2+
const b = obj.makeFactory<Value>;
3+
4+
// Parentheses around await/yield must be kept, otherwise the type
5+
// arguments would bind to the inner expression and change the meaning.
6+
async function withAwait() {
7+
const c = (await makeFactory)<Value>;
8+
}
9+
10+
function* withYield() {
11+
const d = (yield* makeFactory)<Value>;
12+
const e = (yield makeFactory)<Value>;
13+
}
14+
15+
// Parentheses around other lower-precedence expressions must be kept too.
16+
const f = (cond ? a : b)<Value>;
17+
const g = (x++)<Value>;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
info: ts/expression/instantiation_expression.ts
4+
---
5+
6+
# Input
7+
8+
```ts
9+
const a = makeFactory<Value>;
10+
const b = obj.makeFactory<Value>;
11+
12+
// Parentheses around await/yield must be kept, otherwise the type
13+
// arguments would bind to the inner expression and change the meaning.
14+
async function withAwait() {
15+
const c = (await makeFactory)<Value>;
16+
}
17+
18+
function* withYield() {
19+
const d = (yield* makeFactory)<Value>;
20+
const e = (yield makeFactory)<Value>;
21+
}
22+
23+
// Parentheses around other lower-precedence expressions must be kept too.
24+
const f = (cond ? a : b)<Value>;
25+
const g = (x++)<Value>;
26+
27+
```
28+
29+
30+
# Formatted
31+
32+
```ts
33+
const a = makeFactory<Value>;
34+
const b = obj.makeFactory<Value>;
35+
36+
// Parentheses around await/yield must be kept, otherwise the type
37+
// arguments would bind to the inner expression and change the meaning.
38+
async function withAwait() {
39+
const c = (await makeFactory)<Value>;
40+
}
41+
42+
function* withYield() {
43+
const d = (yield* makeFactory)<Value>;
44+
const e = (yield makeFactory)<Value>;
45+
}
46+
47+
// Parentheses around other lower-precedence expressions must be kept too.
48+
const f = (cond ? a : b)<Value>;
49+
const g = (x++)<Value>;
50+
51+
```

crates/biome_js_syntax/src/parentheses/expression.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ fn update_or_lower_expression_needs_parens(
954954
parent.kind(),
955955
JsSyntaxKind::JS_EXTENDS_CLAUSE
956956
| JsSyntaxKind::TS_NON_NULL_ASSERTION_EXPRESSION
957+
| JsSyntaxKind::TS_INSTANTIATION_EXPRESSION
957958
// Callee
958959
| JsSyntaxKind::JS_CALL_EXPRESSION
959960
| JsSyntaxKind::JS_NEW_EXPRESSION

0 commit comments

Comments
 (0)