Skip to content

Commit

Permalink
Fix exclusion of redirected removed rules from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Feb 1, 2024
1 parent d145c23 commit 55221d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
41 changes: 37 additions & 4 deletions crates/ruff_linter/src/rule_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl FromStr for RuleSelector {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
// **Changes should be reflected in `parse_no_redirect` as well**
match s {
"ALL" => Ok(Self::All),
#[allow(deprecated)]
Expand All @@ -67,7 +68,6 @@ impl FromStr for RuleSelector {
return Ok(Self::Linter(linter));
}

// Does the selector select a single rule?
let prefix = RuleCodePrefix::parse(&linter, code)
.map_err(|_| ParseError::Unknown(s.to_string()))?;

Expand Down Expand Up @@ -254,8 +254,6 @@ pub struct PreviewOptions {

#[cfg(feature = "schemars")]
mod schema {
use std::str::FromStr;

use itertools::Itertools;
use schemars::JsonSchema;
use schemars::_serde_json::Value;
Expand Down Expand Up @@ -302,7 +300,7 @@ mod schema {
.filter(|p| {
// Exclude any prefixes where all of the rules are removed
if let Ok(Self::Rule { prefix, .. } | Self::Prefix { prefix, .. }) =
RuleSelector::from_str(p)
RuleSelector::parse_no_redirect(p)
{
!prefix.rules().all(|rule| rule.is_removed())
} else {
Expand Down Expand Up @@ -341,6 +339,41 @@ impl RuleSelector {
}
}
}

/// Parse [`RuleSelector`] from a string; but do not follow redirects.
pub fn parse_no_redirect(s: &str) -> Result<Self, ParseError> {
// **Changes should be reflected in `from_str` as well**
match s {
"ALL" => Ok(Self::All),
#[allow(deprecated)]
"NURSERY" => Ok(Self::Nursery),
"C" => Ok(Self::C),
"T" => Ok(Self::T),
_ => {
let (linter, code) =
Linter::parse_code(s).ok_or_else(|| ParseError::Unknown(s.to_string()))?;

if code.is_empty() {
return Ok(Self::Linter(linter));
}

let prefix = RuleCodePrefix::parse(&linter, code)
.map_err(|_| ParseError::Unknown(s.to_string()))?;

if is_single_rule_selector(&prefix) {
Ok(Self::Rule {
prefix,
redirected_from: None,
})
} else {
Ok(Self::Prefix {
prefix,
redirected_from: None,
})
}
}
}
}
}

#[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
Expand Down
2 changes: 0 additions & 2 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 55221d4

Please sign in to comment.