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 14, 2023
1 parent c7a753a commit 5d8864d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 24 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
3 changes: 2 additions & 1 deletion crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ pub fn lint_fix<'a>(
settings: &Settings,
source_kind: &mut SourceKind,
source_type: PySourceType,
fix_suggested: flags::SuggestedFixes,
) -> Result<FixerResult<'a>> {
let mut transformed = Cow::Borrowed(contents);

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions crates/ruff/src/settings/flags.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
7 changes: 5 additions & 2 deletions crates/ruff/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_cli/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -439,7 +439,7 @@ mod tests {
&settings,
Some(&cache),
flags::Noqa::Enabled,
flags::FixMode::Generate(true),
flags::FixMode::Generate(flags::SuggestedFixes::Apply),
)
.unwrap();
}
Expand Down Expand Up @@ -701,7 +701,7 @@ mod tests {
&self.settings,
Some(cache),
flags::Noqa::Enabled,
flags::FixMode::Generate(true),
flags::FixMode::Generate(flags::SuggestedFixes::Apply),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -254,6 +254,7 @@ pub(crate) fn lint_path(
&settings.lib,
&mut source_kind,
source_type,
fix_suggested,
) {
if !fixed.is_empty() {
match autofix {
Expand Down Expand Up @@ -431,6 +432,7 @@ pub(crate) fn lint_stdin(
settings,
&mut source_kind,
source_type,
fix_suggested,
) {
match autofix {
flags::FixMode::Apply(_) => {
Expand Down
17 changes: 12 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 @@ -240,13 +241,19 @@ 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 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;
Expand Down Expand Up @@ -383,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: 7 additions & 6 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| {
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
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,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 {
Expand Down

0 comments on commit 5d8864d

Please sign in to comment.