diff options
author | Akshay <[email protected]> | 2021-10-15 08:36:29 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-15 08:36:29 +0100 |
commit | 45ab8abf76aa8157b43f3c7675b11fb51fac66d9 (patch) | |
tree | 6a2d7f6e52069b69e303381e9f89a32950d41a3b | |
parent | c6ea58410cb6145cf2791f0ae0d1918cf0d5bc62 (diff) |
allow match_kind to accept multiple kinds
-rw-r--r-- | lib/src/lib.rs | 12 | ||||
-rw-r--r-- | 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 { | |||
112 | where | 112 | where |
113 | Self: Sized; | 113 | Self: Sized; |
114 | fn match_with(&self, with: &SyntaxKind) -> bool; | 114 | fn match_with(&self, with: &SyntaxKind) -> bool; |
115 | fn match_kind(&self) -> SyntaxKind; | 115 | fn match_kind(&self) -> Vec<SyntaxKind>; |
116 | } | 116 | } |
117 | 117 | ||
118 | /// Combines Rule and Metadata, do not implement manually, this is derived by | 118 | /// Combines Rule and Metadata, do not implement manually, this is derived by |
@@ -140,10 +140,12 @@ macro_rules! lint_map { | |||
140 | $( | 140 | $( |
141 | { | 141 | { |
142 | let temp_lint = &*$s::LINT; | 142 | let temp_lint = &*$s::LINT; |
143 | let temp_match = temp_lint.match_kind(); | 143 | let temp_matches = temp_lint.match_kind(); |
144 | map.entry(temp_match) | 144 | for temp_match in temp_matches { |
145 | .and_modify(|v: &mut Vec<_>| v.push(temp_lint)) | 145 | map.entry(temp_match) |
146 | .or_insert_with(|| vec![temp_lint]); | 146 | .and_modify(|v: &mut Vec<_>| v.push(temp_lint)) |
147 | .or_insert_with(|| vec![temp_lint]); | ||
148 | } | ||
147 | } | 149 | } |
148 | )* | 150 | )* |
149 | map | 151 | 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 { | |||
139 | #match_path == *with | 139 | #match_path == *with |
140 | } | 140 | } |
141 | } | 141 | } |
142 | } else if let syn::Expr::Array(array_expr) = match_with_lit { | ||
143 | quote! { | ||
144 | fn match_with(&self, with: &SyntaxKind) -> bool { | ||
145 | #array_expr.contains(with) | ||
146 | } | ||
147 | } | ||
142 | } else { | 148 | } else { |
143 | panic!("`match_with` has non-path value") | 149 | panic!("`match_with` has non-path value") |
144 | } | 150 | } |
@@ -151,8 +157,14 @@ fn generate_match_kind(meta: &LintMeta) -> TokenStream2 { | |||
151 | .unwrap_or_else(|| panic!("`match_with` not present")); | 157 | .unwrap_or_else(|| panic!("`match_with` not present")); |
152 | if let syn::Expr::Path(match_path) = match_with_lit { | 158 | if let syn::Expr::Path(match_path) = match_with_lit { |
153 | quote! { | 159 | quote! { |
154 | fn match_kind(&self) -> SyntaxKind { | 160 | fn match_kind(&self) -> Vec<SyntaxKind> { |
155 | #match_path | 161 | vec![#match_path] |
162 | } | ||
163 | } | ||
164 | } else if let syn::Expr::Array(array_expr) = match_with_lit { | ||
165 | quote! { | ||
166 | fn match_kind(&self) -> Vec<SyntaxKind> { | ||
167 | #array_expr.to_vec() | ||
156 | } | 168 | } |
157 | } | 169 | } |
158 | } else { | 170 | } else { |