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

feat(biome_js_formatter): introduce the new objectWrap option #5068

Merged
merged 11 commits into from
Feb 10, 2025
Merged
20 changes: 20 additions & 0 deletions .changeset/gentle-spies-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"@biomejs/biome": minor
---

Introduce a new option `objectWrap` for JS formatter.
It does the same thing as Prettier's [Object Wrap](https://prettier.io/docs/options#object-wrap) option.

For example, the following code is considered as already formatted when `objectWrap` is `preserve` (default):

```js
const obj = {
foo: "bar",
};
```

However, when `objectWrap` is `collapse`, it will be formatted to the following output:

```js
const obj = { foo: "bar" };
```
26 changes: 25 additions & 1 deletion crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use biome_formatter::{
QuoteStyle,
};
use biome_fs::{FileSystem, OpenOptions};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
use biome_js_formatter::context::{
ArrowParentheses, ObjectWrap as BiomeObjectWrap, QuoteProperties, Semicolons, TrailingCommas,
};
use biome_json_parser::JsonParserOptions;
use camino::Utf8Path;

Expand Down Expand Up @@ -56,6 +58,8 @@ pub(crate) struct PrettierConfiguration {
arrow_parens: ArrowParens,
/// https://prettier.io/docs/en/options#end-of-line
end_of_line: EndOfLine,
/// https://prettier.io/docs/options#object-wrap
object_wrap: ObjectWrap,
/// https://prettier.io/docs/en/configuration.html#configuration-overrides
overrides: Vec<Override>,
}
Expand All @@ -75,6 +79,7 @@ impl Default for PrettierConfiguration {
jsx_single_quote: false,
arrow_parens: ArrowParens::default(),
end_of_line: EndOfLine::default(),
object_wrap: ObjectWrap::default(),
overrides: vec![],
}
}
Expand Down Expand Up @@ -113,6 +118,8 @@ pub(crate) struct OverrideOptions {
arrow_parens: Option<ArrowParens>,
/// https://prettier.io/docs/en/options#end-of-line
end_of_line: Option<EndOfLine>,
/// https://prettier.io/docs/options#object-wrap
object_wrap: ObjectWrap,
}

#[derive(Clone, Debug, Default, Deserializable, Eq, PartialEq)]
Expand Down Expand Up @@ -147,6 +154,13 @@ enum QuoteProps {
Preserve,
}

#[derive(Clone, Debug, Default, Deserializable, Eq, PartialEq)]
enum ObjectWrap {
#[default]
Preserve,
Collapse,
}

impl From<PrettierTrailingComma> for TrailingCommas {
fn from(value: PrettierTrailingComma) -> Self {
match value {
Expand Down Expand Up @@ -186,6 +200,15 @@ impl From<QuoteProps> for QuoteProperties {
}
}

impl From<ObjectWrap> for BiomeObjectWrap {
fn from(value: ObjectWrap) -> Self {
match value {
ObjectWrap::Preserve => Self::Preserve,
ObjectWrap::Collapse => Self::Collapse,
}
}
}

impl TryFrom<PrettierConfiguration> for biome_configuration::Configuration {
type Error = ParseFormatNumberError;
fn try_from(value: PrettierConfiguration) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -248,6 +271,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::Configuration {
bracket_spacing: Some(value.bracket_spacing.into()),
jsx_quote_style: Some(jsx_quote_style),
attribute_position: Some(AttributePosition::default()),
object_wrap: Some(value.object_wrap.into()),
};
let js_config = biome_configuration::JsConfiguration {
formatter: Some(js_formatter),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
# Emitted Messages

Expand Down Expand Up @@ -71,6 +70,8 @@ The configuration that is contained inside the file `biome.json`
code. Defaults to double.
--javascript-attribute-position=<multiline|auto> The attribute position style in jsx
elements. Defaults to auto.
--object-wrap=<preserve|collapse> Whether to enforce collapsing object literals when
possible. Defaults to preserve.
--javascript-linter-enabled=<true|false> Control the linter for JavaScript (and its super
languages) files.
--javascript-assist-enabled=<true|false> Control the assist for JavaScript (and its super
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
# Emitted Messages

Expand Down Expand Up @@ -72,6 +71,8 @@ The configuration that is contained inside the file `biome.json`
code. Defaults to double.
--javascript-attribute-position=<multiline|auto> The attribute position style in jsx
elements. Defaults to auto.
--object-wrap=<preserve|collapse> Whether to enforce collapsing object literals when
possible. Defaults to preserve.
--javascript-linter-enabled=<true|false> Control the linter for JavaScript (and its super
languages) files.
--javascript-assist-enabled=<true|false> Control the assist for JavaScript (and its super
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
# Emitted Messages

Expand Down Expand Up @@ -56,6 +55,8 @@ Formatting options specific to the JavaScript files
elements. Defaults to auto.
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.
--object-wrap=<preserve|collapse> Whether to enforce collapsing object literals when
possible. Defaults to preserve.

Set of properties to integrate Biome with a VCS software.
--vcs-enabled=<true|false> Whether Biome should integrate itself with the VCS client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.json`

Expand Down Expand Up @@ -47,11 +46,12 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
22 │ + → → → "bracketSameLine":·false,
23 │ + → → → "quoteStyle":·"single",
24 │ + → → → "attributePosition":·"auto",
25 │ + → → → "bracketSpacing":·true
26 │ + → → }
27 │ + → }
28 │ + }
29 │ +
25 │ + → → → "bracketSpacing":·true,
26 │ + → → → "objectWrap":·"preserve"
27 │ + → → }
28 │ + → }
29 │ + }
30 │ +


```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.json`

Expand Down Expand Up @@ -50,11 +49,12 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
21 │ + → → → "bracketSameLine":·false,
22 │ + → → → "quoteStyle":·"single",
23 │ + → → → "attributePosition":·"auto",
24 │ + → → → "bracketSpacing":·true
25 │ + → → }
26 │ + → }
27 │ + }
28 │ +
24 │ + → → → "bracketSpacing":·true,
25 │ + → → → "objectWrap":·"preserve"
26 │ + → → }
27 │ + → }
28 │ + }
29 │ +


```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.json`

Expand Down Expand Up @@ -30,7 +29,8 @@ snapshot_kind: text
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto",
"bracketSpacing": true
"bracketSpacing": true,
"objectWrap": "preserve"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.jsonc`

Expand Down Expand Up @@ -47,11 +46,12 @@ biome.jsonc migrate ━━━━━━━━━━━━━━━━━━━━
22 │ + → → → "bracketSameLine":·false,
23 │ + → → → "quoteStyle":·"single",
24 │ + → → → "attributePosition":·"auto",
25 │ + → → → "bracketSpacing":·true
26 │ + → → }
27 │ + → }
28 │ + }
29 │ +
25 │ + → → → "bracketSpacing":·true,
26 │ + → → → "objectWrap":·"preserve"
27 │ + → → }
28 │ + → }
29 │ + }
30 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,28 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
21 │ + → → → "bracketSameLine":·false,
22 │ + → → → "quoteStyle":·"single",
23 │ + → → → "attributePosition":·"auto",
24 │ + → → → "bracketSpacing":·true
25 │ + → → }
26 │ + → },
27 │ + → "overrides":·[
28 │ + → → {·"includes":·["**/*.test.js"],·"formatter":·{·"indentStyle":·"space"·}·},
29 │ + → → {
30 │ + → → → "includes":·["**/*.spec.js"],
31 │ + → → → "javascript":·{
32 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
33 │ + → → → }
34 │ + → → },
35 │ + → → {
36 │ + → → → "includes":·["**/*.ts"],
37 │ + → → → "javascript":·{
38 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
39 │ + → → → },
40 │ + → → → "formatter":·{·"indentStyle":·"space"·}
41 │ + → → }
42 │ + → ]
43 │ + }
44 │ +
24 │ + → → → "bracketSpacing":·true,
25 │ + → → → "objectWrap":·"preserve"
26 │ + → → }
27 │ + → },
28 │ + → "overrides":·[
29 │ + → → {·"includes":·["**/*.test.js"],·"formatter":·{·"indentStyle":·"space"·}·},
30 │ + → → {
31 │ + → → → "includes":·["**/*.spec.js"],
32 │ + → → → "javascript":·{
33 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
34 │ + → → → }
35 │ + → → },
36 │ + → → {
37 │ + → → → "includes":·["**/*.ts"],
38 │ + → → → "javascript":·{
39 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
40 │ + → → → },
41 │ + → → → "formatter":·{·"indentStyle":·"space"·}
42 │ + → → }
43 │ + → ]
44 │ + }
45 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
23 │ + → → → "bracketSameLine":·false,
24 │ + → → → "quoteStyle":·"single",
25 │ + → → → "attributePosition":·"auto",
26 │ + → → → "bracketSpacing":·true
27 │ + → → }
28 │ + → }
29 │ + }
30 │ +
26 │ + → → → "bracketSpacing":·true,
27 │ + → → → "objectWrap":·"preserve"
28 │ + → → }
29 │ + → }
30 │ + }
31 │ +


```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.json`

Expand Down Expand Up @@ -30,7 +29,8 @@ snapshot_kind: text
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto",
"bracketSpacing": true
"bracketSpacing": true,
"objectWrap": "preserve"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.jsonc`

Expand Down Expand Up @@ -30,7 +29,8 @@ snapshot_kind: text
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto",
"bracketSpacing": true
"bracketSpacing": true,
"objectWrap": "preserve"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
expression: redactor(content)
---
## `biome.json`

Expand Down Expand Up @@ -30,7 +29,8 @@ snapshot_kind: text
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto",
"bracketSpacing": true
"bracketSpacing": true,
"objectWrap": "preserve"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ expression: redactor(content)
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto",
"bracketSpacing": true
"bracketSpacing": true,
"objectWrap": "preserve"
}
}
}
Expand Down
Loading