mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Use a derive macro for Violations (#14557)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
6fd10e2fe7
commit
14ba469fc0
629 changed files with 2555 additions and 2562 deletions
|
@ -2,8 +2,9 @@
|
|||
|
||||
use crate::cache_key::derive_cache_key;
|
||||
use crate::newtype_index::generate_newtype_index;
|
||||
use crate::violation_metadata::violation_metadata;
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{parse_macro_input, DeriveInput, ItemFn, ItemStruct};
|
||||
use syn::{parse_macro_input, DeriveInput, Error, ItemFn, ItemStruct};
|
||||
|
||||
mod cache_key;
|
||||
mod combine_options;
|
||||
|
@ -13,7 +14,7 @@ mod map_codes;
|
|||
mod newtype_index;
|
||||
mod rule_code_prefix;
|
||||
mod rule_namespace;
|
||||
mod violation;
|
||||
mod violation_metadata;
|
||||
|
||||
#[proc_macro_derive(OptionsMetadata, attributes(option, doc, option_group))]
|
||||
pub fn derive_options_metadata(input: TokenStream) -> TokenStream {
|
||||
|
@ -47,12 +48,12 @@ pub fn cache_key(input: TokenStream) -> TokenStream {
|
|||
TokenStream::from(stream)
|
||||
}
|
||||
|
||||
/// Adds an `explanation()` method from the doc comment.
|
||||
#[proc_macro_attribute]
|
||||
pub fn violation(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let violation = parse_macro_input!(item as ItemStruct);
|
||||
violation::violation(&violation)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
#[proc_macro_derive(ViolationMetadata)]
|
||||
pub fn derive_violation_metadata(item: TokenStream) -> TokenStream {
|
||||
let input: DeriveInput = parse_macro_input!(item);
|
||||
|
||||
violation_metadata(input)
|
||||
.unwrap_or_else(Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
||||
|
|
|
@ -420,8 +420,7 @@ fn register_rules<'a>(input: impl Iterator<Item = &'a Rule>) -> TokenStream {
|
|||
rule_fixable_match_arms.extend(
|
||||
quote! {#(#attrs)* Self::#name => <#path as ruff_diagnostics::Violation>::FIX_AVAILABILITY,},
|
||||
);
|
||||
rule_explanation_match_arms
|
||||
.extend(quote! {#(#attrs)* Self::#name => #path::explanation(),});
|
||||
rule_explanation_match_arms.extend(quote! {#(#attrs)* Self::#name => #path::explain(),});
|
||||
|
||||
// Enable conversion from `DiagnosticKind` to `Rule`.
|
||||
from_impls_for_diagnostic_kind
|
||||
|
@ -457,6 +456,7 @@ fn register_rules<'a>(input: impl Iterator<Item = &'a Rule>) -> TokenStream {
|
|||
|
||||
/// Returns the documentation for this rule.
|
||||
pub fn explanation(&self) -> Option<&'static str> {
|
||||
use ruff_diagnostics::ViolationMetadata;
|
||||
match self { #rule_explanation_match_arms }
|
||||
}
|
||||
|
||||
|
@ -466,6 +466,7 @@ fn register_rules<'a>(input: impl Iterator<Item = &'a Rule>) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl AsRule for ruff_diagnostics::DiagnosticKind {
|
||||
fn rule(&self) -> Rule {
|
||||
match self.name.as_str() {
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{Attribute, Error, ItemStruct, Lit, LitStr, Meta, Result};
|
||||
|
||||
fn parse_attr<'a, const LEN: usize>(
|
||||
path: [&'static str; LEN],
|
||||
attr: &'a Attribute,
|
||||
) -> Option<&'a LitStr> {
|
||||
if let Meta::NameValue(name_value) = &attr.meta {
|
||||
let path_idents = name_value
|
||||
.path
|
||||
.segments
|
||||
.iter()
|
||||
.map(|segment| &segment.ident);
|
||||
|
||||
if itertools::equal(path_idents, path) {
|
||||
if let syn::Expr::Lit(syn::ExprLit {
|
||||
lit: Lit::Str(lit), ..
|
||||
}) = &name_value.value
|
||||
{
|
||||
return Some(lit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// 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(crate) 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)]
|
||||
#violation
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[automatically_derived]
|
||||
#[allow(deprecated)]
|
||||
impl From<#ident> for ruff_diagnostics::DiagnosticKind {
|
||||
fn from(value: #ident) -> Self {
|
||||
use ruff_diagnostics::Violation;
|
||||
|
||||
Self {
|
||||
body: Violation::message(&value),
|
||||
suggestion: Violation::fix_title(&value),
|
||||
name: stringify!(#ident).to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#violation
|
||||
|
||||
#[automatically_derived]
|
||||
#[allow(deprecated)]
|
||||
impl #ident {
|
||||
pub fn explanation() -> Option<&'static str> {
|
||||
Some(#explanation)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[automatically_derived]
|
||||
impl From<#ident> for ruff_diagnostics::DiagnosticKind {
|
||||
fn from(value: #ident) -> Self {
|
||||
use ruff_diagnostics::Violation;
|
||||
|
||||
Self {
|
||||
body: Violation::message(&value),
|
||||
suggestion: Violation::fix_title(&value),
|
||||
name: stringify!(#ident).to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(violation)
|
||||
}
|
66
crates/ruff_macros/src/violation_metadata.rs
Normal file
66
crates/ruff_macros/src/violation_metadata.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{Attribute, DeriveInput, Error, Lit, LitStr, Meta};
|
||||
|
||||
pub(crate) fn violation_metadata(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
let docs = get_docs(&input.attrs)?;
|
||||
|
||||
let name = input.ident;
|
||||
|
||||
Ok(quote! {
|
||||
#[automatically_derived]
|
||||
#[allow(deprecated)]
|
||||
impl ruff_diagnostics::ViolationMetadata for #name {
|
||||
fn rule_name() -> &'static str {
|
||||
stringify!(#name)
|
||||
}
|
||||
|
||||
fn explain() -> Option<&'static str> {
|
||||
Some(#docs)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Collect all doc comment attributes into a string
|
||||
fn get_docs(attrs: &[Attribute]) -> syn::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)
|
||||
}
|
||||
|
||||
fn parse_attr<'a, const LEN: usize>(
|
||||
path: [&'static str; LEN],
|
||||
attr: &'a Attribute,
|
||||
) -> Option<&'a LitStr> {
|
||||
if let Meta::NameValue(name_value) = &attr.meta {
|
||||
let path_idents = name_value
|
||||
.path
|
||||
.segments
|
||||
.iter()
|
||||
.map(|segment| &segment.ident);
|
||||
|
||||
if itertools::equal(path_idents, path) {
|
||||
if let syn::Expr::Lit(syn::ExprLit {
|
||||
lit: Lit::Str(lit), ..
|
||||
}) = &name_value.value
|
||||
{
|
||||
return Some(lit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue