From ca58c72fc9349547eff753acd5aa2818cadbd6b1 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 24 Jan 2023 09:21:47 +0100 Subject: [PATCH] refactor: Convention::codes to rules_to_be_ignored --- src/rules/pydocstyle/settings.rs | 83 +++++++++++++++----------------- src/settings/mod.rs | 31 +++++------- src/settings/rule_table.rs | 5 ++ 3 files changed, 57 insertions(+), 62 deletions(-) diff --git a/src/rules/pydocstyle/settings.rs b/src/rules/pydocstyle/settings.rs index 24c55d0069..9105440dd1 100644 --- a/src/rules/pydocstyle/settings.rs +++ b/src/rules/pydocstyle/settings.rs @@ -4,7 +4,7 @@ use ruff_macros::ConfigurationOptions; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use crate::registry::RuleSelector; +use crate::registry::Rule; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash, JsonSchema)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] @@ -18,55 +18,50 @@ pub enum Convention { } impl Convention { - pub fn codes(self) -> &'static [RuleSelector] { + pub fn rules_to_be_ignored(self) -> &'static [Rule] { match self { Convention::Google => &[ - // All errors except D203, D204, D213, D215, D400, D401, D404, D406, D407, D408, - // D409 and D413. - RuleSelector::D203, - RuleSelector::D204, - RuleSelector::D213, - RuleSelector::D215, - RuleSelector::D400, - RuleSelector::D404, - RuleSelector::D406, - RuleSelector::D407, - RuleSelector::D408, - RuleSelector::D409, - RuleSelector::D413, + Rule::OneBlankLineBeforeClass, + Rule::OneBlankLineAfterClass, + Rule::MultiLineSummarySecondLine, + Rule::SectionUnderlineNotOverIndented, + Rule::EndsInPeriod, + Rule::NoThisPrefix, + Rule::NewLineAfterSectionName, + Rule::DashedUnderlineAfterSection, + Rule::SectionUnderlineAfterName, + Rule::SectionUnderlineMatchesSectionLength, + Rule::BlankLineAfterLastSection, ], Convention::Numpy => &[ - // All errors except D107, D203, D212, D213, D402, D413, D415, D416, and D417. - RuleSelector::D107, - RuleSelector::D203, - RuleSelector::D212, - RuleSelector::D213, - RuleSelector::D402, - RuleSelector::D413, - RuleSelector::D415, - RuleSelector::D416, - RuleSelector::D417, + Rule::PublicInit, + Rule::OneBlankLineBeforeClass, + Rule::MultiLineSummaryFirstLine, + Rule::MultiLineSummarySecondLine, + Rule::NoSignature, + Rule::BlankLineAfterLastSection, + Rule::EndsInPunctuation, + Rule::SectionNameEndsInColon, + Rule::DocumentAllArguments, ], Convention::Pep257 => &[ - // All errors except D203, D212, D213, D214, D215, D404, D405, D406, D407, D408, - // D409, D410, D411, D413, D415, D416 and D417. - RuleSelector::D203, - RuleSelector::D212, - RuleSelector::D213, - RuleSelector::D214, - RuleSelector::D215, - RuleSelector::D404, - RuleSelector::D405, - RuleSelector::D406, - RuleSelector::D407, - RuleSelector::D408, - RuleSelector::D409, - RuleSelector::D410, - RuleSelector::D411, - RuleSelector::D413, - RuleSelector::D415, - RuleSelector::D416, - RuleSelector::D417, + Rule::OneBlankLineBeforeClass, + Rule::MultiLineSummaryFirstLine, + Rule::MultiLineSummarySecondLine, + Rule::SectionNotOverIndented, + Rule::SectionUnderlineNotOverIndented, + Rule::NoThisPrefix, + Rule::CapitalizeSectionName, + Rule::NewLineAfterSectionName, + Rule::DashedUnderlineAfterSection, + Rule::SectionUnderlineAfterName, + Rule::SectionUnderlineMatchesSectionLength, + Rule::BlankLineAfterSection, + Rule::BlankLineBeforeSection, + Rule::BlankLineAfterLastSection, + Rule::EndsInPunctuation, + Rule::SectionNameEndsInColon, + Rule::DocumentAllArguments, ], } } diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 5be43f5e92..598ebd6158 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -2,12 +2,10 @@ //! command-line options. Structure is optimized for internal usage, as opposed //! to external visibility or parsing. -use std::iter; use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; use globset::Glob; -use itertools::Either::{Left, Right}; use rustc_hash::FxHashSet; use self::hashable::{HashableGlobMatcher, HashableGlobSet, HashableHashSet, HashableRegex}; @@ -256,27 +254,24 @@ impl From<&Configuration> for RuleTable { .iter() .zip(config.extend_ignore.iter()) .map(|(select, ignore)| RuleCodeSpec { select, ignore }), - ) - .chain( - // If a docstring convention is specified, force-disable any incompatible error - // codes. - if let Some(convention) = config - .pydocstyle - .as_ref() - .and_then(|pydocstyle| pydocstyle.convention) - { - Left(iter::once(RuleCodeSpec { - select: &[], - ignore: convention.codes(), - })) - } else { - Right(iter::empty()) - }, ), )) { let fix = fixable.contains(&code); rules.enable(code, fix); } + + // If a docstring convention is specified, force-disable any incompatible error + // codes. + if let Some(convention) = config + .pydocstyle + .as_ref() + .and_then(|pydocstyle| pydocstyle.convention) + { + for rule in convention.rules_to_be_ignored() { + rules.disable(rule); + } + } + rules } } diff --git a/src/settings/rule_table.rs b/src/settings/rule_table.rs index 4d0065546f..c31ece67f1 100644 --- a/src/settings/rule_table.rs +++ b/src/settings/rule_table.rs @@ -40,6 +40,11 @@ impl RuleTable { pub fn enable(&mut self, code: Rule, should_fix: bool) { self.enabled.insert(code, should_fix); } + + /// Disables the given rule. + pub fn disable(&mut self, rule: &Rule) { + self.enabled.remove(rule); + } } impl> From for RuleTable {