forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Decouple Rule from linter prefixes
543865c introduced RuleCode::origin() -> RuleOrigin generation via a macro, while that signature now has been renamed to Rule::origin() -> Linter we actually want to get rid of it since rules and linters shouldn't be this tightly coupled (since one rule can exist in multiple linters). Another disadvantage of the previous approach was that the prefixes had to be defined in ruff_macros/src/prefixes.rs, which was easy to miss when defining new linters in src/*, case in point INP001 => violations::ImplicitNamespacePackage has in the meantime been added without ruff_macros/src/prefixes.rs being updated accordingly which resulted in `ruff --explain INP001` mistakenly reporting that the rule belongs to isort (since INP001 starts with the isort prefix "I"). The derive proc macro introduced in this commit requires every variant to have at least one #[prefix = "..."], eliminating such mistakes.
- Loading branch information
1 parent
b19258a
commit 4e4643a
Showing
7 changed files
with
122 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use quote::quote; | ||
use syn::spanned::Spanned; | ||
use syn::{Data, DataEnum, DeriveInput, Error, Lit, Meta, MetaNameValue}; | ||
|
||
pub fn derive_impl(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> { | ||
let DeriveInput { ident, data: Data::Enum(DataEnum { | ||
variants, .. | ||
}), .. } = input else { | ||
return Err(Error::new(input.ident.span(), "only named fields are supported")); | ||
}; | ||
|
||
let mut parsed = Vec::new(); | ||
|
||
for variant in variants { | ||
let prefix_attrs: Vec<_> = variant | ||
.attrs | ||
.iter() | ||
.filter(|a| a.path.is_ident("prefix")) | ||
.collect(); | ||
|
||
if prefix_attrs.is_empty() { | ||
return Err(Error::new( | ||
variant.span(), | ||
r#"Missing [#prefix = "..."] attribute"#, | ||
)); | ||
} | ||
|
||
for attr in prefix_attrs { | ||
let Ok(Meta::NameValue(MetaNameValue{lit: Lit::Str(lit), ..})) = attr.parse_meta() else { | ||
return Err(Error::new(attr.span(), r#"expected attribute to be in the form of [#prefix = "..."]"#)) | ||
}; | ||
parsed.push((lit, variant.ident.clone())); | ||
} | ||
} | ||
|
||
parsed.sort_by_key(|(prefix, _)| prefix.value().len()); | ||
|
||
let mut if_statements = quote!(); | ||
|
||
for (prefix, field) in parsed { | ||
if_statements.extend(quote! {if let Some(rest) = code.strip_prefix(#prefix) { | ||
return Some((#ident::#field, rest)); | ||
}}); | ||
} | ||
|
||
Ok(quote! { | ||
impl crate::registry::ParseCode for #ident { | ||
fn parse_code(code: &str) -> Option<(Self, &str)> { | ||
#if_statements | ||
None | ||
} | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters