From 2ee7db0b3b6df787c7e62d3614f97a0e45d12a12 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 15 Sep 2021 16:19:43 +0530 Subject: implement lint_map --- lib/src/lib.rs | 69 ++++++++++++++++++++++++++++------------ lib/src/lints.rs | 7 ++-- lib/src/lints/bool_comparison.rs | 8 ++--- 3 files changed, 57 insertions(+), 27 deletions(-) (limited to 'lib/src') 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 @@ -pub mod lints; +mod lints; +pub use lints::LINTS; use rnix::{SyntaxElement, SyntaxKind, TextRange}; -use std::ops::Deref; +use std::default::Default; pub trait Rule { - fn validate(&self, node: &SyntaxElement) -> Option; + fn validate(&self, node: &SyntaxElement) -> Option; } #[derive(Debug)] @@ -19,29 +20,55 @@ impl Diagnostic { } } +#[derive(Debug, Default)] +pub struct Report { + pub diagnostics: Vec, +} + +impl Report { + pub fn new() -> Self { + Self::default() + } + pub fn diagnostic(mut self, at: TextRange, message: String) -> Self { + self.diagnostics.push(Diagnostic::new(at, message)); + self + } +} + pub trait Metadata { fn name(&self) -> &str; fn note(&self) -> &str; fn match_with(&self, with: &SyntaxKind) -> bool; + fn match_kind(&self) -> SyntaxKind; } pub trait Lint: Metadata + Rule + Send + Sync {} -// #[macro_export] -// macro_rules! lint_map { -// ($($s:ident),*,) => { -// lint_map($($s),*); -// } -// ($($s:ident),*) => { -// use ::std::collections::HashMap; -// use rnix::SyntaxKind; -// $( -// mod $s; -// )* -// ::lazy_static::lazy_static! { -// pub static ref RULES: HashMap> = { -// vec![$(&*$s::LINT,)*] -// } -// } -// } -// } +#[macro_export] +macro_rules! lint_map { + ($($s:ident),*,) => { + lint_map!($($s),*); + }; + ($($s:ident),*) => { + use ::std::collections::HashMap; + use ::rnix::SyntaxKind; + $( + mod $s; + )* + ::lazy_static::lazy_static! { + pub static ref LINTS: HashMap>> = { + let mut map = HashMap::new(); + $( + { + 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]); + } + )* + map + }; + } + } +} 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 @@ -pub mod bool_comparison; -pub mod with_list; +use crate::lint_map; + +lint_map! { + bool_comparison, +} 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 @@ -use crate::{Diagnostic, Lint, Metadata, Rule}; +use crate::{Lint, Metadata, Report, Rule}; use if_chain::if_chain; use macros::lint; @@ -10,12 +10,12 @@ use rnix::{ #[lint( name = "bool_comparison", note = "Unnecessary comparison with boolean", - match_with = "SyntaxKind::NODE_BIN_OP" + match_with = SyntaxKind::NODE_BIN_OP )] struct BoolComparison; impl Rule for BoolComparison { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement) -> Option { if_chain! { if let NodeOrToken::Node(bin_op_node) = node; if let Some(bin_expr) = BinOp::cast(bin_op_node.clone()); @@ -37,7 +37,7 @@ impl Rule for BoolComparison { non_bool_side, bool_side ); - dbg!(Some(Diagnostic::new(at, message))) + Some(Report::new().diagnostic(at, message)) } else { None } -- cgit v1.2.3