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 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 21 deletions(-) (limited to 'lib/src/lib.rs') 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 + }; + } + } +} -- cgit v1.2.3