Skip to content

Commit

Permalink
Waiting for Zanie's CLI changes
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Aug 6, 2023
1 parent 47a8a93 commit 02a84a6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
6 changes: 4 additions & 2 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,7 @@ pub(crate) struct FixResult {
pub(crate) fn fix_file(
diagnostics: &[Diagnostic],
locator: &Locator,
fix_unsafe: bool,
fix_suggested: SuggestedFixes,
) -> Option<FixResult> {
let mut with_fixes = diagnostics
.iter()
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ pub fn lint_fix<'a>(
noqa: flags::Noqa,
settings: &Settings,
source_kind: &mut SourceKind,
fix_unsafe: bool,
fix_suggested: flags::SuggestedFixes,
) -> Result<FixerResult<'a>> {
let mut transformed = Cow::Borrowed(contents);

Expand Down Expand Up @@ -476,7 +476,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.
Expand Down
15 changes: 12 additions & 3 deletions crates/ruff/src/settings/flags.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
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)]
Expand Down
12 changes: 7 additions & 5 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -245,13 +246,14 @@ pub fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
// [`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 autofix = if cli.diff {
flags::FixMode::Diff(fix_unsafe)
FixMode::Diff(fix_unsafe)
} else if fix || fix_only {
flags::FixMode::Apply(fix_unsafe)
FixMode::Apply(fix_unsafe)
} 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;
Expand Down Expand Up @@ -388,7 +390,7 @@ pub fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
// 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 {
Expand Down
13 changes: 8 additions & 5 deletions crates/ruff_cli/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
for message in diagnostics.messages {
if let Some(fix) = &msg.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
Expand All @@ -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 {
Expand Down Expand Up @@ -489,7 +489,10 @@ 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 {
Expand Down

0 comments on commit 02a84a6

Please sign in to comment.