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 | |
parent | 4152367f5dee2a70ff49f4aff4040d5d433b8e44 (diff) |
implement lint_map
Diffstat (limited to 'lib')
-rw-r--r-- | lib/src/lib.rs | 69 | ||||
-rw-r--r-- | lib/src/lints.rs | 7 | ||||
-rw-r--r-- | lib/src/lints/bool_comparison.rs | 8 |
3 files changed, 57 insertions, 27 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 | } | ||
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index b0df71b..15d9063 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -1,2 +1,5 @@ | |||
1 | pub mod bool_comparison; | 1 | use crate::lint_map; |
2 | pub mod with_list; | 2 | |
3 | lint_map! { | ||
4 | bool_comparison, | ||
5 | } | ||
diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs index 4476b31..918126f 100644 --- a/lib/src/lints/bool_comparison.rs +++ b/lib/src/lints/bool_comparison.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use crate::{Diagnostic, Lint, Metadata, Rule}; | 1 | use crate::{Lint, Metadata, Report, Rule}; |
2 | 2 | ||
3 | use if_chain::if_chain; | 3 | use if_chain::if_chain; |
4 | use macros::lint; | 4 | use macros::lint; |
@@ -10,12 +10,12 @@ use rnix::{ | |||
10 | #[lint( | 10 | #[lint( |
11 | name = "bool_comparison", | 11 | name = "bool_comparison", |
12 | note = "Unnecessary comparison with boolean", | 12 | note = "Unnecessary comparison with boolean", |
13 | match_with = "SyntaxKind::NODE_BIN_OP" | 13 | match_with = SyntaxKind::NODE_BIN_OP |
14 | )] | 14 | )] |
15 | struct BoolComparison; | 15 | struct BoolComparison; |
16 | 16 | ||
17 | impl Rule for BoolComparison { | 17 | impl Rule for BoolComparison { |
18 | fn validate(&self, node: &SyntaxElement) -> Option<Diagnostic> { | 18 | fn validate(&self, node: &SyntaxElement) -> Option<Report> { |
19 | if_chain! { | 19 | if_chain! { |
20 | if let NodeOrToken::Node(bin_op_node) = node; | 20 | if let NodeOrToken::Node(bin_op_node) = node; |
21 | if let Some(bin_expr) = BinOp::cast(bin_op_node.clone()); | 21 | if let Some(bin_expr) = BinOp::cast(bin_op_node.clone()); |
@@ -37,7 +37,7 @@ impl Rule for BoolComparison { | |||
37 | non_bool_side, | 37 | non_bool_side, |
38 | bool_side | 38 | bool_side |
39 | ); | 39 | ); |
40 | dbg!(Some(Diagnostic::new(at, message))) | 40 | Some(Report::new().diagnostic(at, message)) |
41 | } else { | 41 | } else { |
42 | None | 42 | None |
43 | } | 43 | } |