diff --git a/CHANGELOG.md b/CHANGELOG.md index f64c3d32f79e..fb85d096d6c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom #### New features - Add a new option [`--line-ending`](https://biomejs.dev/reference/configuration/#formatterlineending). This option allows changing the type of line endings. Contributed by @SuperchupuDev +- Added a new option called `--bracket-spacing` to the formatter. This option allows you to control whether spaces are inserted around the brackets of object literals. [#627](https://github.com/biomejs/biome/issues/627). Contributed by @faultyserver #### Bug fixes diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index 67afa485b560..0566043e9be7 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -69,6 +69,16 @@ action => {}; (action = 1) => {}; "#; +const APPLY_BRACKET_SPACING_BEFORE: &str = r#"import { Foo } from "bar"; +let foo = { a, b }; +const { a, b } = foo; +"#; + +const APPLY_BRACKET_SPACING_AFTER: &str = r#"import {Foo} from "bar"; +let foo = {a, b}; +const {a, b} = foo; +"#; + // Without this, Test (windows-latest) fails with: `warning: constant `DEFAULT_CONFIGURATION_BEFORE` is never used` #[allow(dead_code)] const DEFAULT_CONFIGURATION_BEFORE: &str = r#"function f() { @@ -649,6 +659,51 @@ fn applies_custom_arrow_parentheses() { )); } +#[test] +fn applies_custom_bracket_spacing() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let file_path = Path::new("file.js"); + fs.insert(file_path.into(), APPLY_BRACKET_SPACING_BEFORE.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from( + [ + ("format"), + ("--bracket-spacing"), + ("false"), + ("--write"), + file_path.as_os_str().to_str().unwrap(), + ] + .as_slice(), + ), + ); + + 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, APPLY_BRACKET_SPACING_AFTER); + + drop(file); + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "applies_custom_bracket_spacing", + fs, + console, + result, + )); +} + #[test] fn trailing_comma_parse_errors() { let mut console = BufferConsole::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 62196411ca6c..5fbc08a73388 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 @@ -37,6 +37,8 @@ The configuration that is contained inside the file `biome.json` only in for statements where it is necessary because of ASI. --arrow-parentheses= Whether to add non-necessary parentheses to arrow functions. Defaults to "always". + --bracket-spacing= Whether to insert spaces around brackets in object literals. + Defaults to true. --javascript-formatter-enabled= Control the formatter for JavaScript (and its super languages) files. --javascript-formatter-indent-style= The indent style applied to JavaScript (and 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 c809f32b6cf5..c4a147e5660e 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 @@ -39,6 +39,8 @@ The configuration that is contained inside the file `biome.json` only in for statements where it is necessary because of ASI. --arrow-parentheses= Whether to add non-necessary parentheses to arrow functions. Defaults to "always". + --bracket-spacing= Whether to insert spaces around brackets in object literals. + Defaults to true. --javascript-formatter-enabled= Control the formatter for JavaScript (and its super languages) files. --javascript-formatter-indent-style= The indent style applied to JavaScript (and diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing.snap b/crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing.snap new file mode 100644 index 000000000000..785ac2dd7037 --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing.snap @@ -0,0 +1,20 @@ +--- +source: crates/biome_cli/tests/snap_test.rs +expression: content +--- +## `file.js` + +```js +import {Foo} from "bar"; +let foo = {a, b}; +const {a, b} = foo; + +``` + +# Emitted Messages + +```block +Formatted 1 file(s) in