mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-19 02:55:20 +00:00
refactor: Extract ruff_wasm
(#3401)
This commit is contained in:
parent
a7f3532395
commit
229f1c34cb
14 changed files with 193 additions and 181 deletions
|
@ -156,27 +156,16 @@ pub fn map_codes(func: &ItemFn) -> syn::Result<TokenStream> {
|
|||
|
||||
out.extend(quote! {
|
||||
impl RuleCodePrefix {
|
||||
pub fn parse(linter: &Linter, code: &str) -> Result<Self, FromCodeError> {
|
||||
pub fn parse(linter: &Linter, code: &str) -> Result<Self, crate::registry::FromCodeError> {
|
||||
use std::str::FromStr;
|
||||
|
||||
Ok(match linter {
|
||||
#(Linter::#linter_idents => RuleCodePrefix::#linter_idents(#linter_idents::from_str(code).map_err(|_| FromCodeError::Unknown)?),)*
|
||||
#(Linter::#linter_idents => RuleCodePrefix::#linter_idents(#linter_idents::from_str(code).map_err(|_| crate::registry::FromCodeError::Unknown)?),)*
|
||||
})
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
out.extend(quote! {
|
||||
impl crate::registry::Rule {
|
||||
pub fn from_code(code: &str) -> Result<Self, FromCodeError> {
|
||||
use crate::registry::RuleNamespace;
|
||||
let (linter, code) = Linter::parse_code(code).ok_or(FromCodeError::Unknown)?;
|
||||
let prefix: RuleCodePrefix = RuleCodePrefix::parse(&linter, code)?;
|
||||
Ok(prefix.into_iter().next().unwrap())
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
let mut rule_to_codes: HashMap<&Path, Vec<(&Ident, &String, &Vec<Attribute>)>> = HashMap::new();
|
||||
let mut linter_code_for_rule_match_arms = quote!();
|
||||
|
@ -245,25 +234,6 @@ pub fn map_codes(func: &ItemFn) -> syn::Result<TokenStream> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct NoqaCode(&'static str, &'static str);
|
||||
|
||||
impl std::fmt::Display for NoqaCode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
use std::fmt::Write;
|
||||
write!(f, "{}{}", self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<&str> for NoqaCode {
|
||||
fn eq(&self, other: &&str) -> bool {
|
||||
match other.strip_prefix(self.0) {
|
||||
Some(suffix) => suffix == self.1,
|
||||
None => false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let mut linter_into_iter_match_arms = quote!();
|
||||
|
@ -295,12 +265,6 @@ pub fn map_codes(func: &ItemFn) -> syn::Result<TokenStream> {
|
|||
vec![ #(#all_codes,)* ].into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum FromCodeError {
|
||||
#[error("unknown rule code")]
|
||||
Unknown,
|
||||
}
|
||||
});
|
||||
|
||||
Ok(out)
|
||||
|
|
|
@ -66,12 +66,12 @@ pub fn expand<'a>(
|
|||
}
|
||||
|
||||
impl std::str::FromStr for #prefix_ident {
|
||||
type Err = FromCodeError;
|
||||
type Err = crate::registry::FromCodeError;
|
||||
|
||||
fn from_str(code: &str) -> Result<Self, Self::Err> {
|
||||
match code {
|
||||
#(#attributes #variant_strs => Ok(Self::#variant_idents),)*
|
||||
_ => Err(FromCodeError::Unknown)
|
||||
_ => Err(crate::registry::FromCodeError::Unknown)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue