From 5d8864d6d73abe0418dd26256a76e5bdb16ba350 Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Sat, 5 Aug 2023 20:09:30 -0500 Subject: [PATCH] Waiting for Zanie's CLI changes --- crates/ruff/src/autofix/mod.rs | 6 ++++-- crates/ruff/src/linter.rs | 3 ++- crates/ruff/src/settings/flags.rs | 14 +++++++++++--- crates/ruff/src/test.rs | 7 +++++-- crates/ruff_cli/src/cache.rs | 6 +++--- crates/ruff_cli/src/commands/run.rs | 2 +- crates/ruff_cli/src/diagnostics.rs | 4 +++- crates/ruff_cli/src/lib.rs | 17 ++++++++++++----- crates/ruff_cli/src/printer.rs | 13 +++++++------ 9 files changed, 48 insertions(+), 24 deletions(-) diff --git a/crates/ruff/src/autofix/mod.rs b/crates/ruff/src/autofix/mod.rs index 46f616c72f8c7c..490e2a6370f837 100644 --- a/crates/ruff/src/autofix/mod.rs +++ b/crates/ruff/src/autofix/mod.rs @@ -10,6 +10,7 @@ use ruff_source_file::Locator; use crate::autofix::source_map::SourceMap; use crate::linter::FixTable; use crate::registry::{AsRule, Rule}; +use crate::settings::flags::SuggestedFixes; pub(crate) mod codemods; pub(crate) mod edits; @@ -28,7 +29,7 @@ pub(crate) struct FixResult { pub(crate) fn fix_file( diagnostics: &[Diagnostic], locator: &Locator, - fix_unsafe: bool, + fix_suggested: SuggestedFixes, ) -> Option { let mut with_fixes = diagnostics .iter() @@ -37,7 +38,8 @@ pub(crate) fn fix_file( return false; }; - if fix_unsafe { + // change this + if matches!(fix_suggested, SuggestedFixes::Apply) { fix.applicability() >= Applicability::Suggested } else { fix.applicability() == Applicability::Automatic diff --git a/crates/ruff/src/linter.rs b/crates/ruff/src/linter.rs index 940d0087d81ea8..fa8b9e0b517b98 100644 --- a/crates/ruff/src/linter.rs +++ b/crates/ruff/src/linter.rs @@ -423,6 +423,7 @@ pub fn lint_fix<'a>( settings: &Settings, source_kind: &mut SourceKind, source_type: PySourceType, + fix_suggested: flags::SuggestedFixes, ) -> Result> { let mut transformed = Cow::Borrowed(contents); @@ -495,7 +496,7 @@ pub fn lint_fix<'a>( code: fixed_contents, fixes: applied, source_map, - }) = fix_file(&result.data.0, &locator, fix_unsafe) + }) = fix_file(&result.data.0, &locator, fix_suggested) { if iterations < MAX_ITERATIONS { // Count the number of fixed errors. diff --git a/crates/ruff/src/settings/flags.rs b/crates/ruff/src/settings/flags.rs index f7ae00e81d94a6..936c96bebd0663 100644 --- a/crates/ruff/src/settings/flags.rs +++ b/crates/ruff/src/settings/flags.rs @@ -1,8 +1,16 @@ +use ruff_diagnostics::Applicability; + #[derive(Debug, Copy, Clone, Hash, is_macro::Is)] pub enum FixMode { - Generate(bool), - Apply(bool), - Diff(bool), + Generate(SuggestedFixes), + Apply(SuggestedFixes), + Diff(SuggestedFixes), +} + +#[derive(Debug, Copy, Clone, Hash, is_macro::Is)] +pub enum SuggestedFixes { + Apply, + Disable, } #[derive(Debug, Copy, Clone, Hash, result_like::BoolLike)] diff --git a/crates/ruff/src/test.rs b/crates/ruff/src/test.rs index 4476da206b88e2..62bbcdf1208407 100644 --- a/crates/ruff/src/test.rs +++ b/crates/ruff/src/test.rs @@ -149,8 +149,11 @@ fn test_contents(source_kind: &mut SourceKind, path: &Path, settings: &Settings) code: fixed_contents, source_map, .. - }) = fix_file(&diagnostics, &Locator::new(&contents), true) - { + }) = fix_file( + &diagnostics, + &Locator::new(&contents), + flags::SuggestedFixes::Apply, + ) { if iterations < max_iterations() { iterations += 1; } else { diff --git a/crates/ruff_cli/src/cache.rs b/crates/ruff_cli/src/cache.rs index bc0f89dee96761..1cc2da5563efa4 100644 --- a/crates/ruff_cli/src/cache.rs +++ b/crates/ruff_cli/src/cache.rs @@ -394,7 +394,7 @@ mod tests { &settings, Some(&cache), flags::Noqa::Enabled, - flags::FixMode::Generate(true), + flags::FixMode::Generate(flags::SuggestedFixes::Apply), ) .unwrap(); if diagnostics @@ -439,7 +439,7 @@ mod tests { &settings, Some(&cache), flags::Noqa::Enabled, - flags::FixMode::Generate(true), + flags::FixMode::Generate(flags::SuggestedFixes::Apply), ) .unwrap(); } @@ -701,7 +701,7 @@ mod tests { &self.settings, Some(cache), flags::Noqa::Enabled, - flags::FixMode::Generate(true), + flags::FixMode::Generate(flags::SuggestedFixes::Apply), ) } } diff --git a/crates/ruff_cli/src/commands/run.rs b/crates/ruff_cli/src/commands/run.rs index 396e3e44b06b2e..e6f9680524258c 100644 --- a/crates/ruff_cli/src/commands/run.rs +++ b/crates/ruff_cli/src/commands/run.rs @@ -280,7 +280,7 @@ mod test { &Overrides::default(), flags::Cache::Disabled, flags::Noqa::Disabled, - flags::FixMode::Generate(true), + flags::FixMode::Generate(flags::SuggestedFixes::Apply), ) .unwrap(); let mut output = Vec::new(); diff --git a/crates/ruff_cli/src/diagnostics.rs b/crates/ruff_cli/src/diagnostics.rs index 3a94049ed0fe8d..40cbd6df063d61 100644 --- a/crates/ruff_cli/src/diagnostics.rs +++ b/crates/ruff_cli/src/diagnostics.rs @@ -241,7 +241,7 @@ pub(crate) fn lint_path( }, fixed, ) = match autofix { - flags::FixMode::Apply(fix_unsafe) | flags::FixMode::Diff(fix_unsafe) => { + flags::FixMode::Apply(fix_suggested) | flags::FixMode::Diff(fix_suggested) => { if let Ok(FixerResult { result, transformed, @@ -254,6 +254,7 @@ pub(crate) fn lint_path( &settings.lib, &mut source_kind, source_type, + fix_suggested, ) { if !fixed.is_empty() { match autofix { @@ -431,6 +432,7 @@ pub(crate) fn lint_stdin( settings, &mut source_kind, source_type, + fix_suggested, ) { match autofix { flags::FixMode::Apply(_) => { diff --git a/crates/ruff_cli/src/lib.rs b/crates/ruff_cli/src/lib.rs index 2b9b3bac55731e..b4580cd740dbc6 100644 --- a/crates/ruff_cli/src/lib.rs +++ b/crates/ruff_cli/src/lib.rs @@ -10,8 +10,9 @@ use log::warn; use notify::{recommended_watcher, RecursiveMode, Watcher}; use ruff::logging::{set_up_logging, LogLevel}; +use ruff::settings::flags::{FixMode, SuggestedFixes}; use ruff::settings::types::SerializationFormat; -use ruff::settings::{flags, CliSettings}; +use ruff::settings::CliSettings; use ruff::{fs, warn_user_once}; use ruff_python_formatter::{format_module, PyFormatOptions}; @@ -240,13 +241,19 @@ pub fn check(args: CheckArgs, log_level: LogLevel) -> Result { // [`Applicability::Automatic`] fixes. If it is not set, the rules will only apply to // [`Applicability::Automatic`] fixes. + // TODO: can't fix this until @zanieb changes the CLI + let fix_suggested = match fix_unsafe { + true => SuggestedFixes::Apply, + false => SuggestedFixes::Disable, + }; + let autofix = if cli.diff { - flags::FixMode::Diff(fix_unsafe) + FixMode::Diff(fix_suggested) } else if fix || fix_only { - flags::FixMode::Apply(fix_unsafe) + FixMode::Apply(fix_suggested) } else { // We'll always generate all fixes, regardless of [`Applicability`], in `generate` mode - flags::FixMode::Generate(true) + FixMode::Generate(SuggestedFixes::Apply) }; let cache = !cli.no_cache; let noqa = !cli.ignore_noqa; @@ -383,7 +390,7 @@ pub fn check(args: CheckArgs, log_level: LogLevel) -> Result { // Always try to print violations (the printer itself may suppress output), // unless we're writing fixes via stdin (in which case, the transformed // source code goes to stdout). - if !(is_stdin && matches!(autofix, flags::FixMode::Apply(_) | flags::FixMode::Diff(_))) { + if !(is_stdin && matches!(autofix, FixMode::Apply(_) | FixMode::Diff(_))) { if cli.statistics { printer.write_statistics(&diagnostics, &mut writer)?; } else { diff --git a/crates/ruff_cli/src/printer.rs b/crates/ruff_cli/src/printer.rs index 013ffef835a356..583e198cf52b65 100644 --- a/crates/ruff_cli/src/printer.rs +++ b/crates/ruff_cli/src/printer.rs @@ -398,15 +398,15 @@ fn applicable_fixes( ) -> Option<(u32, u32)> { let mut safe_remaining = 0; let mut unsafe_remaining = 0; - diagnostics.messages.iter().for_each(|msg| { - if let Some(fix) = &msg.fix { + for message in diagnostics.messages.iter() { + if let Some(fix) = &message.fix { if fix.applicability() == Applicability::Suggested { unsafe_remaining += 1; } else if fix.applicability() == Applicability::Automatic { safe_remaining += 1; } } - }); + } // If we're in apply mode, calculate the status based on the applicability level of the // fixes in question. If the user passed `--fix --suggested`, we can safely say that all fixes @@ -421,8 +421,8 @@ fn applicable_fixes( // If we're NOT in apply mode, we want to only show a status if any fixes, regardless of // safety, exist. match autofix_level { - flags::FixMode::Apply(should_fix_unsafe) => { - if should_fix_unsafe { + flags::FixMode::Apply(fix_suggested) => { + if matches!(fix_suggested, flags::SuggestedFixes::Apply) { None } else { if safe_remaining > 0 || unsafe_remaining > 0 { @@ -489,7 +489,8 @@ fn display_violations(safe_remaining: u32, unsafe_remaining: u32) -> String { let mut fix_status = prefix; if safe_remaining > 0 { - fix_status = format!("{fix_status} {safe_remaining} potentially fixable with the --fix option."); + fix_status = + format!("{fix_status} {safe_remaining} potentially fixable with the --fix option."); } if unsafe_remaining > 0 {