Skip to content

Commit

Permalink
Add explicit-preview-rules to toggle explicit selection of preview …
Browse files Browse the repository at this point in the history
…rules
  • Loading branch information
zanieb committed Sep 27, 2023
1 parent 70ab4b8 commit f04b7b0
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 38 deletions.
4 changes: 2 additions & 2 deletions crates/flake8_to_ruff/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use anyhow::anyhow;
use ruff_linter::registry::Linter;
use ruff_linter::settings::types::PreviewMode;
use ruff_linter::rule_selector::PreviewOptions;
use ruff_linter::RuleSelector;

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
Expand Down Expand Up @@ -332,7 +332,7 @@ pub(crate) fn infer_plugins_from_codes(selectors: &HashSet<RuleSelector>) -> Vec
.filter(|plugin| {
for selector in selectors {
if selector
.rules(PreviewMode::Disabled)
.rules(&PreviewOptions::default())
.any(|rule| Linter::from(plugin).rules().any(|r| r == rule))
{
return true;
Expand Down
19 changes: 15 additions & 4 deletions crates/ruff_linter/src/rule_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,19 @@ impl RuleSelector {
}
}

/// Returns rules matching the selector, taking into account whether preview mode is enabled.
pub fn rules(&self, preview: PreviewMode) -> impl Iterator<Item = Rule> + '_ {
/// Returns rules matching the selector, taking into account preview options enabled.
pub fn rules<'a>(&'a self, preview: &PreviewOptions) -> impl Iterator<Item = Rule> + 'a {
let preview_enabled = preview.mode.is_enabled();
let preview_require_explicit = preview.require_explicit;
#[allow(deprecated)]
self.all_rules().filter(move |rule| {
// Always include rules that are not in preview or the nursery
!(rule.is_preview() || rule.is_nursery())
// Backwards compatibility allows selection of nursery rules by exact code or dedicated group
|| ((matches!(self, RuleSelector::Rule { .. }) || matches!(self, RuleSelector::Nursery { .. })) && rule.is_nursery())
// Enabling preview includes all preview or nursery rules
|| preview.is_enabled()
// Enabling preview includes all preview or nursery rules unless explicit selection
// is turned on
|| (preview_enabled && (matches!(self, RuleSelector::Rule { .. }) || !preview_require_explicit))
})
}
}
Expand All @@ -232,6 +235,14 @@ impl Iterator for RuleSelectorIter {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PreviewOptions {
pub mode: PreviewMode,
/// If true, preview rule selection requires explicit codes e.g. not prefixes.
/// Generally this should be derived from the user-facing `explicit-preview-rules` option.
pub require_explicit: bool,
}

#[cfg(feature = "schemars")]
mod schema {
use itertools::Itertools;
Expand Down
5 changes: 4 additions & 1 deletion crates/ruff_linter/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::line_width::{LineLength, TabSize};

use self::rule_table::RuleTable;
use self::types::PreviewMode;
use crate::rule_selector::PreviewOptions;

pub mod flags;
pub mod rule_table;
Expand All @@ -44,6 +45,7 @@ pub struct LinterSettings {

pub target_version: PythonVersion,
pub preview: PreviewMode,
pub explicit_preview_rules: bool,

// Rule-specific settings
pub allowed_confusables: FxHashSet<char>,
Expand Down Expand Up @@ -121,7 +123,7 @@ impl LinterSettings {
project_root: project_root.to_path_buf(),
rules: PREFIXES
.iter()
.flat_map(|selector| selector.rules(PreviewMode::default()))
.flat_map(|selector| selector.rules(&PreviewOptions::default()))
.collect(),
allowed_confusables: FxHashSet::from_iter([]),

Expand Down Expand Up @@ -168,6 +170,7 @@ impl LinterSettings {
pylint: pylint::settings::Settings::default(),
pyupgrade: pyupgrade::settings::Settings::default(),
preview: PreviewMode::default(),
explicit_preview_rules: false,
}
}

Expand Down
Loading

0 comments on commit f04b7b0

Please sign in to comment.