From cfa25ea4b0d16f7ce3d515c12c3c937eba36bb0b Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sat, 14 Jan 2023 19:55:31 +0100 Subject: [PATCH] Make ruff::rules private --- flake8_to_ruff/src/main.rs | 14 ++----- ruff_cli/src/cli.rs | 5 +-- .../src => src/flake8_to_ruff}/black.rs | 3 +- .../src => src/flake8_to_ruff}/converter.rs | 40 +++++++++--------- src/flake8_to_ruff/mod.rs | 8 ++++ .../src => src/flake8_to_ruff}/parser.rs | 13 +++--- .../src => src/flake8_to_ruff}/plugin.rs | 5 ++- src/lib.rs | 3 +- src/rules/flake8_pytest_style/types.rs | 1 + src/rules/mod.rs | 42 +++++++++---------- src/rules/pydocstyle/settings.rs | 1 + src/settings/configuration.rs | 6 +++ 12 files changed, 76 insertions(+), 65 deletions(-) rename {flake8_to_ruff/src => src/flake8_to_ruff}/black.rs (95%) rename {flake8_to_ruff/src => src/flake8_to_ruff}/converter.rs (97%) create mode 100644 src/flake8_to_ruff/mod.rs rename {flake8_to_ruff/src => src/flake8_to_ruff}/parser.rs (97%) rename {flake8_to_ruff/src => src/flake8_to_ruff}/plugin.rs (99%) diff --git a/flake8_to_ruff/src/main.rs b/flake8_to_ruff/src/main.rs index df23d29541..97f772eed8 100644 --- a/flake8_to_ruff/src/main.rs +++ b/flake8_to_ruff/src/main.rs @@ -13,18 +13,12 @@ )] #![forbid(unsafe_code)] -mod black; -mod converter; -mod parser; -mod plugin; - use std::path::PathBuf; use anyhow::Result; -use black::parse_black_options; use clap::Parser; use configparser::ini::Ini; -use plugin::Plugin; +use ruff::flake8_to_ruff; #[derive(Parser)] #[command( @@ -42,7 +36,7 @@ struct Cli { pyproject: Option, /// List of plugins to enable. #[arg(long, value_delimiter = ',')] - plugin: Option>, + plugin: Option>, } fn main() -> Result<()> { @@ -56,12 +50,12 @@ fn main() -> Result<()> { // Read the pyproject.toml file. let black = cli .pyproject - .map(parse_black_options) + .map(flake8_to_ruff::parse_black_options) .transpose()? .flatten(); // Create Ruff's pyproject.toml section. - let pyproject = converter::convert(&config, black.as_ref(), cli.plugin)?; + let pyproject = flake8_to_ruff::convert(&config, black.as_ref(), cli.plugin)?; println!("{}", toml_edit::easy::to_string_pretty(&pyproject)?); Ok(()) diff --git a/ruff_cli/src/cli.rs b/ruff_cli/src/cli.rs index e9ec481628..994edfcca2 100644 --- a/ruff_cli/src/cli.rs +++ b/ruff_cli/src/cli.rs @@ -6,7 +6,6 @@ use ruff::fs; use ruff::logging::LogLevel; use ruff::registry::{RuleCode, RuleCodePrefix}; use ruff::resolver::ConfigProcessor; -use ruff::rules::mccabe; use ruff::settings::types::{ FilePattern, PatternPrefixPair, PerFileIgnore, PythonVersion, SerializationFormat, }; @@ -385,9 +384,7 @@ impl ConfigProcessor for &Overrides { config.line_length = Some(*line_length); } if let Some(max_complexity) = &self.max_complexity { - config.mccabe = Some(mccabe::settings::Options { - max_complexity: Some(*max_complexity), - }); + config.set_max_complexity(Some(*max_complexity)); } if let Some(per_file_ignores) = &self.per_file_ignores { config.per_file_ignores = Some(collect_per_file_ignores(per_file_ignores.clone())); diff --git a/flake8_to_ruff/src/black.rs b/src/flake8_to_ruff/black.rs similarity index 95% rename from flake8_to_ruff/src/black.rs rename to src/flake8_to_ruff/black.rs index 595eae1323..0807607be0 100644 --- a/flake8_to_ruff/src/black.rs +++ b/src/flake8_to_ruff/black.rs @@ -3,9 +3,10 @@ use std::path::Path; use anyhow::Result; -use ruff::settings::types::PythonVersion; use serde::{Deserialize, Serialize}; +use crate::settings::types::PythonVersion; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)] pub struct Black { #[serde(alias = "line-length", alias = "line_length")] diff --git a/flake8_to_ruff/src/converter.rs b/src/flake8_to_ruff/converter.rs similarity index 97% rename from flake8_to_ruff/src/converter.rs rename to src/flake8_to_ruff/converter.rs index 2a9e23de77..0a20532a57 100644 --- a/flake8_to_ruff/src/converter.rs +++ b/src/flake8_to_ruff/converter.rs @@ -2,24 +2,24 @@ use std::collections::{BTreeSet, HashMap}; use anyhow::Result; use colored::Colorize; -use ruff::registry::RuleCodePrefix; -use ruff::rules::flake8_pytest_style::types::{ + +use super::black::Black; +use super::plugin::Plugin; +use super::{parser, plugin}; +use crate::registry::RuleCodePrefix; +use crate::rules::flake8_pytest_style::types::{ ParametrizeNameType, ParametrizeValuesRowType, ParametrizeValuesType, }; -use ruff::rules::flake8_quotes::settings::Quote; -use ruff::rules::flake8_tidy_imports::settings::Strictness; -use ruff::rules::pydocstyle::settings::Convention; -use ruff::rules::{ +use crate::rules::flake8_quotes::settings::Quote; +use crate::rules::flake8_tidy_imports::settings::Strictness; +use crate::rules::pydocstyle::settings::Convention; +use crate::rules::{ flake8_annotations, flake8_bugbear, flake8_errmsg, flake8_pytest_style, flake8_quotes, flake8_tidy_imports, mccabe, pep8_naming, pydocstyle, }; -use ruff::settings::options::Options; -use ruff::settings::pyproject::Pyproject; -use ruff::warn_user; - -use crate::black::Black; -use crate::plugin::Plugin; -use crate::{parser, plugin}; +use crate::settings::options::Options; +use crate::settings::pyproject::Pyproject; +use crate::warn_user; #[allow(clippy::unnecessary_wraps)] pub fn convert( @@ -390,14 +390,14 @@ mod tests { use std::collections::HashMap; use anyhow::Result; - use ruff::registry::RuleCodePrefix; - use ruff::rules::pydocstyle::settings::Convention; - use ruff::rules::{flake8_quotes, pydocstyle}; - use ruff::settings::options::Options; - use ruff::settings::pyproject::Pyproject; - use crate::converter::convert; - use crate::plugin::Plugin; + use super::super::plugin::Plugin; + use super::convert; + use crate::registry::RuleCodePrefix; + use crate::rules::pydocstyle::settings::Convention; + use crate::rules::{flake8_quotes, pydocstyle}; + use crate::settings::options::Options; + use crate::settings::pyproject::Pyproject; #[test] fn it_converts_empty() -> Result<()> { diff --git a/src/flake8_to_ruff/mod.rs b/src/flake8_to_ruff/mod.rs new file mode 100644 index 0000000000..eec0d5986d --- /dev/null +++ b/src/flake8_to_ruff/mod.rs @@ -0,0 +1,8 @@ +mod black; +mod converter; +mod parser; +mod plugin; + +pub use black::parse_black_options; +pub use converter::convert; +pub use plugin::Plugin; diff --git a/flake8_to_ruff/src/parser.rs b/src/flake8_to_ruff/parser.rs similarity index 97% rename from flake8_to_ruff/src/parser.rs rename to src/flake8_to_ruff/parser.rs index 39294f0563..f90fa9c8be 100644 --- a/flake8_to_ruff/src/parser.rs +++ b/src/flake8_to_ruff/parser.rs @@ -4,11 +4,12 @@ use anyhow::{bail, Result}; use colored::Colorize; use once_cell::sync::Lazy; use regex::Regex; -use ruff::registry::{RuleCodePrefix, PREFIX_REDIRECTS}; -use ruff::settings::types::PatternPrefixPair; -use ruff::warn_user; use rustc_hash::FxHashMap; +use crate::registry::{RuleCodePrefix, PREFIX_REDIRECTS}; +use crate::settings::types::PatternPrefixPair; +use crate::warn_user; + static COMMA_SEPARATED_LIST_RE: Lazy = Lazy::new(|| Regex::new(r"[,\s]").unwrap()); /// Parse a comma-separated list of `RuleCodePrefix` values (e.g., @@ -203,10 +204,10 @@ pub fn collect_per_file_ignores( #[cfg(test)] mod tests { use anyhow::Result; - use ruff::registry::RuleCodePrefix; - use ruff::settings::types::PatternPrefixPair; - use crate::parser::{parse_files_to_codes_mapping, parse_prefix_codes, parse_strings}; + use super::{parse_files_to_codes_mapping, parse_prefix_codes, parse_strings}; + use crate::registry::RuleCodePrefix; + use crate::settings::types::PatternPrefixPair; #[test] fn it_parses_prefix_codes() { diff --git a/flake8_to_ruff/src/plugin.rs b/src/flake8_to_ruff/plugin.rs similarity index 99% rename from flake8_to_ruff/src/plugin.rs rename to src/flake8_to_ruff/plugin.rs index acc1b0ab93..a0c2d0063b 100644 --- a/flake8_to_ruff/src/plugin.rs +++ b/src/flake8_to_ruff/plugin.rs @@ -3,7 +3,8 @@ use std::fmt; use std::str::FromStr; use anyhow::anyhow; -use ruff::registry::RuleCodePrefix; + +use crate::registry::RuleCodePrefix; #[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum Plugin { @@ -298,7 +299,7 @@ pub fn resolve_select(plugins: &[Plugin]) -> BTreeSet { mod tests { use std::collections::HashMap; - use crate::plugin::{infer_plugins_from_options, Plugin}; + use super::{infer_plugins_from_options, Plugin}; #[test] fn it_infers_plugins() { diff --git a/src/lib.rs b/src/lib.rs index 0c9560fec8..a890a3174c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ pub mod directives; mod doc_lines; mod docstrings; pub mod fix; +pub mod flake8_to_ruff; pub mod fs; mod lex; pub mod linter; @@ -38,7 +39,7 @@ mod noqa; mod python; pub mod registry; pub mod resolver; -pub mod rules; +mod rules; pub mod rustpython_helpers; pub mod settings; pub mod source_code; diff --git a/src/rules/flake8_pytest_style/types.rs b/src/rules/flake8_pytest_style/types.rs index fbd5026506..7ef8ec7da1 100644 --- a/src/rules/flake8_pytest_style/types.rs +++ b/src/rules/flake8_pytest_style/types.rs @@ -4,6 +4,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[allow(clippy::upper_case_acronyms)] pub enum ParametrizeNameType { #[serde(rename = "csv")] CSV, diff --git a/src/rules/mod.rs b/src/rules/mod.rs index 3c6be79253..78765cd76b 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -1,33 +1,33 @@ -pub(crate) mod eradicate; -pub(crate) mod flake8_2020; +pub mod eradicate; +pub mod flake8_2020; pub mod flake8_annotations; pub mod flake8_bandit; -pub(crate) mod flake8_blind_except; +pub mod flake8_blind_except; pub mod flake8_boolean_trap; pub mod flake8_bugbear; -pub(crate) mod flake8_builtins; -pub(crate) mod flake8_comprehensions; -pub(crate) mod flake8_datetimez; -pub(crate) mod flake8_debugger; +pub mod flake8_builtins; +pub mod flake8_comprehensions; +pub mod flake8_datetimez; +pub mod flake8_debugger; pub mod flake8_errmsg; -pub(crate) mod flake8_implicit_str_concat; -pub(crate) mod flake8_import_conventions; +pub mod flake8_implicit_str_concat; +pub mod flake8_import_conventions; pub mod flake8_pie; -pub(crate) mod flake8_print; +pub mod flake8_print; pub mod flake8_pytest_style; pub mod flake8_quotes; -pub(crate) mod flake8_return; -pub(crate) mod flake8_simplify; +pub mod flake8_return; +pub mod flake8_simplify; pub mod flake8_tidy_imports; -pub(crate) mod flake8_unused_arguments; -pub(crate) mod isort; +pub mod flake8_unused_arguments; +pub mod isort; pub mod mccabe; -pub(crate) mod pandas_vet; +pub mod pandas_vet; pub mod pep8_naming; -pub(crate) mod pycodestyle; +pub mod pycodestyle; pub mod pydocstyle; -pub(crate) mod pyflakes; -pub(crate) mod pygrep_hooks; -pub(crate) mod pylint; -pub(crate) mod pyupgrade; -pub(crate) mod ruff; +pub mod pyflakes; +pub mod pygrep_hooks; +pub mod pylint; +pub mod pyupgrade; +pub mod ruff; diff --git a/src/rules/pydocstyle/settings.rs b/src/rules/pydocstyle/settings.rs index 2b20b75b49..16292d4098 100644 --- a/src/rules/pydocstyle/settings.rs +++ b/src/rules/pydocstyle/settings.rs @@ -18,6 +18,7 @@ pub enum Convention { } impl Convention { + #[allow(clippy::trivially_copy_pass_by_ref)] pub fn codes(&self) -> &'static [RuleCodePrefix] { match self { Convention::Google => &[ diff --git a/src/settings/configuration.rs b/src/settings/configuration.rs index 01d91208f9..a544fc0c18 100644 --- a/src/settings/configuration.rs +++ b/src/settings/configuration.rs @@ -251,6 +251,12 @@ impl Configuration { pyupgrade: self.pyupgrade.or(config.pyupgrade), } } + + pub fn set_max_complexity(&mut self, max_complexity: Option) { + let mut mccabe = self.mccabe.take().unwrap_or_default(); + mccabe.max_complexity = max_complexity; + self.mccabe = Some(mccabe); + } } /// Given a list of source paths, which could include glob patterns, resolve the