From 45ab8abf76aa8157b43f3c7675b11fb51fac66d9 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 15 Oct 2021 13:06:29 +0530 Subject: allow match_kind to accept multiple kinds --- lib/src/lib.rs | 12 +++++++----- macros/src/lib.rs | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4065345..4374ac7 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -112,7 +112,7 @@ pub trait Metadata { where Self: Sized; fn match_with(&self, with: &SyntaxKind) -> bool; - fn match_kind(&self) -> SyntaxKind; + fn match_kind(&self) -> Vec; } /// Combines Rule and Metadata, do not implement manually, this is derived by @@ -140,10 +140,12 @@ macro_rules! lint_map { $( { let temp_lint = &*$s::LINT; - let temp_match = temp_lint.match_kind(); - map.entry(temp_match) - .and_modify(|v: &mut Vec<_>| v.push(temp_lint)) - .or_insert_with(|| vec![temp_lint]); + let temp_matches = temp_lint.match_kind(); + for temp_match in temp_matches { + map.entry(temp_match) + .and_modify(|v: &mut Vec<_>| v.push(temp_lint)) + .or_insert_with(|| vec![temp_lint]); + } } )* map diff --git a/macros/src/lib.rs b/macros/src/lib.rs index c33fcba..127b4cb 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -139,6 +139,12 @@ fn generate_match_with_fn(meta: &LintMeta) -> TokenStream2 { #match_path == *with } } + } else if let syn::Expr::Array(array_expr) = match_with_lit { + quote! { + fn match_with(&self, with: &SyntaxKind) -> bool { + #array_expr.contains(with) + } + } } else { panic!("`match_with` has non-path value") } @@ -151,8 +157,14 @@ fn generate_match_kind(meta: &LintMeta) -> TokenStream2 { .unwrap_or_else(|| panic!("`match_with` not present")); if let syn::Expr::Path(match_path) = match_with_lit { quote! { - fn match_kind(&self) -> SyntaxKind { - #match_path + fn match_kind(&self) -> Vec { + vec![#match_path] + } + } + } else if let syn::Expr::Array(array_expr) = match_with_lit { + quote! { + fn match_kind(&self) -> Vec { + #array_expr.to_vec() } } } else { -- cgit v1.2.3