Skip to content

Commit

Permalink
fix(formatter): never format biome.json with trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed May 28, 2024
1 parent fec262f commit eb33352
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
38 changes: 38 additions & 0 deletions crates/biome_cli/tests/cases/biome_json_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,41 @@ fn biome_json_is_not_ignored() {
result,
));
}

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

let file_path = Path::new("biome.json");
let config = r#"{
"formatter": {
"indentStyle": "space",
"indentWidth": 4
},
"json": {
"formatter": {
"trailingCommas": "all"
}
}
}
"#;
fs.insert(file_path.into(), config);

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

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

assert_file_contents(&fs, file_path, config);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"always_disable_trailing_commas_biome_json",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"formatter": {
"indentStyle": "space",
"indentWidth": 4
},
"json": {
"formatter": {
"trailingCommas": "all"
}
}
}
```

# Emitted Messages

```block
Checked 1 file in <TIME>. No fixes needed.
```
12 changes: 11 additions & 1 deletion crates/biome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::OsStr;

use super::{CodeActionsParams, DocumentFileSource, ExtensionHandler, ParseResult};
use crate::configuration::to_analyzer_rules;
use crate::file_handlers::DebugCapabilities;
Expand Down Expand Up @@ -96,14 +98,22 @@ impl ServiceLanguage for JsonLanguage {
global.line_ending.unwrap_or_default()
};

// ensure it never formats biome.json into a form it can't parse
let trailing_commas =
if matches!(path.file_name().and_then(OsStr::to_str), Some("biome.json")) {
TrailingCommas::None
} else {
language.trailing_commas.unwrap_or_default()
};

overrides.to_override_json_format_options(
path,
JsonFormatOptions::new()
.with_line_ending(line_ending)
.with_indent_style(indent_style)
.with_indent_width(indent_width)
.with_line_width(line_width)
.with_trailing_commas(language.trailing_commas.unwrap_or_default()),
.with_trailing_commas(trailing_commas),
)
}

Expand Down

0 comments on commit eb33352

Please sign in to comment.