From 2cd670e7635cf9a9fe3bbb4cb3eb0a94854dc22e Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 1 Mar 2024 16:10:04 +0000 Subject: [PATCH] apply suggestions --- CHANGELOG.md | 1 - crates/biome_cli/src/commands/rage.rs | 2 +- crates/biome_cli/src/execute/migrate.rs | 2 +- .../execute/process_file/workspace_file.rs | 4 +- crates/biome_cli/src/execute/std_in.rs | 9 ++-- crates/biome_cli/tests/commands/format.rs | 48 ++++++++++++++++++- .../main_commands_check/check_help.snap | 2 +- .../snapshots/main_commands_ci/ci_help.snap | 2 +- .../main_commands_format/format_help.snap | 2 +- ...ormat_json_when_allow_trailing_commas.snap | 2 +- ...json_when_allow_trailing_commas_write.snap | 2 +- .../format_omits_json_trailing_comma.snap | 2 +- ...format_omits_json_trailing_comma_omit.snap | 35 ++++++++++++++ .../with_formatter_configuration.snap | 2 +- crates/biome_json_formatter/src/context.rs | 26 +++++----- .../specs/json/array/empty_line.json.snap | 1 + .../specs/json/array/fill_layout.json.snap | 1 + .../tests/specs/json/array/layout.json.snap | 1 + .../specs/json/array/multi_line.json.snap | 1 + .../tests/specs/json/array/nested.json.snap | 1 + .../json/array/one_per_line_layout.json.snap | 1 + .../specs/json/array/single_line.json.snap | 1 + .../comments/empty_with_comments.json.snap | 1 + .../specs/json/comments/multiline.json.snap | 1 + .../tests/specs/json/empty.json.snap | 1 + .../tests/specs/json/number.json.snap | 1 + .../tests/specs/json/object/complex.json.snap | 1 + .../specs/json/object/missing_value.json.snap | 1 + .../specs/json/object/multi_line.json.snap | 1 + .../json/object/multi_line_long.json.snap | 1 + .../specs/json/object/one_line.json.snap | 1 + .../specs/json/object/one_line_long.json.snap | 1 + .../tests/specs/json/object/string.json.snap | 1 + .../tests/specs/json/smoke.json.snap | 1 + .../undefined/utf8_bom_empty_object.json.snap | 1 + .../specs/prettier/json/json/array.json.snap | 22 ++------- .../biome_lsp/src/handlers/text_document.rs | 2 +- .../biome_service/src/configuration/json.rs | 10 ++-- .../biome_service/src/file_handlers/json.rs | 22 ++------- crates/biome_service/src/settings.rs | 2 +- crates/biome_service/src/workspace.rs | 3 +- crates/biome_service/src/workspace/server.rs | 6 ++- .../@biomejs/biome/configuration_schema.json | 18 ++++++- .../src/content/docs/internals/changelog.md | 1 - .../content/docs/reference/configuration.mdx | 8 ++-- 45 files changed, 168 insertions(+), 87 deletions(-) create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma_omit.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index bf79a11c7e01..c22f545033d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -622,7 +622,6 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b #### 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"`. - When this option isn't provided, and you set the parser option `json.parser.allowTrailingComma` to `true`, the value of `json.formatter.trailingComma` is `"all"`. #### Bug fixes diff --git a/crates/biome_cli/src/commands/rage.rs b/crates/biome_cli/src/commands/rage.rs index 49cbe2633a81..ccd28d320619 100644 --- a/crates/biome_cli/src/commands/rage.rs +++ b/crates/biome_cli/src/commands/rage.rs @@ -263,7 +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 Comma", markup!({DebugDisplayOption(json_formatter_configuration.trailing_comma)}))} + {KeyValuePair("Trailing Commas", markup!({DebugDisplayOption(json_formatter_configuration.trailing_commas)}))} ).fmt(fmt)?; } diff --git a/crates/biome_cli/src/execute/migrate.rs b/crates/biome_cli/src/execute/migrate.rs index d1d96fa1b3e6..f2d9917b1842 100644 --- a/crates/biome_cli/src/execute/migrate.rs +++ b/crates/biome_cli/src/execute/migrate.rs @@ -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( diff --git a/crates/biome_cli/src/execute/process_file/workspace_file.rs b/crates/biome_cli/src/execute/process_file/workspace_file.rs index 29738d285d1b..6503c64c6baa 100644 --- a/crates/biome_cli/src/execute/process_file/workspace_file.rs +++ b/crates/biome_cli/src/execute/process_file/workspace_file.rs @@ -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}; @@ -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(), diff --git a/crates/biome_cli/src/execute/std_in.rs b/crates/biome_cli/src/execute/std_in.rs index 9456364e6834..84635a263d10 100644 --- a/crates/biome_cli/src/execute/std_in.rs +++ b/crates/biome_cli/src/execute/std_in.rs @@ -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; @@ -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 })?; @@ -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 { diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index b1ce5643dc6b..abec31458ed6 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -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!(), @@ -2284,7 +2284,7 @@ fn format_omits_json_trailing_comma() { let config_json = r#"{ "json": { "parser": { "allowTrailingCommas": true }, - "formatter": { "trailingComma": "none" } + "formatter": { "trailingCommas": "none" } } }"#; let biome_config = "biome.json"; @@ -2320,6 +2320,50 @@ fn format_omits_json_trailing_comma() { )); } +#[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(); diff --git a/crates/biome_cli/tests/snapshots/main_commands_check/check_help.snap b/crates/biome_cli/tests/snapshots/main_commands_check/check_help.snap index 98beb9494a42..04073a73c6a3 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_check/check_help.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_check/check_help.snap @@ -70,7 +70,7 @@ 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-comma= Print trailing commas wherever possible in multi-line + --json-formatter-trailing-commas= Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to "omit". Global options applied to all commands diff --git a/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap b/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap index 00866aa0180c..446f0cd56bea 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap @@ -72,7 +72,7 @@ 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-comma= Print trailing commas wherever possible in multi-line + --json-formatter-trailing-commas= Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to "omit". Global options applied to all commands diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/format_help.snap b/crates/biome_cli/tests/snapshots/main_commands_format/format_help.snap index 4adbb8460705..394695de74d7 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_format/format_help.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_format/format_help.snap @@ -106,7 +106,7 @@ 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-comma= Print trailing commas wherever possible in multi-line + --json-formatter-trailing-commas= 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`. diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas.snap b/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas.snap index 5b1c3d1f59b1..52547132a5ec 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas.snap @@ -45,7 +45,7 @@ file.json format ━━━━━━━━━━━━━━━━━━━━━ 3 │ - ········1, 4 │ - ····], 5 │ - } - 2 │ + → "array":·[1], + 2 │ + → "array":·[1] 3 │ + } 4 │ + diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas_write.snap b/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas_write.snap index c266c6725bc3..233f36f1bd0c 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas_write.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_format/format_json_when_allow_trailing_commas_write.snap @@ -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" } ``` diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma.snap b/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma.snap index 958bbb46a3b2..872ff7d80021 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma.snap @@ -8,7 +8,7 @@ expression: content { "json": { "parser": { "allowTrailingCommas": true }, - "formatter": { "trailingComma": "none" } + "formatter": { "trailingCommas": "none" } } } ``` diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma_omit.snap b/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma_omit.snap new file mode 100644 index 000000000000..943bf42e6a3f --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_commands_format/format_omits_json_trailing_comma_omit.snap @@ -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