Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add --write, --fix and --unsafe options to fixable commands #2898

Merged
merged 32 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
36b780b
feat: write, fix, unsafe options to lint
unvalley May 17, 2024
669d04e
Add option comments and rename to unsafe_
unvalley May 17, 2024
22f1642
Add a fix option to format
unvalley May 17, 2024
980057f
Refactor functions and naming
unvalley May 17, 2024
815bedc
Add write, fix and unsafe to check command
unvalley May 17, 2024
f149636
Fix build error
unvalley May 17, 2024
8fcdfcc
Update option comments
unvalley May 17, 2024
e21f4a0
Fix incompatible arguments
unvalley May 17, 2024
5721fe3
Fix options derive
unvalley May 17, 2024
01f4be0
Deprecate --apply and --apply-unsafe
unvalley May 17, 2024
3244876
Remove deprecation
unvalley May 17, 2024
fbda3eb
Remove unused import
unvalley May 17, 2024
5bfd3c7
Update check, lint, format help snapshot
unvalley May 17, 2024
c8b9b1c
Add command tests
unvalley May 17, 2024
3bf9376
Recommend using --fix --unsafe
unvalley May 17, 2024
a6db3b1
Add snapshot tests
unvalley May 17, 2024
7994ae2
Fix if condition and add comment test cases
unvalley May 17, 2024
fd3cd7e
Update snapshot tests that should be incompatible
unvalley May 17, 2024
3eb8988
Fix snapshot
unvalley May 17, 2024
f002fab
CHANGELOG.md
unvalley May 17, 2024
e29c1c1
Merge branch 'main' into resolve-#2267
unvalley May 17, 2024
2648e9a
Merge branch 'main' into resolve-#2267
unvalley May 17, 2024
5c9cb7f
Make biome migrate command accepts --fix option
unvalley May 19, 2024
5b80116
Prioritize --write in command usage and help
unvalley May 19, 2024
5d56edb
Update CHANGELOG.md
unvalley May 19, 2024
62e265d
Merge branch 'main' into resolve-#2267
unvalley May 19, 2024
b16f27a
Add argument console and update tests for readability
unvalley May 19, 2024
868fedc
Fix to import BufferConsole
unvalley May 19, 2024
089ed2a
Pass arguments correctly
unvalley May 19, 2024
b61d013
Fix lint errors
unvalley May 19, 2024
9b9994b
Merge branch 'main' into resolve-#2267
unvalley May 19, 2024
3d18445
Add snapshot tests for migrate <eslint|prettier> --fix
unvalley May 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Add new CLI options to control the CSS formatting. Check the [CLI reference page](https://biomejs.dev/reference/cli/) for more details. Contributed by @ematipico

- Add new options `--write`, `--fix` (alias of `--write`) and `--unsafe` to the command `biome lint` and `biome check`.
Add a new option `--fix` (alias of `--write`) to the command `biome format` and `biome migrate`.

```shell
biome <lint|check> --<write|fix> [--unsafe]
biome format --<write|fix>
biome migrate --<write|fix>
```

The `biome <lint|check> --<write|fix>` has the same behavior as `biome <lint|check> --apply`.
The `biome <lint|check> --<write|fix> --unsafe` has the same behavior as `biome <lint|check> --apply-unsafe`.
The `biome format --fix` has the same behavior as `biome format --write`.
The `biome migrate --fix` has the same behavior as `biome migrate --write`.

This change allows these commands to write modifications in the same options.

Contributed by @unvalley

#### Enhancements

- Biome now executes commands (lint, format, check and ci) on the working directory by default. [#2266](https://github.com/biomejs/biome/issues/2266) Contributed by @unvalley
Expand Down
29 changes: 16 additions & 13 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ use biome_service::configuration::PartialConfigurationExt;
use biome_service::workspace::RegisterProjectFolderParams;
use biome_service::{
configuration::{load_configuration, LoadedConfiguration},
workspace::{FixFileMode, UpdateSettingsParams},
workspace::UpdateSettingsParams,
};
use std::ffi::OsString;

use super::{determine_fix_file_mode, FixFileModeOptions};

pub(crate) struct CheckCommandPayload {
pub(crate) apply: bool,
pub(crate) apply_unsafe: bool,
pub(crate) write: bool,
pub(crate) fix: bool,
pub(crate) unsafe_: bool,
pub(crate) cli_options: CliOptions,
pub(crate) configuration: Option<PartialConfiguration>,
pub(crate) paths: Vec<OsString>,
Expand All @@ -41,6 +46,9 @@ pub(crate) fn check(
let CheckCommandPayload {
apply,
apply_unsafe,
write,
fix,
unsafe_,
cli_options,
configuration,
mut paths,
Expand All @@ -54,18 +62,13 @@ pub(crate) fn check(
} = payload;
setup_cli_subscriber(cli_options.log_level, cli_options.log_kind);

let fix_file_mode = if apply && apply_unsafe {
return Err(CliDiagnostic::incompatible_arguments(
"--apply",
"--apply-unsafe",
));
} else if !apply && !apply_unsafe {
None
} else if apply && !apply_unsafe {
Some(FixFileMode::SafeFixes)
} else {
Some(FixFileMode::SafeAndUnsafeFixes)
};
let fix_file_mode = determine_fix_file_mode(FixFileModeOptions {
apply,
apply_unsafe,
write,
fix,
unsafe_,
})?;

let loaded_configuration =
load_configuration(&session.app.fs, cli_options.as_configuration_path_hint())?;
Expand Down
14 changes: 13 additions & 1 deletion crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use biome_service::configuration::{
use biome_service::workspace::{RegisterProjectFolderParams, UpdateSettingsParams};
use std::ffi::OsString;

use super::check_fix_incompatible_arguments;

pub(crate) struct FormatCommandPayload {
pub(crate) javascript_formatter: Option<PartialJavascriptFormatter>,
pub(crate) json_formatter: Option<PartialJsonFormatter>,
Expand All @@ -29,6 +31,7 @@ pub(crate) struct FormatCommandPayload {
pub(crate) files_configuration: Option<PartialFilesConfiguration>,
pub(crate) stdin_file_path: Option<String>,
pub(crate) write: bool,
pub(crate) fix: bool,
pub(crate) cli_options: CliOptions,
pub(crate) paths: Vec<OsString>,
pub(crate) staged: bool,
Expand All @@ -50,6 +53,7 @@ pub(crate) fn format(
stdin_file_path,
files_configuration,
write,
fix,
mut json_formatter,
css_formatter,
since,
Expand All @@ -58,6 +62,14 @@ pub(crate) fn format(
} = payload;
setup_cli_subscriber(cli_options.log_level, cli_options.log_kind);

check_fix_incompatible_arguments(super::FixFileModeOptions {
apply: false,
apply_unsafe: false,
write,
fix,
unsafe_: false,
})?;

let loaded_configuration =
load_configuration(&session.app.fs, cli_options.as_configuration_path_hint())?;
validate_configuration_diagnostics(
Expand Down Expand Up @@ -195,7 +207,7 @@ pub(crate) fn format(

let execution = Execution::new(TraversalMode::Format {
ignore_errors: cli_options.skip_errors,
write,
write: write || fix,
stdin,
})
.set_report(&cli_options);
Expand Down
29 changes: 16 additions & 13 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ use biome_deserialize::Merge;
use biome_service::configuration::{
load_configuration, LoadedConfiguration, PartialConfigurationExt,
};
use biome_service::workspace::{FixFileMode, RegisterProjectFolderParams, UpdateSettingsParams};
use biome_service::workspace::{RegisterProjectFolderParams, UpdateSettingsParams};
use std::ffi::OsString;

use super::{determine_fix_file_mode, FixFileModeOptions};

pub(crate) struct LintCommandPayload {
pub(crate) apply: bool,
pub(crate) apply_unsafe: bool,
pub(crate) write: bool,
pub(crate) fix: bool,
pub(crate) unsafe_: bool,
pub(crate) cli_options: CliOptions,
pub(crate) linter_configuration: Option<PartialLinterConfiguration>,
pub(crate) vcs_configuration: Option<PartialVcsConfiguration>,
Expand All @@ -43,6 +48,9 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
let LintCommandPayload {
apply,
apply_unsafe,
write,
fix,
unsafe_,
cli_options,
mut linter_configuration,
mut paths,
Expand All @@ -59,18 +67,13 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
} = payload;
setup_cli_subscriber(cli_options.log_level, cli_options.log_kind);

let fix_file_mode = if apply && apply_unsafe {
return Err(CliDiagnostic::incompatible_arguments(
"--apply",
"--apply-unsafe",
));
} else if !apply && !apply_unsafe {
None
} else if apply && !apply_unsafe {
Some(FixFileMode::SafeFixes)
} else {
Some(FixFileMode::SafeAndUnsafeFixes)
};
let fix_file_mode = determine_fix_file_mode(FixFileModeOptions {
apply,
apply_unsafe,
write,
fix,
unsafe_,
})?;

let loaded_configuration =
load_configuration(&session.app.fs, cli_options.as_configuration_path_hint())?;
Expand Down
13 changes: 11 additions & 2 deletions crates/biome_cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use biome_console::{markup, ConsoleExt};
use biome_service::configuration::{load_configuration, LoadedConfiguration};
use biome_service::workspace::RegisterProjectFolderParams;

use super::MigrateSubCommand;
use super::{check_fix_incompatible_arguments, FixFileModeOptions, MigrateSubCommand};

/// Handler for the "check" command of the Biome CLI
pub(crate) fn migrate(
session: CliSession,
cli_options: CliOptions,
write: bool,
fix: bool,
sub_command: Option<MigrateSubCommand>,
) -> Result<(), CliDiagnostic> {
let base_path = cli_options.as_configuration_path_hint();
Expand All @@ -24,6 +25,14 @@ pub(crate) fn migrate(
} = load_configuration(&session.app.fs, base_path)?;
setup_cli_subscriber(cli_options.log_level, cli_options.log_kind);

check_fix_incompatible_arguments(FixFileModeOptions {
apply: false,
apply_unsafe: false,
write,
fix,
unsafe_: false,
})?;

session
.app
.workspace
Expand All @@ -35,7 +44,7 @@ pub(crate) fn migrate(
if let (Some(path), Some(directory_path)) = (file_path, directory_path) {
execute_mode(
Execution::new(TraversalMode::Migrate {
write,
write: write || fix,
configuration_file_path: path,
configuration_directory_path: directory_path,
sub_command,
Expand Down
Loading