From 935bf9db3436c48b24e1d8b77d0e2b244f8fd97b Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 18 Jan 2024 14:23:18 +0000 Subject: [PATCH] fix(formatter): apply line ending option (#1591) --- CHANGELOG.md | 1 + crates/biome_cli/tests/commands/format.rs | 41 +++++++++++++++++++ crates/biome_formatter/src/printer/mod.rs | 23 +++++++++++ .../src/file_handlers/javascript.rs | 7 ++++ .../biome_service/src/file_handlers/json.rs | 7 ++++ .../src/content/docs/internals/changelog.mdx | 1 + 6 files changed, 80 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81c8aa0a0a4e..8edb840662d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom #### Bug fixed +- Fix [#1178](https://github.com/biomejs/biome/issues/1178), where the line ending option wasn't correctly applied. Contributed by @ematipico - Fix [#1571](https://github.com/biomejs/biome/issues/1571). Fix invalid formatting of nested multiline comments. Contributed by @ah-yu ### JavaScript APIs diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index 60d6d0ab7d56..1ff8f4525031 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -2689,3 +2689,44 @@ fn override_don_t_affect_ignored_files() { result, )); } + +#[test] +fn format_with_configured_line_ending() { + let mut console = BufferConsole::default(); + let mut fs = MemoryFileSystem::default(); + + let config = r#"{ + "formatter": { + "lineEnding": "crlf", + "lineWidth": 20 + } + }"#; + let code_json = r#"{ "name": "mike", "surname": "ross" }"#; + let code_js = r#"const b = { "name": "mike", "surname": "ross" }"#; + let json_file = Path::new("input.json"); + fs.insert(json_file.into(), code_json.as_bytes()); + + let js_file = Path::new("input.js"); + fs.insert(js_file.into(), code_js.as_bytes()); + + let file_path = Path::new("biome.json"); + fs.insert(file_path.into(), config); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from([("format"), ("."), ("--write")].as_slice()), + ); + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_file_contents( + &fs, + json_file, + "{\r\n\t\"name\": \"mike\",\r\n\t\"surname\": \"ross\"\r\n}\r\n", + ); + assert_file_contents( + &fs, + js_file, + "const b = {\r\n\tname: \"mike\",\r\n\tsurname: \"ross\",\r\n};\r\n", + ); +} diff --git a/crates/biome_formatter/src/printer/mod.rs b/crates/biome_formatter/src/printer/mod.rs index c88ef3ded0e3..a26dd6664ea0 100644 --- a/crates/biome_formatter/src/printer/mod.rs +++ b/crates/biome_formatter/src/printer/mod.rs @@ -1401,6 +1401,29 @@ a"#, ); } + #[test] + fn it_converts_line_endings_to_cr() { + let options = PrinterOptions { + line_ending: LineEnding::Cr, + ..PrinterOptions::default() + }; + + let result = format_with_options( + &format_args![ + text("function main() {"), + block_indent(&text("let x = `This is a multiline\nstring`;")), + text("}"), + hard_line_break() + ], + options, + ); + + assert_eq!( + "function main() {\r\tlet x = `This is a multiline\rstring`;\r}\r", + result.as_code() + ); + } + #[test] fn it_breaks_a_group_if_a_string_contains_a_newline() { let result = format(&FormatArrayElements { diff --git a/crates/biome_service/src/file_handlers/javascript.rs b/crates/biome_service/src/file_handlers/javascript.rs index 9dc42093f06f..fdca5bf134e9 100644 --- a/crates/biome_service/src/file_handlers/javascript.rs +++ b/crates/biome_service/src/file_handlers/javascript.rs @@ -111,10 +111,17 @@ impl Language for JsLanguage { } else { global.indent_width.unwrap_or_default() }; + + let line_ending = if let Some(line_ending) = language.line_ending { + line_ending + } else { + global.line_ending.unwrap_or_default() + }; let options = JsFormatOptions::new(path.as_path().try_into().unwrap_or_default()) .with_indent_style(indent_style) .with_indent_width(indent_width) .with_line_width(line_width) + .with_line_ending(line_ending) .with_quote_style(language.quote_style.unwrap_or_default()) .with_jsx_quote_style(language.jsx_quote_style.unwrap_or_default()) .with_quote_properties(language.quote_properties.unwrap_or_default()) diff --git a/crates/biome_service/src/file_handlers/json.rs b/crates/biome_service/src/file_handlers/json.rs index 6115348d397d..7295770856db 100644 --- a/crates/biome_service/src/file_handlers/json.rs +++ b/crates/biome_service/src/file_handlers/json.rs @@ -77,9 +77,16 @@ impl Language for JsonLanguage { global.indent_width.unwrap_or_default() }; + let line_ending = if let Some(line_ending) = language.line_ending { + line_ending + } else { + global.line_ending.unwrap_or_default() + }; + overrides.override_json_format_options( path, JsonFormatOptions::new(path.as_path().try_into().unwrap_or_default()) + .with_line_ending(line_ending) .with_indent_style(indent_style) .with_indent_width(indent_width) .with_line_width(line_width), diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index f894a5ad3fbb..c8e3ed6b39ed 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -28,6 +28,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom #### Bug fixed +- Fix [#1178](https://github.com/biomejs/biome/issues/1178), where the line ending option wasn't correctly applied. Contributed by @ematipico - Fix [#1571](https://github.com/biomejs/biome/issues/1571). Fix invalid formatting of nested multiline comments. Contributed by @ah-yu ### JavaScript APIs