Skip to content

Commit 4da4228

Browse files
committed
fix(formatter): do not break properties of type annotation in function parameters
Signed-off-by: Naoki Ikeguchi <[email protected]>
1 parent 177f005 commit 4da4228

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

.changeset/cyan-beans-yawn.md

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 properties of object type annotation in function parameters are expanded, which is inconsistent with Prettier.

crates/biome_js_formatter/src/utils/object_like.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::prelude::*;
22
use crate::JsFormatContext;
33
use biome_formatter::write;
44
use biome_formatter::{Format, FormatResult};
5-
use biome_js_syntax::{JsObjectExpression, JsSyntaxToken, TsObjectType};
5+
use biome_js_syntax::{
6+
JsFormalParameter, JsObjectExpression, JsSyntaxToken, TsObjectType, TsTypeAnnotation,
7+
};
68
use biome_rowan::{declare_node_union, AstNode, AstNodeList, AstSeparatedList, SyntaxResult};
79

810
declare_node_union! {
@@ -61,8 +63,13 @@ impl Format<JsFormatContext> for JsObjectLike {
6163
)?;
6264
} else {
6365
let should_insert_space_around_brackets = f.options().bracket_spacing().value();
64-
let should_expand =
65-
f.options().object_wrap().is_preserve() && self.members_have_leading_newline();
66+
let should_expand = f.options().object_wrap().is_preserve()
67+
&& self.members_have_leading_newline()
68+
// const fn = ({ foo }: { foo: string }) => { ... };
69+
// ^ do not break properties here
70+
&& self
71+
.parent::<TsTypeAnnotation>()
72+
.is_none_or(|node| node.parent::<JsFormalParameter>().is_none());
6673

6774
write!(
6875
f,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let foo: {
2+
bar: string;
3+
};
4+
5+
const fn = ({
6+
foo,
7+
bar,
8+
}: {
9+
foo: boolean;
10+
bar: string;
11+
}) => {
12+
console.log(foo, bar);
13+
}
14+
15+
function fn2(foo: {
16+
bar: string;
17+
}) {
18+
console.log(foo);
19+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
info: ts/object/objects.ts
4+
---
5+
# Input
6+
7+
```ts
8+
let foo: {
9+
bar: string;
10+
};
11+
12+
const fn = ({
13+
foo,
14+
bar,
15+
}: {
16+
foo: boolean;
17+
bar: string;
18+
}) => {
19+
console.log(foo, bar);
20+
}
21+
22+
function fn2(foo: {
23+
bar: string;
24+
}) {
25+
console.log(foo);
26+
}
27+
28+
```
29+
30+
31+
=============================
32+
33+
# Outputs
34+
35+
## Output 1
36+
37+
-----
38+
Indent style: Tab
39+
Indent width: 2
40+
Line ending: LF
41+
Line width: 80
42+
Quote style: Double Quotes
43+
JSX quote style: Double Quotes
44+
Quote properties: As needed
45+
Trailing commas: All
46+
Semicolons: Always
47+
Arrow parentheses: Always
48+
Bracket spacing: true
49+
Bracket same line: false
50+
Attribute Position: Auto
51+
Object wrap: Preserve
52+
-----
53+
54+
```ts
55+
let foo: {
56+
bar: string;
57+
};
58+
59+
const fn = ({ foo, bar }: { foo: boolean; bar: string }) => {
60+
console.log(foo, bar);
61+
};
62+
63+
function fn2(foo: { bar: string }) {
64+
console.log(foo);
65+
}
66+
```

0 commit comments

Comments
 (0)