Add plugin configuration to flake8-to-ruff (#535)

This commit is contained in:
Charlie Marsh 2022-11-01 17:08:53 -04:00 committed by GitHub
parent 2d83f99dbf
commit bad5723d80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 5 deletions

View file

@ -2,6 +2,9 @@ use std::collections::HashMap;
use anyhow::Result;
use ruff::flake8_quotes;
use ruff::flake8_quotes::settings::Quote;
use ruff::pep8_naming;
use ruff::settings::options::Options;
use ruff::settings::pyproject::Pyproject;
@ -15,9 +18,12 @@ pub fn convert(config: HashMap<String, HashMap<String, Option<String>>>) -> Resu
// Parse each supported option.
let mut options: Options = Default::default();
let mut flake8_quotes: flake8_quotes::settings::Options = Default::default();
let mut pep8_naming: pep8_naming::settings::Options = Default::default();
for (key, value) in flake8 {
if let Some(value) = value {
match key.as_str() {
// flake8
"line-length" | "line_length" => match value.clone().parse::<usize>() {
Ok(line_length) => options.line_length = Some(line_length),
Err(e) => eprintln!("Unable to parse '{key}' property: {e}"),
@ -49,11 +55,52 @@ pub fn convert(config: HashMap<String, HashMap<String, Option<String>>>) -> Resu
Err(e) => eprintln!("Unable to parse '{key}' property: {e}"),
}
}
// flake8-quotes
"quotes" | "inline-quotes" | "inline_quotes" => match value.trim() {
"'" | "single" => flake8_quotes.inline_quotes = Some(Quote::Single),
"\"" | "double" => flake8_quotes.inline_quotes = Some(Quote::Single),
_ => eprintln!("Unexpected '{key}' value: {value}"),
},
"multiline-quotes" | "multiline_quotes" => match value.trim() {
"'" | "single" => flake8_quotes.multiline_quotes = Some(Quote::Single),
"\"" | "double" => flake8_quotes.multiline_quotes = Some(Quote::Single),
_ => eprintln!("Unexpected '{key}' value: {value}"),
},
"docstring-quotes" | "docstring_quotes" => match value.trim() {
"'" | "single" => flake8_quotes.docstring_quotes = Some(Quote::Single),
"\"" | "double" => flake8_quotes.docstring_quotes = Some(Quote::Single),
_ => eprintln!("Unexpected '{key}' value: {value}"),
},
"avoid-escape" | "avoid_escape" => match value.trim() {
"true" => flake8_quotes.avoid_escape = Some(true),
"false" => flake8_quotes.avoid_escape = Some(false),
_ => eprintln!("Unexpected '{key}' value: {value}"),
},
// pep8-naming
"ignore-names" | "ignore_names" => {
pep8_naming.ignore_names = Some(parser::parse_strings(value.as_ref()));
}
"classmethod-decorators" | "classmethod_decorators" => {
pep8_naming.classmethod_decorators =
Some(parser::parse_strings(value.as_ref()));
}
"staticmethod-decorators" | "staticmethod_decorators" => {
pep8_naming.staticmethod_decorators =
Some(parser::parse_strings(value.as_ref()));
}
// Unknown
_ => eprintln!("Skipping unsupported property: {key}"),
}
}
}
if flake8_quotes != Default::default() {
options.flake8_quotes = Some(flake8_quotes);
}
if pep8_naming != Default::default() {
options.pep8_naming = Some(pep8_naming);
}
// Create the pyproject.toml.
Ok(Pyproject::new(options))
}

View file

@ -17,3 +17,5 @@ exclude =
test_fstring.py,
bad_coding2.py,
badsyntax_*.py
ignore_names = foo, bar
quotes = double

View file

@ -19,7 +19,7 @@ pub fn check_tokens(
| settings.enabled.contains(&CheckCode::Q002)
| settings.enabled.contains(&CheckCode::Q003);
let mut state_machine = StateMachine::new();
let mut state_machine: StateMachine = Default::default();
for (start, tok, end) in tokens.iter().flatten() {
// W605
if enforce_invalid_escape_sequence {

View file

@ -27,6 +27,12 @@ pub struct StateMachine {
bracket_count: usize,
}
impl Default for StateMachine {
fn default() -> Self {
Self::new()
}
}
impl StateMachine {
pub fn new() -> Self {
Self {

View file

@ -9,7 +9,7 @@ pub enum Quote {
Double,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct Options {
pub inline_quotes: Option<Quote>,

View file

@ -30,13 +30,13 @@ mod flake8_bugbear;
mod flake8_builtins;
mod flake8_comprehensions;
mod flake8_print;
mod flake8_quotes;
pub mod flake8_quotes;
pub mod fs;
pub mod linter;
pub mod logging;
pub mod message;
mod noqa;
mod pep8_naming;
pub mod pep8_naming;
pub mod printer;
mod pycodestyle;
mod pydocstyle;

View file

@ -21,7 +21,7 @@ const CLASSMETHOD_DECORATORS: [&str; 1] = ["classmethod"];
const STATICMETHOD_DECORATORS: [&str; 1] = ["staticmethod"];
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct Options {
pub ignore_names: Option<Vec<String>>,