Skip to content

Commit

Permalink
refactor: Rename RuleCodePrefix to RuleSelector
Browse files Browse the repository at this point in the history
More accurate since the enum also encompasses:

* ALL (which isn't a prefix at all)

* fully-qualified rule codes (which aren't prefixes unless you say
  they're a prefix to the empty string but that's not intuitive)
  • Loading branch information
not-my-profile authored and charliermarsh committed Jan 21, 2023
1 parent 7fc42f8 commit b19258a
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 276 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ recommended to only use `extend-ignore` when extending a

**Default value**: `[]`

**Type**: `Vec<RuleCodePrefix>`
**Type**: `Vec<RuleSelector>`

**Example usage**:

Expand All @@ -2005,7 +2005,7 @@ recommended to only use `extend-select` when extending a

**Default value**: `[]`

**Type**: `Vec<RuleCodePrefix>`
**Type**: `Vec<RuleSelector>`

**Example usage**:

Expand Down Expand Up @@ -2080,7 +2080,7 @@ A list of rule codes or prefixes to consider autofixable.

**Default value**: `["A", "ANN", "ARG", "B", "BLE", "C", "D", "E", "ERA", "F", "FBT", "I", "ICN", "N", "PGH", "PLC", "PLE", "PLR", "PLW", "Q", "RET", "RUF", "S", "T", "TID", "UP", "W", "YTT"]`

**Type**: `Vec<RuleCodePrefix>`
**Type**: `Vec<RuleSelector>`

**Example usage**:

Expand Down Expand Up @@ -2152,7 +2152,7 @@ specific prefixes.

**Default value**: `[]`

**Type**: `Vec<RuleCodePrefix>`
**Type**: `Vec<RuleSelector>`

**Example usage**:

Expand Down Expand Up @@ -2230,7 +2230,7 @@ exclude, when considering any matching files.

**Default value**: `{}`

**Type**: `HashMap<String, Vec<RuleCodePrefix>>`
**Type**: `HashMap<String, Vec<RuleSelector>>`

**Example usage**:

Expand Down Expand Up @@ -2294,7 +2294,7 @@ specific prefixes.

**Default value**: `["E", "F"]`

**Type**: `Vec<RuleCodePrefix>`
**Type**: `Vec<RuleSelector>`

**Example usage**:

Expand Down Expand Up @@ -2438,7 +2438,7 @@ A list of rule codes or prefixes to consider non-autofix-able.

**Default value**: `[]`

**Type**: `Vec<RuleCodePrefix>`
**Type**: `Vec<RuleSelector>`

**Example usage**:

Expand Down
16 changes: 8 additions & 8 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"null"
],
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
},
"extend-select": {
Expand All @@ -83,7 +83,7 @@
"null"
],
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
},
"external": {
Expand Down Expand Up @@ -117,7 +117,7 @@
"null"
],
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
},
"flake8-annotations": {
Expand Down Expand Up @@ -244,7 +244,7 @@
"null"
],
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
},
"ignore-init-module-imports": {
Expand Down Expand Up @@ -315,7 +315,7 @@
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
}
},
Expand Down Expand Up @@ -388,7 +388,7 @@
"null"
],
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
},
"show-source": {
Expand Down Expand Up @@ -446,7 +446,7 @@
"null"
],
"items": {
"$ref": "#/definitions/RuleCodePrefix"
"$ref": "#/definitions/RuleSelector"
}
},
"update-check": {
Expand Down Expand Up @@ -1134,7 +1134,7 @@
}
]
},
"RuleCodePrefix": {
"RuleSelector": {
"type": "string",
"enum": [
"A",
Expand Down
28 changes: 14 additions & 14 deletions ruff_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::{command, Parser};
use regex::Regex;
use ruff::fs;
use ruff::logging::LogLevel;
use ruff::registry::{Rule, RuleCodePrefix};
use ruff::registry::{Rule, RuleSelector};
use ruff::resolver::ConfigProcessor;
use ruff::settings::types::{
FilePattern, PatternPrefixPair, PerFileIgnore, PythonVersion, SerializationFormat,
Expand Down Expand Up @@ -66,18 +66,18 @@ pub struct Cli {
/// Comma-separated list of rule codes to enable (or ALL, to enable all
/// rules).
#[arg(long, value_delimiter = ',', value_name = "RULE_CODE")]
pub select: Option<Vec<RuleCodePrefix>>,
pub select: Option<Vec<RuleSelector>>,
/// Like --select, but adds additional rule codes on top of the selected
/// ones.
#[arg(long, value_delimiter = ',', value_name = "RULE_CODE")]
pub extend_select: Option<Vec<RuleCodePrefix>>,
pub extend_select: Option<Vec<RuleSelector>>,
/// Comma-separated list of rule codes to disable.
#[arg(long, value_delimiter = ',', value_name = "RULE_CODE")]
pub ignore: Option<Vec<RuleCodePrefix>>,
pub ignore: Option<Vec<RuleSelector>>,
/// Like --ignore, but adds additional rule codes on top of the ignored
/// ones.
#[arg(long, value_delimiter = ',', value_name = "RULE_CODE")]
pub extend_ignore: Option<Vec<RuleCodePrefix>>,
pub extend_ignore: Option<Vec<RuleSelector>>,
/// List of paths, used to omit files and/or directories from analysis.
#[arg(long, value_delimiter = ',', value_name = "FILE_PATTERN")]
pub exclude: Option<Vec<FilePattern>>,
Expand All @@ -88,11 +88,11 @@ pub struct Cli {
/// List of rule codes to treat as eligible for autofix. Only applicable
/// when autofix itself is enabled (e.g., via `--fix`).
#[arg(long, value_delimiter = ',', value_name = "RULE_CODE")]
pub fixable: Option<Vec<RuleCodePrefix>>,
pub fixable: Option<Vec<RuleSelector>>,
/// List of rule codes to treat as ineligible for autofix. Only applicable
/// when autofix itself is enabled (e.g., via `--fix`).
#[arg(long, value_delimiter = ',', value_name = "RULE_CODE")]
pub unfixable: Option<Vec<RuleCodePrefix>>,
pub unfixable: Option<Vec<RuleSelector>>,
/// List of mappings from file pattern to code to exclude
#[arg(long, value_delimiter = ',')]
pub per_file_ignores: Option<Vec<PatternPrefixPair>>,
Expand Down Expand Up @@ -324,17 +324,17 @@ pub struct Overrides {
pub dummy_variable_rgx: Option<Regex>,
pub exclude: Option<Vec<FilePattern>>,
pub extend_exclude: Option<Vec<FilePattern>>,
pub extend_ignore: Option<Vec<RuleCodePrefix>>,
pub extend_select: Option<Vec<RuleCodePrefix>>,
pub fixable: Option<Vec<RuleCodePrefix>>,
pub ignore: Option<Vec<RuleCodePrefix>>,
pub extend_ignore: Option<Vec<RuleSelector>>,
pub extend_select: Option<Vec<RuleSelector>>,
pub fixable: Option<Vec<RuleSelector>>,
pub ignore: Option<Vec<RuleSelector>>,
pub line_length: Option<usize>,
pub per_file_ignores: Option<Vec<PatternPrefixPair>>,
pub respect_gitignore: Option<bool>,
pub select: Option<Vec<RuleCodePrefix>>,
pub select: Option<Vec<RuleSelector>>,
pub show_source: Option<bool>,
pub target_version: Option<PythonVersion>,
pub unfixable: Option<Vec<RuleCodePrefix>>,
pub unfixable: Option<Vec<RuleSelector>>,
// TODO(charlie): Captured in pyproject.toml as a default, but not part of `Settings`.
pub cache_dir: Option<PathBuf>,
pub fix: Option<bool>,
Expand Down Expand Up @@ -435,7 +435,7 @@ pub fn extract_log_level(cli: &Arguments) -> LogLevel {

/// Convert a list of `PatternPrefixPair` structs to `PerFileIgnore`.
pub fn collect_per_file_ignores(pairs: Vec<PatternPrefixPair>) -> Vec<PerFileIgnore> {
let mut per_file_ignores: FxHashMap<String, Vec<RuleCodePrefix>> = FxHashMap::default();
let mut per_file_ignores: FxHashMap<String, Vec<RuleSelector>> = FxHashMap::default();
for pair in pairs {
per_file_ignores
.entry(pair.pattern)
Expand Down
4 changes: 2 additions & 2 deletions ruff_dev/src/generate_rules_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use anyhow::Result;
use clap::Args;
use ruff::registry::{Linter, Prefixes, RuleCodePrefix};
use ruff::registry::{Linter, Prefixes, RuleSelector};
use strum::IntoEnumIterator;

use crate::utils::replace_readme_section;
Expand All @@ -20,7 +20,7 @@ pub struct Cli {
pub(crate) dry_run: bool,
}

fn generate_table(table_out: &mut String, prefix: &RuleCodePrefix) {
fn generate_table(table_out: &mut String, prefix: &RuleSelector) {
table_out.push_str("| Code | Name | Message | Fix |");
table_out.push('\n');
table_out.push_str("| ---- | ---- | ------- | --- |");
Expand Down
2 changes: 1 addition & 1 deletion ruff_macros/src/define_rule_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {

let rulecodeprefix = super::rule_code_prefix::expand(
&Ident::new("Rule", Span::call_site()),
&Ident::new("RuleCodePrefix", Span::call_site()),
&Ident::new("RuleSelector", Span::call_site()),
mapping.entries.iter().map(|(code, ..)| code),
|code| code_to_name[code],
);
Expand Down
6 changes: 3 additions & 3 deletions ruff_macros/src/rule_code_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use syn::Ident;

const ALL: &str = "ALL";

/// A hash map from deprecated `RuleCodePrefix` to latest
/// `RuleCodePrefix`.
/// A hash map from deprecated `RuleSelector` to latest
/// `RuleSelector`.
pub static PREFIX_REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
HashMap::from_iter([
// TODO(charlie): Remove by 2023-01-01.
Expand Down Expand Up @@ -169,7 +169,7 @@ pub fn expand<'a>(

#prefix_impl

/// A hash map from deprecated `RuleCodePrefix` to latest `RuleCodePrefix`.
/// A hash map from deprecated `RuleSelector` to latest `RuleSelector`.
pub static PREFIX_REDIRECTS: ::once_cell::sync::Lazy<::rustc_hash::FxHashMap<&'static str, #prefix_ident>> = ::once_cell::sync::Lazy::new(|| {
::rustc_hash::FxHashMap::from_iter([
#(#prefix_redirects),*
Expand Down
2 changes: 1 addition & 1 deletion scripts/add_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def main(*, plugin: str, url: str) -> None:
fp.write(f"{indent}{pascal_case(plugin)},")
fp.write("\n")

elif line.strip() == "Linter::Ruff => Prefixes::Single(RuleCodePrefix::RUF),":
elif line.strip() == "Linter::Ruff => Prefixes::Single(RuleSelector::RUF),":
prefix = 'todo!("Fill-in prefix after generating codes")'
fp.write(
f"{indent}Linter::{pascal_case(plugin)} => Prefixes::Single({prefix}),"
Expand Down
52 changes: 16 additions & 36 deletions src/flake8_to_ruff/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use colored::Colorize;
use super::black::Black;
use super::plugin::Plugin;
use super::{parser, plugin};
use crate::registry::RuleCodePrefix;
use crate::registry::RuleSelector;
use crate::rules::flake8_pytest_style::types::{
ParametrizeNameType, ParametrizeValuesRowType, ParametrizeValuesType,
};
Expand All @@ -32,7 +32,7 @@ pub fn convert(
.expect("Unable to find flake8 section in INI file");

// Extract all referenced rule code prefixes, to power plugin inference.
let mut referenced_codes: BTreeSet<RuleCodePrefix> = BTreeSet::default();
let mut referenced_codes: BTreeSet<RuleSelector> = BTreeSet::default();
for (key, value) in flake8 {
if let Some(value) = value {
match key.as_str() {
Expand Down Expand Up @@ -392,7 +392,7 @@ mod tests {

use super::super::plugin::Plugin;
use super::convert;
use crate::registry::RuleCodePrefix;
use crate::registry::RuleSelector;
use crate::rules::pydocstyle::settings::Convention;
use crate::rules::{flake8_quotes, pydocstyle};
use crate::settings::options::Options;
Expand Down Expand Up @@ -428,11 +428,7 @@ mod tests {
per_file_ignores: None,
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::W,
]),
select: Some(vec![RuleSelector::E, RuleSelector::F, RuleSelector::W]),
show_source: None,
src: None,
target_version: None,
Expand Down Expand Up @@ -495,11 +491,7 @@ mod tests {
per_file_ignores: None,
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::W,
]),
select: Some(vec![RuleSelector::E, RuleSelector::F, RuleSelector::W]),
show_source: None,
src: None,
target_version: None,
Expand Down Expand Up @@ -562,11 +554,7 @@ mod tests {
per_file_ignores: None,
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::W,
]),
select: Some(vec![RuleSelector::E, RuleSelector::F, RuleSelector::W]),
show_source: None,
src: None,
target_version: None,
Expand Down Expand Up @@ -629,11 +617,7 @@ mod tests {
per_file_ignores: None,
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::W,
]),
select: Some(vec![RuleSelector::E, RuleSelector::F, RuleSelector::W]),
show_source: None,
src: None,
target_version: None,
Expand Down Expand Up @@ -696,11 +680,7 @@ mod tests {
per_file_ignores: None,
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::W,
]),
select: Some(vec![RuleSelector::E, RuleSelector::F, RuleSelector::W]),
show_source: None,
src: None,
target_version: None,
Expand Down Expand Up @@ -772,10 +752,10 @@ mod tests {
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::D,
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::W,
RuleSelector::D,
RuleSelector::E,
RuleSelector::F,
RuleSelector::W,
]),
show_source: None,
src: None,
Expand Down Expand Up @@ -842,10 +822,10 @@ mod tests {
required_version: None,
respect_gitignore: None,
select: Some(vec![
RuleCodePrefix::E,
RuleCodePrefix::F,
RuleCodePrefix::Q,
RuleCodePrefix::W,
RuleSelector::E,
RuleSelector::F,
RuleSelector::Q,
RuleSelector::W,
]),
show_source: None,
src: None,
Expand Down
Loading

0 comments on commit b19258a

Please sign in to comment.