diff options
author | Akshay <[email protected]> | 2021-09-15 11:49:43 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-09-15 11:49:43 +0100 |
commit | 2ee7db0b3b6df787c7e62d3614f97a0e45d12a12 (patch) | |
tree | 04e3aad2b98f8c55ca88d6b409cd74263410f2cc /lib/src/lib.rs | |
parent | 4152367f5dee2a70ff49f4aff4040d5d433b8e44 (diff) |
implement lint_map
Diffstat (limited to 'lib/src/lib.rs')
-rw-r--r-- | lib/src/lib.rs | 69 |
1 files changed, 48 insertions, 21 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 537f4b3..c06f02d 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | pub mod lints; | 1 | mod lints; |
2 | pub use lints::LINTS; | ||
2 | 3 | ||
3 | use rnix::{SyntaxElement, SyntaxKind, TextRange}; | 4 | use rnix::{SyntaxElement, SyntaxKind, TextRange}; |
4 | use std::ops::Deref; | 5 | use std::default::Default; |
5 | 6 | ||
6 | pub trait Rule { | 7 | pub trait Rule { |
7 | fn validate(&self, node: &SyntaxElement) -> Option<Diagnostic>; | 8 | fn validate(&self, node: &SyntaxElement) -> Option<Report>; |
8 | } | 9 | } |
9 | 10 | ||
10 | #[derive(Debug)] | 11 | #[derive(Debug)] |
@@ -19,29 +20,55 @@ impl Diagnostic { | |||
19 | } | 20 | } |
20 | } | 21 | } |
21 | 22 | ||
23 | #[derive(Debug, Default)] | ||
24 | pub struct Report { | ||
25 | pub diagnostics: Vec<Diagnostic>, | ||
26 | } | ||
27 | |||
28 | impl Report { | ||
29 | pub fn new() -> Self { | ||
30 | Self::default() | ||
31 | } | ||
32 | pub fn diagnostic(mut self, at: TextRange, message: String) -> Self { | ||
33 | self.diagnostics.push(Diagnostic::new(at, message)); | ||
34 | self | ||
35 | } | ||
36 | } | ||
37 | |||
22 | pub trait Metadata { | 38 | pub trait Metadata { |
23 | fn name(&self) -> &str; | 39 | fn name(&self) -> &str; |
24 | fn note(&self) -> &str; | 40 | fn note(&self) -> &str; |
25 | fn match_with(&self, with: &SyntaxKind) -> bool; | 41 | fn match_with(&self, with: &SyntaxKind) -> bool; |
42 | fn match_kind(&self) -> SyntaxKind; | ||
26 | } | 43 | } |
27 | 44 | ||
28 | pub trait Lint: Metadata + Rule + Send + Sync {} | 45 | pub trait Lint: Metadata + Rule + Send + Sync {} |
29 | 46 | ||
30 | // #[macro_export] | 47 | #[macro_export] |
31 | // macro_rules! lint_map { | 48 | macro_rules! lint_map { |
32 | // ($($s:ident),*,) => { | 49 | ($($s:ident),*,) => { |
33 | // lint_map($($s),*); | 50 | lint_map!($($s),*); |
34 | // } | 51 | }; |
35 | // ($($s:ident),*) => { | 52 | ($($s:ident),*) => { |
36 | // use ::std::collections::HashMap; | 53 | use ::std::collections::HashMap; |
37 | // use rnix::SyntaxKind; | 54 | use ::rnix::SyntaxKind; |
38 | // $( | 55 | $( |
39 | // mod $s; | 56 | mod $s; |
40 | // )* | 57 | )* |
41 | // ::lazy_static::lazy_static! { | 58 | ::lazy_static::lazy_static! { |
42 | // pub static ref RULES: HashMap<SyntaxKind, &'static Box<dyn $crate::Lint>> = { | 59 | pub static ref LINTS: HashMap<SyntaxKind, Vec<&'static Box<dyn $crate::Lint>>> = { |
43 | // vec![$(&*$s::LINT,)*] | 60 | let mut map = HashMap::new(); |
44 | // } | 61 | $( |
45 | // } | 62 | { |
46 | // } | 63 | let temp_lint = &*$s::LINT; |
47 | // } | 64 | let temp_match = temp_lint.match_kind(); |
65 | map.entry(temp_match) | ||
66 | .and_modify(|v: &mut Vec<_>| v.push(temp_lint)) | ||
67 | .or_insert_with(|| vec![temp_lint]); | ||
68 | } | ||
69 | )* | ||
70 | map | ||
71 | }; | ||
72 | } | ||
73 | } | ||
74 | } | ||