aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lib.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-09-15 11:49:43 +0100
committerAkshay <[email protected]>2021-09-15 11:49:43 +0100
commit2ee7db0b3b6df787c7e62d3614f97a0e45d12a12 (patch)
tree04e3aad2b98f8c55ca88d6b409cd74263410f2cc /lib/src/lib.rs
parent4152367f5dee2a70ff49f4aff4040d5d433b8e44 (diff)
implement lint_map
Diffstat (limited to 'lib/src/lib.rs')
-rw-r--r--lib/src/lib.rs69
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 @@
1pub mod lints; 1mod lints;
2pub use lints::LINTS;
2 3
3use rnix::{SyntaxElement, SyntaxKind, TextRange}; 4use rnix::{SyntaxElement, SyntaxKind, TextRange};
4use std::ops::Deref; 5use std::default::Default;
5 6
6pub trait Rule { 7pub 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)]
24pub struct Report {
25 pub diagnostics: Vec<Diagnostic>,
26}
27
28impl 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
22pub trait Metadata { 38pub 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
28pub trait Lint: Metadata + Rule + Send + Sync {} 45pub trait Lint: Metadata + Rule + Send + Sync {}
29 46
30// #[macro_export] 47#[macro_export]
31// macro_rules! lint_map { 48macro_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}