Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formatter): do not break properties of type annotation in function parameters #5174

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
---

Fix [#2406](https://github.com/biomejs/biome/issues/2406), don't expand properties of object type annotation in function parameters, which was 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);
}
```
Loading