Skip to content

Commit

Permalink
Add plugin configuration to flake8-to-ruff (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 1, 2022
1 parent 2d83f99 commit bad5723
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
47 changes: 47 additions & 0 deletions crates/flake8_to_ruff/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}"),
Expand Down Expand Up @@ -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))
}
2 changes: 2 additions & 0 deletions scripts/.flake8
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ exclude =
test_fstring.py,
bad_coding2.py,
badsyntax_*.py
ignore_names = foo, bar
quotes = double
2 changes: 1 addition & 1 deletion src/check_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/flake8_quotes/docstring_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/flake8_quotes/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/pep8_naming/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down

0 comments on commit bad5723

Please sign in to comment.