Add --select and --ignore

This commit is contained in:
Charlie Marsh 2022-08-27 20:55:20 -04:00
parent 2f266e5e20
commit 9686676710
4 changed files with 43 additions and 2 deletions

View File

@ -11,6 +11,7 @@ use notify::{raw_watcher, RecursiveMode, Watcher};
use rayon::prelude::*;
use walkdir::DirEntry;
use ::ruff::checks::CheckCode;
use ::ruff::fs::iter_python_files;
use ::ruff::linter::check_path;
use ::ruff::logging::set_up_logging;
@ -39,6 +40,12 @@ struct Cli {
/// Disable cache reads.
#[clap(short, long, action)]
no_cache: bool,
/// Comma-separated list of error codes to enable.
#[clap(long)]
select: Vec<CheckCode>,
/// Comma-separated list of error codes to ignore.
#[clap(long)]
ignore: Vec<CheckCode>,
}
fn run_once(files: &[PathBuf], settings: &Settings, cache: bool) -> Result<Vec<Message>> {
@ -107,7 +114,13 @@ fn inner_main() -> Result<ExitCode> {
// TODO(charlie): Can we avoid this cast?
let paths: Vec<&Path> = cli.files.iter().map(PathBuf::as_path).collect();
let settings = Settings::from_paths(paths)?;
let mut settings = Settings::from_paths(paths)?;
if !cli.select.is_empty() {
settings.select(&cli.select);
}
if !cli.ignore.is_empty() {
settings.ignore(&cli.ignore);
}
if cli.watch {
// Perform an initial run instantly.

View File

@ -1,5 +1,7 @@
use anyhow::Result;
use rustpython_parser::ast::Location;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hash, PartialOrd, Ord)]
pub enum CheckCode {
@ -13,6 +15,24 @@ pub enum CheckCode {
F901,
}
impl FromStr for CheckCode {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"E501" => Ok(CheckCode::E501),
"F401" => Ok(CheckCode::F401),
"F403" => Ok(CheckCode::F403),
"F541" => Ok(CheckCode::F541),
"F634" => Ok(CheckCode::F634),
"F706" => Ok(CheckCode::F706),
"F831" => Ok(CheckCode::F831),
"F901" => Ok(CheckCode::F901),
_ => Err(anyhow::anyhow!("Unknown check code: {s}")),
}
}
}
impl CheckCode {
pub fn as_str(&self) -> &str {
match self {

View File

@ -1,7 +1,7 @@
mod cache;
pub mod check_ast;
mod check_lines;
mod checks;
pub mod checks;
pub mod fs;
pub mod linter;
pub mod logging;

View File

@ -54,4 +54,12 @@ impl Settings {
}),
})
}
pub fn select(&mut self, codes: &[CheckCode]) {
self.select.retain(|code| codes.contains(code));
}
pub fn ignore(&mut self, codes: &[CheckCode]) {
self.select.retain(|code| !codes.contains(code));
}
}