Move fix::FixMode to flags::FixMode (#3753)

This commit is contained in:
Charlie Marsh 2023-03-26 17:40:06 -04:00 committed by GitHub
parent cd75b57036
commit 6ed6da3e82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 66 deletions

View file

@ -12,11 +12,11 @@ use log::{debug, error};
use rustc_hash::FxHashMap;
use similar::TextDiff;
use ruff::fs;
use ruff::jupyter::{is_jupyter_notebook, JupyterIndex, JupyterNotebook};
use ruff::linter::{lint_fix, lint_only, FixTable, LinterResult};
use ruff::message::Message;
use ruff::settings::{flags, AllSettings, Settings};
use ruff::{fix, fs};
use crate::cache;
@ -100,7 +100,7 @@ pub fn lint_path(
settings: &AllSettings,
cache: flags::Cache,
noqa: flags::Noqa,
autofix: fix::FixMode,
autofix: flags::FixMode,
) -> Result<Diagnostics> {
// Check the cache.
// TODO(charlie): `fixer::Mode::Apply` and `fixer::Mode::Diff` both have
@ -110,7 +110,7 @@ pub fn lint_path(
// to reason about. We need to come up with a better solution here.)
let metadata = if cache.into()
&& noqa.into()
&& matches!(autofix, fix::FixMode::None | fix::FixMode::Generate)
&& matches!(autofix, flags::FixMode::None | flags::FixMode::Generate)
{
let metadata = path.metadata()?;
if let Some(messages) =
@ -143,14 +143,14 @@ pub fn lint_path(
error: parse_error,
},
fixed,
) = if matches!(autofix, fix::FixMode::Apply | fix::FixMode::Diff) {
) = if matches!(autofix, flags::FixMode::Apply | flags::FixMode::Diff) {
if let Ok((result, transformed, fixed)) =
lint_fix(&contents, path, package, noqa, &settings.lib)
{
if !fixed.is_empty() {
if matches!(autofix, fix::FixMode::Apply) {
if matches!(autofix, flags::FixMode::Apply) {
write(path, transformed.as_bytes())?;
} else if matches!(autofix, fix::FixMode::Diff) {
} else if matches!(autofix, flags::FixMode::Diff) {
let mut stdout = io::stdout().lock();
TextDiff::from_lines(contents.as_str(), &transformed)
.unified_diff()
@ -243,7 +243,7 @@ pub fn lint_stdin(
contents: &str,
settings: &Settings,
noqa: flags::Noqa,
autofix: fix::FixMode,
autofix: flags::FixMode,
) -> Result<Diagnostics> {
// Lint the inputs.
let (
@ -252,7 +252,7 @@ pub fn lint_stdin(
error: parse_error,
},
fixed,
) = if matches!(autofix, fix::FixMode::Apply | fix::FixMode::Diff) {
) = if matches!(autofix, flags::FixMode::Apply | flags::FixMode::Diff) {
if let Ok((result, transformed, fixed)) = lint_fix(
contents,
path.unwrap_or_else(|| Path::new("-")),
@ -260,10 +260,10 @@ pub fn lint_stdin(
noqa,
settings,
) {
if matches!(autofix, fix::FixMode::Apply) {
if matches!(autofix, flags::FixMode::Apply) {
// Write the contents to stdout, regardless of whether any errors were fixed.
io::stdout().write_all(transformed.as_bytes())?;
} else if matches!(autofix, fix::FixMode::Diff) {
} else if matches!(autofix, flags::FixMode::Diff) {
// But only write a diff if it's non-empty.
if !fixed.is_empty() {
let text_diff = TextDiff::from_lines(contents, &transformed);
@ -293,7 +293,7 @@ pub fn lint_stdin(
let fixed = FxHashMap::default();
// Write the contents to stdout anyway.
if matches!(autofix, fix::FixMode::Apply) {
if matches!(autofix, flags::FixMode::Apply) {
io::stdout().write_all(contents.as_bytes())?;
}