Skip to content

Commit

Permalink
fix(formatter): apply line ending option (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Jan 18, 2024
1 parent 4cefa72 commit 935bf9d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}
23 changes: 23 additions & 0 deletions crates/biome_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_service/src/file_handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 935bf9d

Please sign in to comment.