Skip to content

Commit

Permalink
many-to-one 4/9: Rename define_rule_mapping! to register_rules!
Browse files Browse the repository at this point in the history
Currently the define_rule_mapping! macro generates both the Rule enum as
well as the RuleCodePrefix enum and the mapping between the two.  After
this commit series the macro will only generate the Rule enum and the
RuleCodePrefix enum and the mapping will be generated by a new map_codes
proc macro, so we rename the macro now to fit its new purpose.
  • Loading branch information
not-my-profile authored and charliermarsh committed Feb 14, 2023
1 parent 1b8d2df commit 65a3461
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::fix::Fix;
use crate::rules;
use crate::violation::Violation;

ruff_macros::define_rule_mapping!(
ruff_macros::register_rules!(
// pycodestyle errors
E101 => rules::pycodestyle::rules::MixedSpacesAndTabs,
#[cfg(feature = "logical_lines")]
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use syn::{parse_macro_input, DeriveInput, ItemFn};

mod config;
mod define_rule_mapping;
mod define_violation;
mod derive_message_formats;
mod register_rules;
mod rule_code_prefix;
mod rule_namespace;

Expand All @@ -19,9 +19,9 @@ pub fn derive_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream
}

#[proc_macro]
pub fn define_rule_mapping(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mapping = parse_macro_input!(item as define_rule_mapping::Mapping);
define_rule_mapping::define_rule_mapping(&mapping).into()
pub fn register_rules(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mapping = parse_macro_input!(item as register_rules::Input);
register_rules::register_rules(&mapping).into()
}

#[proc_macro]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quote::quote;
use syn::parse::Parse;
use syn::{Attribute, Ident, LitStr, Path, Token};

pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
pub fn register_rules(input: &Input) -> proc_macro2::TokenStream {
let mut rule_variants = quote!();
let mut diagnostic_kind_variants = quote!();
let mut rule_message_formats_match_arms = quote!();
Expand All @@ -19,7 +19,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
let mut diagnostic_kind_commit_match_arms = quote!();
let mut from_impls_for_diagnostic_kind = quote!();

for (code, path, name, attr) in &mapping.entries {
for (code, path, name, attr) in &input.entries {
let code_str = LitStr::new(&code.to_string(), Span::call_site());
rule_variants.extend(quote! {
#[doc = #code_str]
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
});
}

let code_to_name: HashMap<_, _> = mapping
let code_to_name: HashMap<_, _> = input
.entries
.iter()
.map(|(code, _, name, _)| (code.to_string(), name))
Expand All @@ -64,7 +64,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
let rule_code_prefix = super::rule_code_prefix::expand(
&Ident::new("Rule", Span::call_site()),
&Ident::new("RuleCodePrefix", Span::call_site()),
mapping.entries.iter().map(|(code, .., attr)| (code, attr)),
input.entries.iter().map(|(code, .., attr)| (code, attr)),
|code| code_to_name[code],
);

Expand Down Expand Up @@ -161,11 +161,11 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
}
}

pub struct Mapping {
pub struct Input {
entries: Vec<(Ident, Path, Ident, Vec<Attribute>)>,
}

impl Parse for Mapping {
impl Parse for Input {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut entries = Vec::new();
while !input.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion scripts/add_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def main(*, name: str, code: str, linter: str) -> None:
if has_written:
continue

if line.startswith("ruff_macros::define_rule_mapping!"):
if line.startswith("ruff_macros::register_rules!"):
seen_macro = True
continue

Expand Down

0 comments on commit 65a3461

Please sign in to comment.