refactor: Convention::codes to rules_to_be_ignored

This commit is contained in:
Martin Fischer
2023-01-24 09:21:47 +01:00
committed by Charlie Marsh
parent c40f14620a
commit ca58c72fc9
3 changed files with 57 additions and 62 deletions

View File

@@ -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,
],
}
}

View File

@@ -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
}
}

View File

@@ -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<I: IntoIterator<Item = Rule>> From<I> for RuleTable {