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(overrides): json.formatter.trailingCommas should work #2025

Merged
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
108 changes: 104 additions & 4 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,7 @@ fn format_json_when_allow_trailing_commas_write() {
}

#[test]
fn format_omits_json_trailing_comma() {
fn format_json_trailing_commas_none() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

Expand Down Expand Up @@ -2338,15 +2338,15 @@ fn format_omits_json_trailing_comma() {

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_omits_json_trailing_comma",
"format_json_trailing_commas_none",
fs,
console,
result,
));
}

#[test]
fn format_omits_json_trailing_comma_omit() {
fn format_json_trailing_commas_all() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

Expand Down Expand Up @@ -2382,7 +2382,107 @@ fn format_omits_json_trailing_comma_omit() {

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_omits_json_trailing_comma_omit",
"format_json_trailing_commas_all",
fs,
console,
result,
));
}

#[test]
fn format_json_trailing_commas_overrides_all() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let config_json = r#"{
"json": {
"parser": { "allowTrailingCommas": true },
"formatter": { "trailingCommas": "none" }
},
"overrides": [{
"include": ["file.json"],
"json": {
"formatter": { "trailingCommas": "all" }
}
}]
}"#;
let biome_config = "biome.json";
let code = r#"{ "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar",
}"#;
let file_path = Path::new("file.json");
fs.insert(file_path.into(), code.as_bytes());
fs.insert(biome_config.into(), config_json);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
"--write",
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, Path::new(file_path), "{\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n}\n");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_json_trailing_commas_overrides_all",
fs,
console,
result,
));
}

#[test]
fn format_json_trailing_commas_overrides_none() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let config_json = r#"{
"json": {
"parser": { "allowTrailingCommas": true },
"formatter": { "trailingCommas": "all" }
},
"overrides": [{
"include": ["file.json"],
"json": {
"formatter": { "trailingCommas": "none" }
}
}]
}"#;
let biome_config = "biome.json";
let code = r#"{ "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar", "loreum_ipsum_lorem_ipsum": "bar",
}"#;
let file_path = Path::new("file.json");
fs.insert(file_path.into(), code.as_bytes());
fs.insert(biome_config.into(), config_json);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
"--write",
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, Path::new(file_path), "{\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\",\n\t\"loreum_ipsum_lorem_ipsum\": \"bar\"\n}\n");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_json_trailing_commas_overrides_none",
fs,
console,
result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ expression: content
```block
Formatted 1 file in <TIME>. Fixed 1 file.
```


Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ expression: content
```block
Formatted 1 file in <TIME>. Fixed 1 file.
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"json": {
"parser": { "allowTrailingCommas": true },
"formatter": { "trailingCommas": "none" }
},
"overrides": [
{
"include": ["file.json"],
"json": {
"formatter": { "trailingCommas": "all" }
}
}
]
}
```

## `file.json`

```json
{
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
}

```

# Emitted Messages

```block
Formatted 1 file in <TIME>. Fixed 1 file.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"json": {
"parser": { "allowTrailingCommas": true },
"formatter": { "trailingCommas": "all" }
},
"overrides": [
{
"include": ["file.json"],
"json": {
"formatter": { "trailingCommas": "none" }
}
}
]
}
```

## `file.json`

```json
{
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar",
"loreum_ipsum_lorem_ipsum": "bar"
}

```

# Emitted Messages

```block
Formatted 1 file in <TIME>. Fixed 1 file.
```
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,3 @@ Server:
Workspace:
Open Documents: 0
```


14 changes: 9 additions & 5 deletions crates/biome_json_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ pub struct JsonFormatOptions {
)]
pub enum TrailingCommas {
#[default]
/// The formatter will remove the trailing comma
/// The formatter will remove the trailing commas
None,
/// The trailing comma is allowed and advised
/// The trailing commas are allowed and advised
All,
}

Expand Down Expand Up @@ -131,8 +131,8 @@ impl JsonFormatOptions {
self
}

pub fn with_trailing_comma(mut self, trailing_comma: TrailingCommas) -> Self {
self.trailing_commas = trailing_comma;
pub fn with_trailing_commas(mut self, trailing_commas: TrailingCommas) -> Self {
self.trailing_commas = trailing_commas;
self
}

Expand All @@ -152,6 +152,10 @@ impl JsonFormatOptions {
self.line_width = line_width;
}

pub fn set_trailing_commas(&mut self, trailing_commas: TrailingCommas) {
self.trailing_commas = trailing_commas;
}

pub(crate) fn to_trailing_separator(&self) -> TrailingSeparator {
match self.trailing_commas {
TrailingCommas::None => TrailingSeparator::Omit,
Expand Down Expand Up @@ -192,6 +196,6 @@ impl fmt::Display for JsonFormatOptions {
writeln!(f, "Indent width: {}", self.indent_width.value())?;
writeln!(f, "Line ending: {}", self.line_ending)?;
writeln!(f, "Line width: {}", self.line_width.get())?;
writeln!(f, "Trailing comma: {}", self.trailing_commas)
writeln!(f, "Trailing commas: {}", self.trailing_commas)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand All @@ -41,4 +41,3 @@ Trailing comma: None
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand All @@ -34,4 +34,3 @@ Trailing comma: None
]
```


Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand Down Expand Up @@ -64,4 +64,3 @@ Trailing comma: None
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand All @@ -47,4 +47,3 @@ Trailing comma: None
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand All @@ -50,4 +50,3 @@ Trailing comma: None
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand Down Expand Up @@ -81,4 +81,3 @@ Trailing comma: None
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand All @@ -39,4 +39,3 @@ Trailing comma: None
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Trailing comma: None
Trailing commas: None
-----

```json
Expand Down Expand Up @@ -64,4 +64,3 @@ Trailing comma: None
}
```


Loading
Loading