mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Deprecate define violation (#3358)
* Add `#[violation]` proc macro as a replacement for `define_violation!` * Switch all rules to #[violation]
This commit is contained in:
parent
22e6778e17
commit
348a38d261
305 changed files with 4680 additions and 4635 deletions
|
@ -1,7 +1,7 @@
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::{Attribute, Error, Ident, Lit, LitStr, Meta, Result, Token};
|
||||
use syn::{Attribute, Error, Ident, ItemStruct, Lit, LitStr, Meta, Result, Token};
|
||||
|
||||
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
|
||||
if let Meta::NameValue(name_value) = attr.parse_meta().ok()? {
|
||||
|
@ -73,3 +73,45 @@ pub fn define_violation(input: &TokenStream, meta: LintMeta) -> TokenStream {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect all doc comment attributes into a string
|
||||
fn get_docs(attrs: &[Attribute]) -> Result<String> {
|
||||
let mut explanation = String::new();
|
||||
for attr in attrs {
|
||||
if attr.path.is_ident("doc") {
|
||||
if let Some(lit) = parse_attr(["doc"], attr) {
|
||||
let value = lit.value();
|
||||
// `/// ` adds
|
||||
let line = value.strip_prefix(' ').unwrap_or(&value);
|
||||
explanation.push_str(line);
|
||||
explanation.push('\n');
|
||||
} else {
|
||||
return Err(Error::new_spanned(attr, "unimplemented doc comment style"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(explanation)
|
||||
}
|
||||
|
||||
pub fn violation(violation: &ItemStruct) -> Result<TokenStream> {
|
||||
let ident = &violation.ident;
|
||||
let explanation = get_docs(&violation.attrs)?;
|
||||
let violation = if explanation.trim().is_empty() {
|
||||
quote! {
|
||||
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#violation
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#violation
|
||||
|
||||
impl #ident {
|
||||
pub fn explanation() -> Option<&'static str> {
|
||||
Some(#explanation)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(violation)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use crate::cache_key::derive_cache_key;
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{parse_macro_input, DeriveInput, ItemFn};
|
||||
use syn::{parse_macro_input, DeriveInput, ItemFn, ItemStruct};
|
||||
|
||||
mod cache_key;
|
||||
mod config;
|
||||
|
@ -45,6 +45,16 @@ pub fn define_violation(item: proc_macro::TokenStream) -> proc_macro::TokenStrea
|
|||
define_violation::define_violation(&item.into(), meta).into()
|
||||
}
|
||||
|
||||
/// Adds an `explanation()` method from the doc comment and
|
||||
/// `#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]`
|
||||
#[proc_macro_attribute]
|
||||
pub fn violation(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let violation = parse_macro_input!(item as ItemStruct);
|
||||
define_violation::violation(&violation)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(RuleNamespace, attributes(prefix))]
|
||||
pub fn derive_rule_namespace(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue