diff --git a/crates/rome_cli/src/commands/format.rs b/crates/rome_cli/src/commands/format.rs index 26508e99deaf..ffe1ef82ca59 100644 --- a/crates/rome_cli/src/commands/format.rs +++ b/crates/rome_cli/src/commands/format.rs @@ -13,6 +13,7 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> { let configuration = load_config(&session.app.fs, None)?; let configuration = apply_format_settings_from_cli(&mut session, configuration)?; + dbg!(&configuration); session .app .workspace @@ -105,6 +106,14 @@ pub(crate) fn apply_format_settings_from_cli( source, })?; + // if at least one argument is passed via CLI and no "formatter" configuration was passed + // via `rome.json`, we need to create it + if (line_width.is_some() | indent_style.is_some() | size.is_some()) + && configuration.formatter.is_none() + { + configuration.formatter = Some(FormatterConfiguration::default()); + } + if let Some(formatter) = configuration.formatter.as_mut() { match indent_style { Some(IndentStyle::Tab) => { @@ -137,6 +146,12 @@ pub(crate) fn apply_format_settings_from_cli( argument: "--quote-style", source, })?; + + // if at least one argument is passed via CLI and no "javascript.formatter" configuration was passed + // via `rome.json`, we need to create it + if (quote_style.is_some() | quote_properties.is_some()) && configuration.javascript.is_none() { + configuration.javascript = Some(JavascriptConfiguration::with_formatter()) + } if let Some(javascript) = configuration .javascript .as_mut() diff --git a/crates/rome_cli/tests/configs.rs b/crates/rome_cli/tests/configs.rs index d4fc35b3b42a..cd14e3682692 100644 --- a/crates/rome_cli/tests/configs.rs +++ b/crates/rome_cli/tests/configs.rs @@ -144,3 +144,19 @@ pub const CONFIG_INCORRECT_GLOBALS_V2: &str = r#"{ } } }"#; + +pub const CONFIG_ISSUE_3175_1: &str = r#"{ + "formatter": { + "indentStyle": "space", + "indentSize": 2, + "lineWidth": 120 + } +}"#; + +pub const CONFIG_ISSUE_3175_2: &str = r#"{ + "javascript": { + "formatter": { + "quoteStyle": "single" + } + } +}"#; diff --git a/crates/rome_cli/tests/main.rs b/crates/rome_cli/tests/main.rs index d9b7b321bffd..bad310d867d6 100644 --- a/crates/rome_cli/tests/main.rs +++ b/crates/rome_cli/tests/main.rs @@ -734,7 +734,9 @@ mod ci { mod format { use super::*; - use crate::configs::{CONFIG_DISABLED_FORMATTER, CONFIG_FORMAT}; + use crate::configs::{ + CONFIG_DISABLED_FORMATTER, CONFIG_FORMAT, CONFIG_ISSUE_3175_1, CONFIG_ISSUE_3175_2, + }; use crate::snap_test::markup_to_string; use rome_console::markup; use rome_fs::FileSystemExt; @@ -934,6 +936,97 @@ mod format { ); } + #[test] + fn applies_custom_configuration_over_config_file_issue_3175_v1() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let file_path = Path::new("rome.json"); + fs.insert(file_path.into(), CONFIG_ISSUE_3175_1.as_bytes()); + + let file_path = Path::new("file.js"); + fs.insert(file_path.into(), "import React from 'react';\n".as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--quote-style"), + OsString::from("single"), + file_path.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + let mut file = fs + .open(file_path) + .expect("formatting target file was removed by the CLI"); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("failed to read file from memory FS"); + + assert_eq!(content, "import React from 'react';\n"); + + drop(file); + assert_cli_snapshot( + module_path!(), + "applies_custom_configuration_over_config_file_issue_3175_v1", + fs, + console, + ); + } + + #[test] + fn applies_custom_configuration_over_config_file_issue_3175_v2() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let source = r#"function f() { + return 'hey'; +} +"#; + + let file_path = Path::new("rome.json"); + fs.insert(file_path.into(), CONFIG_ISSUE_3175_2.as_bytes()); + + let file_path = Path::new("file.js"); + fs.insert(file_path.into(), source.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + DynRef::Borrowed(&mut console), + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--indent-style"), + OsString::from("space"), + file_path.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + let mut file = fs + .open(file_path) + .expect("formatting target file was removed by the CLI"); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("failed to read file from memory FS"); + + assert_eq!(content, source); + + drop(file); + assert_cli_snapshot( + module_path!(), + "applies_custom_configuration_over_config_file_issue_3175_v2", + fs, + console, + ); + } + #[test] fn applies_custom_quote_style() { let mut fs = MemoryFileSystem::default(); diff --git a/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap new file mode 100644 index 000000000000..4819d371e9f5 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v1.snap @@ -0,0 +1,26 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `rome.json` + +```json +{ + "formatter": { + "indentStyle": "space", + "indentSize": 2, + "lineWidth": 120 + } +} +``` + +## `file.js` + +```js +import React from 'react'; + +``` + +# Emitted Messages + + diff --git a/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap new file mode 100644 index 000000000000..cdf17b21be4a --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_format/applies_custom_configuration_over_config_file_issue_3175_v2.snap @@ -0,0 +1,28 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `rome.json` + +```json +{ + "javascript": { + "formatter": { + "quoteStyle": "single" + } + } +} +``` + +## `file.js` + +```js +function f() { + return 'hey'; +} + +``` + +# Emitted Messages + + diff --git a/crates/rome_service/src/configuration/javascript.rs b/crates/rome_service/src/configuration/javascript.rs index 0fcecf0d3441..de2e197fa176 100644 --- a/crates/rome_service/src/configuration/javascript.rs +++ b/crates/rome_service/src/configuration/javascript.rs @@ -23,6 +23,15 @@ pub struct JavascriptConfiguration { pub globals: Option>, } +impl JavascriptConfiguration { + pub fn with_formatter() -> Self { + Self { + formatter: Some(JavascriptFormatter::default()), + ..JavascriptConfiguration::default() + } + } +} + pub(crate) fn deserialize_globals<'de, D>( deserializer: D, ) -> Result>, D::Error> diff --git a/crates/rome_service/src/file_handlers/javascript.rs b/crates/rome_service/src/file_handlers/javascript.rs index 9301855d1c69..3cebb58a8219 100644 --- a/crates/rome_service/src/file_handlers/javascript.rs +++ b/crates/rome_service/src/file_handlers/javascript.rs @@ -381,6 +381,7 @@ fn format( ) -> Result { let options = settings.format_options::(rome_path); + dbg!(&options); let tree = parse.syntax(); let formatted = format_node(options, &tree)?; let printed = formatted.print();