Skip to content

Commit

Permalink
feat(formatter): add option json.formatter.trailingComma (#1948)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Mar 2, 2024
1 parent d555d91 commit d945159
Show file tree
Hide file tree
Showing 56 changed files with 364 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Formatter

#### New features

- Add option `json.formatter.trailingComma`, to provide a better control over the trailing comma in JSON/JSONC files. Its default value is `"none"`.

#### Bug fixes

- Fix [#1178](https://github.com/biomejs/biome/issues/1178), where the line ending option wasn't correctly applied.
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Indent size", markup!({DebugDisplayOption(json_formatter_configuration.indent_size)}))}
{KeyValuePair("Line ending", markup!({DebugDisplayOption(json_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(json_formatter_configuration.line_width.map(|lw| lw.get()))}))}
{KeyValuePair("Trailing Commas", markup!({DebugDisplayOption(json_formatter_configuration.trailing_commas)}))}
).fmt(fmt)?;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/src/execute/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
path: biome_path.clone(),
content: configuration_content.to_string(),
version: 0,
document_file_source: JsonFileSource::json().into(),
document_file_source: Some(JsonFileSource::json().into()),
})?;

let parsed = parse_json_with_cache(
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/src/execute/process_file/workspace_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::execute::diagnostics::{ResultExt, ResultIoExt};
use crate::execute::process_file::SharedTraversalOptions;
use biome_diagnostics::{category, Error};
use biome_fs::{BiomePath, File, OpenOptions};
use biome_service::workspace::{DocumentFileSource, FileGuard, OpenFileParams};
use biome_service::workspace::{FileGuard, OpenFileParams};
use biome_service::{Workspace, WorkspaceError};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -36,7 +36,7 @@ impl<'ctx, 'app> WorkspaceFile<'ctx, 'app> {
let guard = FileGuard::open(
ctx.workspace,
OpenFileParams {
document_file_source: DocumentFileSource::from_path(&biome_path),
document_file_source: None,
path: biome_path,
version: 0,
content: input.clone(),
Expand Down
9 changes: 4 additions & 5 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use biome_diagnostics::Diagnostic;
use biome_diagnostics::PrintDiagnostic;
use biome_fs::BiomePath;
use biome_service::workspace::{
ChangeFileParams, DocumentFileSource, FeaturesBuilder, FixFileParams, FormatFileParams,
OpenFileParams, OrganizeImportsParams, PullDiagnosticsParams, RuleCategories,
SupportsFeatureParams,
ChangeFileParams, FeaturesBuilder, FixFileParams, FormatFileParams, OpenFileParams,
OrganizeImportsParams, PullDiagnosticsParams, RuleCategories, SupportsFeatureParams,
};
use biome_service::WorkspaceError;
use std::borrow::Cow;
Expand Down Expand Up @@ -49,7 +48,7 @@ pub(crate) fn run<'a>(
path: biome_path.clone(),
version: 0,
content: content.into(),
document_file_source: DocumentFileSource::default(),
document_file_source: None,
})?;
let printed = workspace.format_file(FormatFileParams { path: biome_path })?;

Expand All @@ -72,7 +71,7 @@ pub(crate) fn run<'a>(
path: biome_path.clone(),
version: 0,
content: content.into(),
document_file_source: DocumentFileSource::default(),
document_file_source: None,
})?;
// apply fix file of the linter
let file_features = workspace.file_features(SupportsFeatureParams {
Expand Down
90 changes: 89 additions & 1 deletion crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ fn format_json_when_allow_trailing_commas_write() {

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_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!(),
Expand All @@ -2276,6 +2276,94 @@ fn format_json_when_allow_trailing_commas_write() {
));
}

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

let config_json = r#"{
"json": {
"parser": { "allowTrailingCommas": true },
"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_omits_json_trailing_comma",
fs,
console,
result,
));
}

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

let config_json = r#"{
"json": {
"parser": { "allowTrailingCommas": true },
"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_omits_json_trailing_comma_omit",
fs,
console,
result,
));
}

#[test]
fn treat_known_json_files_as_jsonc_files() {
let mut fs = MemoryFileSystem::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ The configuration that is contained inside the file `biome.json`
languages) files.
--json-formatter-line-width=NUMBER What's the max width of a line applied to JSON (and its super
languages) files. Defaults to 80.
--json-formatter-trailing-commas=<omit|allow> Print trailing commas wherever possible in multi-line
comma-separated syntactic structures. Defaults to "omit".
Global options applied to all commands
--colors=<off|force> Set the formatting mode for markup: "off" prints everything as plain text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ The configuration that is contained inside the file `biome.json`
languages) files.
--json-formatter-line-width=NUMBER What's the max width of a line applied to JSON (and its super
languages) files. Defaults to 80.
--json-formatter-trailing-commas=<omit|allow> Print trailing commas wherever possible in multi-line
comma-separated syntactic structures. Defaults to "omit".
Global options applied to all commands
--colors=<off|force> Set the formatting mode for markup: "off" prints everything as plain text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Available options:
languages) files.
--json-formatter-line-width=NUMBER What's the max width of a line applied to JSON (and its super
languages) files. Defaults to 80.
--json-formatter-trailing-commas=<omit|allow> Print trailing commas wherever possible in multi-line
comma-separated syntactic structures. Defaults to "omit".
--stdin-file-path=PATH Use this option when you want to format code piped from `stdin`, and
print the output to `stdout`.
The file doesn't need to exist on disk, what matters is the extension of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ file.json format ━━━━━━━━━━━━━━━━━━━━━
3 │ - ········1,
4 │ - ····],
5 │ - }
2 │ + → "array":·[1],
2 │ + → "array":·[1]
3 │ + }
4 │ +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ expression: content
"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"
}

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

```json
{
"json": {
"parser": { "allowTrailingCommas": true },
"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
@@ -0,0 +1,35 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"json": {
"parser": { "allowTrailingCommas": true },
"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
Expand Up @@ -110,6 +110,7 @@ JSON Formatter:
Indent size: unset
Line ending: Lf
Line width: 100
Trailing Commas: unset
Server:
Version: 0.0.0
Expand Down
19 changes: 13 additions & 6 deletions crates/biome_json_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ version = "0.4.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_diagnostics = { workspace = true }
biome_formatter = { workspace = true }
biome_json_syntax = { workspace = true }
biome_rowan = { workspace = true }
biome_suppression = { workspace = true }
biome_deserialize = { workspace = true }
biome_deserialize_macros = { workspace = true }
biome_diagnostics = { workspace = true }
biome_formatter = { workspace = true }
biome_json_syntax = { workspace = true }
biome_rowan = { workspace = true }
biome_suppression = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }


[dev-dependencies]
biome_formatter_test = { path = "../biome_formatter_test" }
Expand All @@ -27,13 +32,15 @@ biome_json_parser = { path = "../biome_json_parser" }
biome_parser = { path = "../biome_parser" }
countme = { workspace = true, features = ["enable"] }
insta = { workspace = true, features = ["glob"] }
serde = { version = "1", features = ["derive"] }
serde_json = { workspace = true }
tests_macros = { path = "../tests_macros" }

# cargo-workspaces metadata
[package.metadata.workspaces]
independent = true

[features]
serde = ["dep:serde", "schemars"]

[lints]
workspace = true

0 comments on commit d945159

Please sign in to comment.