From a0e859b582e5a3a4a82cc94d7e337f029ec293ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Correa=20Casablanca?= Date: Thu, 11 Apr 2024 18:39:53 +0200 Subject: [PATCH] refactor: extract function Signed-off-by: Andres Correa Casablanca --- crates/biome_cli/src/commands/check.rs | 24 ++++++------------ crates/biome_cli/src/commands/format.rs | 25 ++++++------------- crates/biome_cli/src/commands/lint.rs | 25 ++++++------------- crates/biome_cli/src/commands/mod.rs | 33 +++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 55 deletions(-) diff --git a/crates/biome_cli/src/commands/check.rs b/crates/biome_cli/src/commands/check.rs index ee964e778f94..5e79bdc2124d 100644 --- a/crates/biome_cli/src/commands/check.rs +++ b/crates/biome_cli/src/commands/check.rs @@ -1,6 +1,7 @@ -use crate::changed::{get_changed_files, get_staged_files}; use crate::cli_options::CliOptions; -use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics}; +use crate::commands::{ + get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics, +}; use crate::{ execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode, }; @@ -122,23 +123,12 @@ pub(crate) fn check( let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "check")?; - if since.is_some() { - if !changed { - return Err(CliDiagnostic::incompatible_arguments("since", "changed")); - } - if staged { - return Err(CliDiagnostic::incompatible_arguments("since", "staged")); - } + if let Some(_paths) = + get_files_to_process(since, changed, staged, &session.app.fs, &fs_configuration)? + { + paths = _paths; } - if changed { - if staged { - return Err(CliDiagnostic::incompatible_arguments("changed", "staged")); - } - paths = get_changed_files(&session.app.fs, &fs_configuration, since)?; - } else if staged { - paths = get_staged_files(&session.app.fs)?; - } session .app .workspace diff --git a/crates/biome_cli/src/commands/format.rs b/crates/biome_cli/src/commands/format.rs index ef660b91ffca..e8fa25965a28 100644 --- a/crates/biome_cli/src/commands/format.rs +++ b/crates/biome_cli/src/commands/format.rs @@ -1,6 +1,7 @@ -use crate::changed::{get_changed_files, get_staged_files}; use crate::cli_options::CliOptions; -use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics}; +use crate::commands::{ + get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics, +}; use crate::diagnostics::DeprecatedArgument; use crate::execute::ReportMode; use crate::{ @@ -159,22 +160,10 @@ pub(crate) fn format( let (vcs_base_path, gitignore_matches) = configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?; - if since.is_some() { - if !changed { - return Err(CliDiagnostic::incompatible_arguments("since", "changed")); - } - if staged { - return Err(CliDiagnostic::incompatible_arguments("since", "staged")); - } - } - - if changed { - if staged { - return Err(CliDiagnostic::incompatible_arguments("changed", "staged")); - } - paths = get_changed_files(&session.app.fs, &configuration, since)?; - } else if staged { - paths = get_staged_files(&session.app.fs)?; + if let Some(_paths) = + get_files_to_process(since, changed, staged, &session.app.fs, &configuration)? + { + paths = _paths; } session diff --git a/crates/biome_cli/src/commands/lint.rs b/crates/biome_cli/src/commands/lint.rs index b4c3ebdae64b..ab0514b2b657 100644 --- a/crates/biome_cli/src/commands/lint.rs +++ b/crates/biome_cli/src/commands/lint.rs @@ -1,6 +1,7 @@ -use crate::changed::{get_changed_files, get_staged_files}; use crate::cli_options::CliOptions; -use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics}; +use crate::commands::{ + get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics, +}; use crate::{ execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode, }; @@ -97,22 +98,10 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<( let (vcs_base_path, gitignore_matches) = fs_configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?; - if since.is_some() { - if !changed { - return Err(CliDiagnostic::incompatible_arguments("since", "changed")); - } - if staged { - return Err(CliDiagnostic::incompatible_arguments("since", "staged")); - } - } - - if changed { - if staged { - return Err(CliDiagnostic::incompatible_arguments("changed", "staged")); - } - paths = get_changed_files(&session.app.fs, &fs_configuration, since)?; - } else if staged { - paths = get_staged_files(&session.app.fs)?; + if let Some(_paths) = + get_files_to_process(since, changed, staged, &session.app.fs, &fs_configuration)? + { + paths = _paths; } let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "lint")?; diff --git a/crates/biome_cli/src/commands/mod.rs b/crates/biome_cli/src/commands/mod.rs index 06437b84b208..6625f6b87220 100644 --- a/crates/biome_cli/src/commands/mod.rs +++ b/crates/biome_cli/src/commands/mod.rs @@ -1,3 +1,4 @@ +use crate::changed::{get_changed_files, get_staged_files}; use crate::cli_options::{cli_options, CliOptions, ColorsArg}; use crate::diagnostics::DeprecatedConfigurationFile; use crate::execute::Stdin; @@ -14,11 +15,11 @@ use biome_configuration::{ use biome_configuration::{ConfigurationDiagnostic, PartialConfiguration}; use biome_console::{markup, Console, ConsoleExt}; use biome_diagnostics::{Diagnostic, PrintDiagnostic}; -use biome_fs::BiomePath; +use biome_fs::{BiomePath, FileSystem}; use biome_service::configuration::LoadedConfiguration; use biome_service::documentation::Doc; use biome_service::workspace::{OpenProjectParams, UpdateProjectParams}; -use biome_service::WorkspaceError; +use biome_service::{DynRef, WorkspaceError}; use bpaf::Bpaf; use std::ffi::OsString; use std::path::PathBuf; @@ -524,6 +525,34 @@ pub(crate) fn get_stdin( Ok(stdin) } +fn get_files_to_process( + since: Option, + changed: bool, + staged: bool, + fs: &DynRef<'_, dyn FileSystem>, + configuration: &PartialConfiguration, +) -> Result>, CliDiagnostic> { + if since.is_some() { + if !changed { + return Err(CliDiagnostic::incompatible_arguments("since", "changed")); + } + if staged { + return Err(CliDiagnostic::incompatible_arguments("since", "staged")); + } + } + + if changed { + if staged { + return Err(CliDiagnostic::incompatible_arguments("changed", "staged")); + } + Ok(Some(get_changed_files(fs, configuration, since)?)) + } else if staged { + Ok(Some(get_staged_files(fs)?)) + } else { + Ok(None) + } +} + /// Tests that all CLI options adhere to the invariants expected by `bpaf`. #[test] fn check_options() {