Skip to content

Commit

Permalink
fix(formatter): do not break properties of type annotation in functio…
Browse files Browse the repository at this point in the history
…n parameters

Signed-off-by: Naoki Ikeguchi <[email protected]>
  • Loading branch information
siketyan committed Feb 22, 2025
1 parent 177f005 commit 4da4228
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-beans-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed properties of object type annotation in function parameters are expanded, which is inconsistent with Prettier.
13 changes: 10 additions & 3 deletions crates/biome_js_formatter/src/utils/object_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::prelude::*;
use crate::JsFormatContext;
use biome_formatter::write;
use biome_formatter::{Format, FormatResult};
use biome_js_syntax::{JsObjectExpression, JsSyntaxToken, TsObjectType};
use biome_js_syntax::{
JsFormalParameter, JsObjectExpression, JsSyntaxToken, TsObjectType, TsTypeAnnotation,
};
use biome_rowan::{declare_node_union, AstNode, AstNodeList, AstSeparatedList, SyntaxResult};

declare_node_union! {
Expand Down Expand Up @@ -61,8 +63,13 @@ impl Format<JsFormatContext> for JsObjectLike {
)?;
} else {
let should_insert_space_around_brackets = f.options().bracket_spacing().value();
let should_expand =
f.options().object_wrap().is_preserve() && self.members_have_leading_newline();
let should_expand = f.options().object_wrap().is_preserve()
&& self.members_have_leading_newline()
// const fn = ({ foo }: { foo: string }) => { ... };
// ^ do not break properties here
&& self
.parent::<TsTypeAnnotation>()
.is_none_or(|node| node.parent::<JsFormalParameter>().is_none());

write!(
f,
Expand Down
19 changes: 19 additions & 0 deletions crates/biome_js_formatter/tests/specs/ts/object/objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let foo: {
bar: string;
};

const fn = ({
foo,
bar,
}: {
foo: boolean;
bar: string;
}) => {
console.log(foo, bar);
}

function fn2(foo: {
bar: string;
}) {
console.log(foo);
}
66 changes: 66 additions & 0 deletions crates/biome_js_formatter/tests/specs/ts/object/objects.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: ts/object/objects.ts
---
# Input

```ts
let foo: {
bar: string;
};

const fn = ({
foo,
bar,
}: {
foo: boolean;
bar: string;
}) => {
console.log(foo, bar);
}

function fn2(foo: {
bar: string;
}) {
console.log(foo);
}

```


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

# 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
Object wrap: Preserve
-----

```ts
let foo: {
bar: string;
};

const fn = ({ foo, bar }: { foo: boolean; bar: string }) => {
console.log(foo, bar);
};

function fn2(foo: { bar: string }) {
console.log(foo);
}
```

0 comments on commit 4da4228

Please sign in to comment.