diff --git a/.changeset/icy-views-lick.md b/.changeset/icy-views-lick.md new file mode 100644 index 000000000000..17839a23bddf --- /dev/null +++ b/.changeset/icy-views-lick.md @@ -0,0 +1,14 @@ +--- +"@biomejs/biome": minor +--- + +The `formatWithErrors` option can now be set via CLI using the `--format-with-errors` flag. + +This flag was previously only available in the configuration file. It allows formatting to proceed on files with syntax errors, which is useful during development when you want to auto-format code while fixing syntax issues. + +#### Example + +```shell +biome format --format-with-errors=true --write file.js +``` + diff --git a/crates/biome_cli/tests/commands/check.rs b/crates/biome_cli/tests/commands/check.rs index 6c8e39fc84b2..7fedfad28813 100644 --- a/crates/biome_cli/tests/commands/check.rs +++ b/crates/biome_cli/tests/commands/check.rs @@ -3251,3 +3251,38 @@ fn check_does_not_enable_assist() { result, )); } + +#[test] +fn check_format_with_syntax_errors_when_flag_enabled() { + let fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let invalid = Utf8Path::new("invalid.js"); + fs.insert(invalid.into(), "while ) {}".as_bytes()); + + let (fs, result) = run_cli( + fs, + &mut console, + Args::from( + [ + "check", + "--format-with-errors=true", + "--write", + invalid.as_str(), + ] + .as_slice(), + ), + ); + + assert!(result.is_err(), "run_cli returned {result:?}"); + + assert_file_contents(&fs, invalid, "while ) {}\n"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "check_format_with_syntax_errors_when_flag_enabled", + fs, + console, + result, + )); +} diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index a953a9b49f78..68478f8391d5 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -3549,3 +3549,38 @@ fn should_not_format_file_with_syntax_errors() { result, )); } + +#[test] +fn should_format_file_with_syntax_errors_when_flag_enabled() { + let fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let invalid = Utf8Path::new("invalid.js"); + fs.insert(invalid.into(), "while ) {}".as_bytes()); + + let (fs, result) = run_cli( + fs, + &mut console, + Args::from( + [ + "format", + "--format-with-errors=true", + "--write", + invalid.as_str(), + ] + .as_slice(), + ), + ); + + assert!(result.is_err(), "run_cli returned {result:?}"); + + assert_file_contents(&fs, invalid, "while ) {}\n"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "should_format_file_with_syntax_errors_when_flag_enabled", + fs, + console, + result, + )); +} diff --git a/crates/biome_cli/tests/snapshots/main_commands_check/check_format_with_syntax_errors_when_flag_enabled.snap b/crates/biome_cli/tests/snapshots/main_commands_check/check_format_with_syntax_errors_when_flag_enabled.snap new file mode 100644 index 000000000000..eec4334f47a1 --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_commands_check/check_format_with_syntax_errors_when_flag_enabled.snap @@ -0,0 +1,54 @@ +--- +source: crates/biome_cli/tests/snap_test.rs +expression: redactor(content) +--- +## `invalid.js` + +```js +while ) {} + +``` + +# Termination Message + +```block +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while applying fixes. + + + +``` + +# Emitted Messages + +```block +invalid.js:1:7 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × expected `(` but instead found `)` + + > 1 │ while ) {} + │ ^ + + i Remove ) + + +``` + +```block +invalid.js:1:7 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × expected `(` but instead found `)` + + > 1 │ while ) {} + │ ^ + + i Remove ) + + +``` + +```block +Checked 1 file in