Skip to content

Commit

Permalink
Generate RuleCode::origin() via macro
Browse files Browse the repository at this point in the history
  • Loading branch information
not-my-profile committed Jan 10, 2023
1 parent 643cedb commit a8417d5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 425 deletions.
25 changes: 24 additions & 1 deletion ruff_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
)]
#![forbid(unsafe_code)]

use syn::{parse_macro_input, DeriveInput};
use proc_macro2::Span;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Ident};

mod config;
mod prefixes;
mod rule_code_prefix;

#[proc_macro_derive(ConfigurationOptions, attributes(option, doc, option_group))]
Expand All @@ -34,3 +37,23 @@ pub fn derive_rule_code_prefix(input: proc_macro::TokenStream) -> proc_macro::To
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

#[proc_macro]
pub fn origin_by_code(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ident = parse_macro_input!(item as Ident).to_string();
let mut iter = prefixes::PREFIX_TO_ORIGIN.iter();
let origin = loop {
let (prefix, origin) = iter
.next()
.unwrap_or_else(|| panic!("code doesn't start with any recognized prefix: {ident}"));
if ident.starts_with(prefix) {
break origin;
}
};
let prefix = Ident::new(origin, Span::call_site());

quote! {
RuleOrigin::#prefix
}
.into()
}
39 changes: 39 additions & 0 deletions ruff_macros/src/prefixes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Longer prefixes should come first so that you can find an origin for a code
// by simply picking the first entry that starts with the given prefix.

pub const PREFIX_TO_ORIGIN: &[(&str, &str)] = &[
("ANN", "Flake8Annotations"),
("ARG", "Flake8UnusedArguments"),
("A", "Flake8Builtins"),
("BLE", "Flake8BlindExcept"),
("B", "Flake8Bugbear"),
("C4", "Flake8Comprehensions"),
("C9", "McCabe"),
("DTZ", "Flake8Datetimez"),
("D", "Pydocstyle"),
("ERA", "Eradicate"),
("EM", "Flake8ErrMsg"),
("E", "Pycodestyle"),
("FBT", "Flake8BooleanTrap"),
("F", "Pyflakes"),
("ICN", "Flake8ImportConventions"),
("ISC", "Flake8ImplicitStrConcat"),
("I", "Isort"),
("N", "PEP8Naming"),
("PD", "PandasVet"),
("PGH", "PygrepHooks"),
("PL", "Pylint"),
("PT", "Flake8PytestStyle"),
("Q", "Flake8Quotes"),
("RET", "Flake8Return"),
("S", "Flake8Bandit"),
("SIM", "Flake8Simplify"),
("T10", "Flake8Debugger"),
("T20", "Flake8Print"),
("TID", "Flake8TidyImports"),
("UP", "Pyupgrade"),
("W", "Pycodestyle"),
("YTT", "Flake82020"),
("PIE", "Flake8Pie"),
("RUF", "Ruff"),
];
27 changes: 0 additions & 27 deletions scripts/add_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,6 @@ def main(*, name: str, code: str, origin: str) -> None:
fp.write("\n")
has_written = True

# Add the relevant code-to-origin pair to `src/registry.rs`.
with open(os.path.join(ROOT_DIR, "src/registry.rs")) as fp:
content = fp.read()

seen_impl = False
has_written = False
with open(os.path.join(ROOT_DIR, "src/registry.rs"), "w") as fp:
for line in content.splitlines():
fp.write(line)
fp.write("\n")

if has_written:
continue

if line.startswith("impl RuleCode"):
seen_impl = True
continue

if not seen_impl:
continue

if line.strip() == f"// {origin}":
indent = line.split("//")[0]
fp.write(f"{indent}RuleCode::{code} => RuleOrigin::{pascal_case(origin)},")
fp.write("\n")
has_written = True


if __name__ == "__main__":
parser = argparse.ArgumentParser(
Expand Down

0 comments on commit a8417d5

Please sign in to comment.