From 9157e5e32f81f2d6f5b41c624b8d72ca228923af Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Sun, 30 Jul 2023 10:22:18 +0100 Subject: [PATCH] feat: add support for `.jsonc` files (#4734) --- .github/workflows/pull_request.yml | 7 +- Cargo.toml | 2 +- crates/rome_cli/src/configuration.rs | 6 +- crates/rome_cli/tests/commands/format.rs | 77 +++++++++++- .../format_jsonc_files.snap | 37 ++++++ ...treat_known_json_files_as_jsonc_files.snap | 115 ++++++++++++++++++ .../treats_typescript_json_like_jsonc.snap | 37 ++++++ crates/rome_css_parser/Cargo.toml | 2 +- crates/rome_deserialize/src/json.rs | 10 +- crates/rome_js_analyze/Cargo.toml | 12 +- crates/rome_js_transform/Cargo.toml | 10 +- crates/rome_json_analyze/Cargo.toml | 2 +- crates/rome_json_syntax/src/file_source.rs | 4 + .../src/configuration/diagnostics.rs | 8 +- crates/rome_service/src/configuration/mod.rs | 3 +- crates/rome_service/src/file_handlers/json.rs | 18 ++- crates/rome_service/src/file_handlers/mod.rs | 31 +++++ crates/rome_service/tests/spec_tests.rs | 10 +- crates/rome_test_utils/Cargo.toml | 33 +++-- crates/rome_test_utils/src/lib.rs | 8 +- justfile | 4 +- 21 files changed, 385 insertions(+), 51 deletions(-) create mode 100644 crates/rome_cli/tests/snapshots/main_commands_format/format_jsonc_files.snap create mode 100644 crates/rome_cli/tests/snapshots/main_commands_format/treat_known_json_files_as_jsonc_files.snap create mode 100644 crates/rome_cli/tests/snapshots/main_commands_format/treats_typescript_json_like_jsonc.snap diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index e40db743ca3..f037080d87a 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -28,8 +28,11 @@ jobs: uses: moonrepo/setup-rust@v0 with: components: rustfmt - - name: Run rustfmt - run: cargo fmt --all --check + bins: taplo-cli + - name: Run format + run: | + cargo fmt --all --check + taplo fmt -- --locked lint: name: Lint Rust Files diff --git a/Cargo.toml b/Cargo.toml index 4bfe4b8d25b..5e86ca561d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,10 +61,10 @@ rome_parser = { version = "0.0.1", path = "./crates/rome_parser" rome_rowan = { version = "0.0.1", path = "./crates/rome_rowan" } rome_service = { path = "./crates/rome_service" } rome_suppression = { version = "0.0.1", path = "./crates/rome_suppression" } +rome_test_utils = { path = "./crates/rome_test_utils" } rome_text_edit = { version = "0.0.1", path = "./crates/rome_text_edit" } rome_text_size = { version = "0.0.1", path = "./crates/rome_text_size" } tests_macros = { path = "./crates/tests_macros" } -rome_test_utils = { path = "./crates/rome_test_utils" } # Crates needed in the workspace bitflags = "2.3.1" diff --git a/crates/rome_cli/src/configuration.rs b/crates/rome_cli/src/configuration.rs index 437df170af2..7e310486fc1 100644 --- a/crates/rome_cli/src/configuration.rs +++ b/crates/rome_cli/src/configuration.rs @@ -5,6 +5,7 @@ use rome_deserialize::json::deserialize_from_json_str; use rome_deserialize::Deserialized; use rome_diagnostics::{DiagnosticExt, Error, PrintDiagnostic}; use rome_fs::{FileSystem, OpenOptions}; +use rome_json_parser::JsonParserOptions; use rome_service::configuration::diagnostics::CantLoadExtendFile; use rome_service::configuration::ConfigurationPayload; use rome_service::{ @@ -72,7 +73,10 @@ impl LoadedConfiguration { ) })?; - let deserialized = deserialize_from_json_str::(content.as_str()); + let deserialized = deserialize_from_json_str::( + content.as_str(), + JsonParserOptions::default(), + ); deserialized_configurations.push(deserialized) } Ok(deserialized_configurations) diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index 4091095bac5..b5256255993 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -1866,7 +1866,7 @@ fn ignore_comments_error_when_allow_comments() { Args::from([("format"), file_path.as_os_str().to_str().unwrap()].as_slice()), ); - // assert!(result.is_ok(), "run_cli returned {result:?}"); + assert!(result.is_ok(), "run_cli returned {result:?}"); assert_cli_snapshot(SnapshotPayload::new( module_path!(), @@ -1876,3 +1876,78 @@ fn ignore_comments_error_when_allow_comments() { result, )); } + +#[test] +fn format_jsonc_files() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let code = r#" +/*test*/ [ + +/* some other comment*/1, 2, 3] + "#; + let file_path = Path::new("file.jsonc"); + fs.insert(file_path.into(), code.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from([("format"), file_path.as_os_str().to_str().unwrap()].as_slice()), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "format_jsonc_files", + fs, + console, + result, + )); +} + +#[test] +fn treat_known_json_files_as_jsonc_files() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let code = r#" +/*test*/ [ + +/* some other comment*/1, 2, 3] + "#; + let ts = Path::new("files/typescript.json"); + fs.insert(ts.into(), code.as_bytes()); + let eslint = Path::new("files/.eslintrc.json"); + fs.insert(eslint.into(), code.as_bytes()); + let jshint = Path::new("files/.jshintrc"); + fs.insert(jshint.into(), code.as_bytes()); + let babel = Path::new("files/.babelrc"); + fs.insert(babel.into(), code.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from( + [ + ("format"), + ts.as_os_str().to_str().unwrap(), + eslint.as_os_str().to_str().unwrap(), + jshint.as_os_str().to_str().unwrap(), + babel.as_os_str().to_str().unwrap(), + ] + .as_slice(), + ), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "treat_known_json_files_as_jsonc_files", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/snapshots/main_commands_format/format_jsonc_files.snap b/crates/rome_cli/tests/snapshots/main_commands_format/format_jsonc_files.snap new file mode 100644 index 00000000000..025ce940ad8 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_format/format_jsonc_files.snap @@ -0,0 +1,37 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `file.jsonc` + +```jsonc + +/*test*/ [ + +/* some other comment*/1, 2, 3] + +``` + +# Emitted Messages + +```block +file.jsonc format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + i Formatter would have printed the following content: + + 1 │ - + 2 │ - /*test*/·[ + 3 │ - + 4 │ - /*·some·other·comment*/1,·2,·3] + 5 │ - → + 1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3] + 2 │ + + + +``` + +```block +Compared 1 file(s) in