mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-08 21:55:12 +00:00
refactor: Define origin names & URLs within doc comments
This commit is contained in:
parent
6868bb46f5
commit
b78b6f275e
36 changed files with 120 additions and 99 deletions
84
build.rs
Normal file
84
build.rs
Normal file
|
@ -0,0 +1,84 @@
|
|||
use std::fs;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn main() {
|
||||
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
||||
generate_origin_name_and_url(&out_dir);
|
||||
}
|
||||
|
||||
const RULES_SUBMODULE_DOC_PREFIX: &str = "//! Rules from ";
|
||||
|
||||
/// The `src/rules/*/mod.rs` files are expected to have a first line such as the
|
||||
/// following:
|
||||
///
|
||||
/// //! Rules from [Pyflakes](https://pypi.org/project/pyflakes/2.5.0/).
|
||||
///
|
||||
/// This function extracts the link label and url from these comments and
|
||||
/// generates the `name` and `url` functions for the `RuleOrigin` enum
|
||||
/// accordingly, so that they can be used by `ruff_dev::generate_rules_table`.
|
||||
fn generate_origin_name_and_url(out_dir: &Path) {
|
||||
println!("cargo:rerun-if-changed=src/rules/");
|
||||
|
||||
let mut name_match_arms: String = r#"RuleOrigin::Ruff => "Ruff-specific rules","#.into();
|
||||
let mut url_match_arms: String = r#"RuleOrigin::Ruff => None,"#.into();
|
||||
|
||||
for file in fs::read_dir("src/rules/")
|
||||
.unwrap()
|
||||
.flatten()
|
||||
.filter(|f| f.file_type().unwrap().is_dir() && f.file_name() != "ruff")
|
||||
{
|
||||
let mod_rs_path = file.path().join("mod.rs");
|
||||
let mod_rs_path = mod_rs_path.to_str().unwrap();
|
||||
let first_line = BufReader::new(fs::File::open(mod_rs_path).unwrap())
|
||||
.lines()
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
let Some(comment) = first_line.strip_prefix(RULES_SUBMODULE_DOC_PREFIX) else {
|
||||
panic!("expected first line in {mod_rs_path} to start with `{RULES_SUBMODULE_DOC_PREFIX}`")
|
||||
};
|
||||
let md_link = comment.trim_end_matches('.');
|
||||
|
||||
let (name, url) = md_link
|
||||
.strip_prefix('[')
|
||||
.unwrap()
|
||||
.strip_suffix(')')
|
||||
.unwrap()
|
||||
.split_once("](")
|
||||
.unwrap();
|
||||
|
||||
let dirname = file.file_name();
|
||||
let dirname = dirname.to_str().unwrap();
|
||||
|
||||
let variant_name = dirname
|
||||
.split('_')
|
||||
.map(|part| match part {
|
||||
"errmsg" => "ErrMsg".to_string(),
|
||||
"mccabe" => "McCabe".to_string(),
|
||||
"pep8" => "PEP8".to_string(),
|
||||
_ => format!("{}{}", part[..1].to_uppercase(), &part[1..]),
|
||||
})
|
||||
.collect::<String>();
|
||||
|
||||
name_match_arms.push_str(&format!(r#"RuleOrigin::{variant_name} => "{name}","#));
|
||||
url_match_arms.push_str(&format!(r#"RuleOrigin::{variant_name} => Some("{url}"),"#));
|
||||
}
|
||||
|
||||
write!(
|
||||
BufWriter::new(fs::File::create(out_dir.join("origin.rs")).unwrap()),
|
||||
"
|
||||
impl RuleOrigin {{
|
||||
pub fn name(&self) -> &'static str {{
|
||||
match self {{ {name_match_arms} }}
|
||||
}}
|
||||
|
||||
pub fn url(&self) -> Option<&'static str> {{
|
||||
match self {{ {url_match_arms} }}
|
||||
}}
|
||||
}}
|
||||
"
|
||||
)
|
||||
.unwrap();
|
||||
}
|
100
src/registry.rs
100
src/registry.rs
|
@ -478,46 +478,9 @@ impl Prefixes {
|
|||
}
|
||||
}
|
||||
|
||||
impl RuleOrigin {
|
||||
pub fn name(&self) -> &'static str {
|
||||
match self {
|
||||
RuleOrigin::Eradicate => "eradicate",
|
||||
RuleOrigin::Flake82020 => "flake8-2020",
|
||||
RuleOrigin::Flake8Annotations => "flake8-annotations",
|
||||
RuleOrigin::Flake8Bandit => "flake8-bandit",
|
||||
RuleOrigin::Flake8BlindExcept => "flake8-blind-except",
|
||||
RuleOrigin::Flake8BooleanTrap => "flake8-boolean-trap",
|
||||
RuleOrigin::Flake8Bugbear => "flake8-bugbear",
|
||||
RuleOrigin::Flake8Builtins => "flake8-builtins",
|
||||
RuleOrigin::Flake8Comprehensions => "flake8-comprehensions",
|
||||
RuleOrigin::Flake8Debugger => "flake8-debugger",
|
||||
RuleOrigin::Flake8ErrMsg => "flake8-errmsg",
|
||||
RuleOrigin::Flake8ImplicitStrConcat => "flake8-implicit-str-concat",
|
||||
RuleOrigin::Flake8ImportConventions => "flake8-import-conventions",
|
||||
RuleOrigin::Flake8Print => "flake8-print",
|
||||
RuleOrigin::Flake8PytestStyle => "flake8-pytest-style",
|
||||
RuleOrigin::Flake8Quotes => "flake8-quotes",
|
||||
RuleOrigin::Flake8Return => "flake8-return",
|
||||
RuleOrigin::Flake8TidyImports => "flake8-tidy-imports",
|
||||
RuleOrigin::Flake8Simplify => "flake8-simplify",
|
||||
RuleOrigin::Flake8UnusedArguments => "flake8-unused-arguments",
|
||||
RuleOrigin::Flake8Datetimez => "flake8-datetimez",
|
||||
RuleOrigin::Isort => "isort",
|
||||
RuleOrigin::McCabe => "mccabe",
|
||||
RuleOrigin::PandasVet => "pandas-vet",
|
||||
RuleOrigin::PEP8Naming => "pep8-naming",
|
||||
RuleOrigin::Pycodestyle => "pycodestyle",
|
||||
RuleOrigin::Pydocstyle => "pydocstyle",
|
||||
RuleOrigin::Pyflakes => "Pyflakes",
|
||||
RuleOrigin::PygrepHooks => "pygrep-hooks",
|
||||
RuleOrigin::Pylint => "Pylint",
|
||||
RuleOrigin::Pyupgrade => "pyupgrade",
|
||||
RuleOrigin::Flake8Pie => "flake8-pie",
|
||||
RuleOrigin::Flake8Commas => "flake8-commas",
|
||||
RuleOrigin::Ruff => "Ruff-specific rules",
|
||||
}
|
||||
}
|
||||
include!(concat!(env!("OUT_DIR"), "/origin.rs"));
|
||||
|
||||
impl RuleOrigin {
|
||||
pub fn prefixes(&self) -> Prefixes {
|
||||
match self {
|
||||
RuleOrigin::Eradicate => Prefixes::Single(RuleCodePrefix::ERA),
|
||||
|
@ -564,65 +527,6 @@ impl RuleOrigin {
|
|||
RuleOrigin::Ruff => Prefixes::Single(RuleCodePrefix::RUF),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn url(&self) -> Option<&'static str> {
|
||||
match self {
|
||||
RuleOrigin::Eradicate => Some("https://pypi.org/project/eradicate/2.1.0/"),
|
||||
RuleOrigin::Flake82020 => Some("https://pypi.org/project/flake8-2020/1.7.0/"),
|
||||
RuleOrigin::Flake8Annotations => {
|
||||
Some("https://pypi.org/project/flake8-annotations/2.9.1/")
|
||||
}
|
||||
RuleOrigin::Flake8Bandit => Some("https://pypi.org/project/flake8-bandit/4.1.1/"),
|
||||
RuleOrigin::Flake8BlindExcept => {
|
||||
Some("https://pypi.org/project/flake8-blind-except/0.2.1/")
|
||||
}
|
||||
RuleOrigin::Flake8BooleanTrap => {
|
||||
Some("https://pypi.org/project/flake8-boolean-trap/0.1.0/")
|
||||
}
|
||||
RuleOrigin::Flake8Bugbear => Some("https://pypi.org/project/flake8-bugbear/22.10.27/"),
|
||||
RuleOrigin::Flake8Builtins => Some("https://pypi.org/project/flake8-builtins/2.0.1/"),
|
||||
RuleOrigin::Flake8Comprehensions => {
|
||||
Some("https://pypi.org/project/flake8-comprehensions/3.10.1/")
|
||||
}
|
||||
RuleOrigin::Flake8Debugger => Some("https://pypi.org/project/flake8-debugger/4.1.2/"),
|
||||
RuleOrigin::Flake8ErrMsg => Some("https://pypi.org/project/flake8-errmsg/0.4.0/"),
|
||||
RuleOrigin::Flake8ImplicitStrConcat => {
|
||||
Some("https://pypi.org/project/flake8-implicit-str-concat/0.3.0/")
|
||||
}
|
||||
RuleOrigin::Flake8ImportConventions => {
|
||||
Some("https://github.com/joaopalmeiro/flake8-import-conventions")
|
||||
}
|
||||
RuleOrigin::Flake8Print => Some("https://pypi.org/project/flake8-print/5.0.0/"),
|
||||
RuleOrigin::Flake8PytestStyle => {
|
||||
Some("https://pypi.org/project/flake8-pytest-style/1.6.0/")
|
||||
}
|
||||
RuleOrigin::Flake8Quotes => Some("https://pypi.org/project/flake8-quotes/3.3.1/"),
|
||||
RuleOrigin::Flake8Return => Some("https://pypi.org/project/flake8-return/1.2.0/"),
|
||||
RuleOrigin::Flake8Simplify => Some("https://pypi.org/project/flake8-simplify/0.19.3/"),
|
||||
RuleOrigin::Flake8TidyImports => {
|
||||
Some("https://pypi.org/project/flake8-tidy-imports/4.8.0/")
|
||||
}
|
||||
RuleOrigin::Flake8UnusedArguments => {
|
||||
Some("https://pypi.org/project/flake8-unused-arguments/0.0.12/")
|
||||
}
|
||||
RuleOrigin::Flake8Datetimez => {
|
||||
Some("https://pypi.org/project/flake8-datetimez/20.10.0/")
|
||||
}
|
||||
RuleOrigin::Isort => Some("https://pypi.org/project/isort/5.10.1/"),
|
||||
RuleOrigin::McCabe => Some("https://pypi.org/project/mccabe/0.7.0/"),
|
||||
RuleOrigin::PandasVet => Some("https://pypi.org/project/pandas-vet/0.2.3/"),
|
||||
RuleOrigin::PEP8Naming => Some("https://pypi.org/project/pep8-naming/0.13.2/"),
|
||||
RuleOrigin::Pycodestyle => Some("https://pypi.org/project/pycodestyle/2.9.1/"),
|
||||
RuleOrigin::Pydocstyle => Some("https://pypi.org/project/pydocstyle/6.1.1/"),
|
||||
RuleOrigin::Pyflakes => Some("https://pypi.org/project/pyflakes/2.5.0/"),
|
||||
RuleOrigin::Pylint => Some("https://pypi.org/project/pylint/2.15.7/"),
|
||||
RuleOrigin::PygrepHooks => Some("https://github.com/pre-commit/pygrep-hooks"),
|
||||
RuleOrigin::Pyupgrade => Some("https://pypi.org/project/pyupgrade/3.2.0/"),
|
||||
RuleOrigin::Flake8Pie => Some("https://pypi.org/project/flake8-pie/0.16.0/"),
|
||||
RuleOrigin::Flake8Commas => Some("https://pypi.org/project/flake8-commas/2.1.0/"),
|
||||
RuleOrigin::Ruff => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum LintSource {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [eradicate](https://pypi.org/project/eradicate/2.1.0/).
|
||||
pub(crate) mod detection;
|
||||
pub(crate) mod rules;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-2020](https://pypi.org/project/flake8-2020/1.7.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-annotations](https://pypi.org/project/flake8-annotations/2.9.1/).
|
||||
mod fixes;
|
||||
pub(crate) mod helpers;
|
||||
pub(crate) mod rules;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-bandit](https://pypi.org/project/flake8-bandit/4.1.1/).
|
||||
mod helpers;
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-blind-except](https://pypi.org/project/flake8-blind-except/0.2.1/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-boolean-trap](https://pypi.org/project/flake8-boolean-trap/0.1.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-bugbear](https://pypi.org/project/flake8-bugbear/22.10.27/).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-builtins](https://pypi.org/project/flake8-builtins/2.0.1/).
|
||||
pub(crate) mod rules;
|
||||
pub(crate) mod types;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-commas](https://pypi.org/project/flake8-commas/2.1.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-comprehensions](https://pypi.org/project/flake8-comprehensions/3.10.1/).
|
||||
mod fixes;
|
||||
pub(crate) mod rules;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-datetimez](https://pypi.org/project/flake8-datetimez/20.10.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-debugger](https://pypi.org/project/flake8-debugger/4.1.2/).
|
||||
pub(crate) mod rules;
|
||||
pub(crate) mod types;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-errmsg](https://pypi.org/project/flake8-errmsg/0.4.0/).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-implicit-str-concat](https://pypi.org/project/flake8-implicit-str-concat/0.3.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-import-conventions](https://github.com/joaopalmeiro/flake8-import-conventions).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-pie](https://pypi.org/project/flake8-pie/0.16.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-print](https://pypi.org/project/flake8-print/5.0.0/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-pytest-style](https://pypi.org/project/flake8-pytest-style/1.6.0/).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
pub mod types;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-quotes](https://pypi.org/project/flake8-quotes/3.3.1/).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-return](https://pypi.org/project/flake8-return/1.2.0/).
|
||||
mod helpers;
|
||||
pub(crate) mod rules;
|
||||
mod visitor;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-simplify](https://pypi.org/project/flake8-simplify/0.19.3/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-tidy-imports](https://pypi.org/project/flake8-tidy-imports/4.8.0/).
|
||||
pub mod options;
|
||||
|
||||
pub mod banned_api;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [flake8-unused-arguments](https://pypi.org/project/flake8-unused-arguments/0.0.12/).
|
||||
mod helpers;
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [isort](https://pypi.org/project/isort/5.10.1/).
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [mccabe](https://pypi.org/project/mccabe/0.7.0/).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [pandas-vet](https://pypi.org/project/pandas-vet/0.2.3/).
|
||||
pub(crate) mod helpers;
|
||||
pub(crate) mod rules;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [pep8-naming](https://pypi.org/project/pep8-naming/0.13.2/).
|
||||
mod helpers;
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [pycodestyle](https://pypi.org/project/pycodestyle/2.9.1/).
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [pydocstyle](https://pypi.org/project/pydocstyle/6.1.1/).
|
||||
pub(crate) mod helpers;
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [Pyflakes](https://pypi.org/project/pyflakes/2.5.0/).
|
||||
pub(crate) mod cformat;
|
||||
pub(crate) mod fixes;
|
||||
pub(crate) mod format;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [pygrep-hooks](https://github.com/pre-commit/pygrep-hooks).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [Pylint](https://pypi.org/project/pylint/2.15.7/).
|
||||
pub(crate) mod rules;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//! Rules from [pyupgrade](https://pypi.org/project/pyupgrade/3.2.0/).
|
||||
mod fixes;
|
||||
pub(crate) mod rules;
|
||||
pub mod settings;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Module for Ruff-specific rules.
|
||||
//! Ruff-specific rules.
|
||||
|
||||
pub(crate) mod rules;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue