From 3be9ff1b41522de0d636a8df732b2040c3e2d0c8 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 22 Jul 2024 16:52:22 -0400 Subject: [PATCH] update --- examples/dlint/config.rs | 14 ++++++++---- examples/dlint/main.rs | 15 ++++++------ src/lib.rs | 6 ++--- src/rules.rs | 49 +++++++++++++++++++++++++--------------- 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/examples/dlint/config.rs b/examples/dlint/config.rs index ae8896a5..e6c2a480 100644 --- a/examples/dlint/config.rs +++ b/examples/dlint/config.rs @@ -2,7 +2,8 @@ use anyhow::bail; use anyhow::Error as AnyError; -use deno_lint::rules::{get_filtered_rules, LintRule}; +use deno_lint::rules::get_all_rules; +use deno_lint::rules::{filtered_rules, LintRule}; use serde::Deserialize; use std::path::Path; use std::path::PathBuf; @@ -31,7 +32,8 @@ pub struct Config { impl Config { pub fn get_rules(&self) -> Vec> { - get_filtered_rules( + filtered_rules( + get_all_rules(), Some(self.rules.tags.clone()), Some(self.rules.exclude.clone()), Some(self.rules.include.clone()), @@ -144,7 +146,7 @@ fn glob( #[cfg(test)] mod tests { use super::*; - use deno_lint::rules::get_recommended_rules; + use deno_lint::rules::recommended_rules; use std::collections::HashSet; macro_rules! svec { @@ -182,7 +184,8 @@ mod tests { }, ..Default::default() }; - let recommended_rules_codes = into_codes(get_recommended_rules()); + let recommended_rules_codes = + into_codes(recommended_rules(get_all_rules())); assert_eq!(into_codes(config.get_rules()), recommended_rules_codes); // even if "recommended" is specified in `tags` and `include` contains a rule @@ -196,7 +199,8 @@ mod tests { }, ..Default::default() }; - let recommended_rules_codes = into_codes(get_recommended_rules()); + let recommended_rules_codes = + into_codes(recommended_rules(get_all_rules())); assert_eq!(into_codes(config.get_rules()), recommended_rules_codes); // `exclude` has higher precedence over `include` diff --git a/examples/dlint/main.rs b/examples/dlint/main.rs index 5c892a21..c1969efa 100644 --- a/examples/dlint/main.rs +++ b/examples/dlint/main.rs @@ -11,7 +11,8 @@ use deno_ast::ModuleSpecifier; use deno_lint::linter::LintConfig; use deno_lint::linter::LintFileOptions; use deno_lint::linter::LinterBuilder; -use deno_lint::rules::{get_filtered_rules, get_recommended_rules}; +use deno_lint::rules::get_all_rules; +use deno_lint::rules::{filtered_rules, recommended_rules}; use log::debug; use rayon::prelude::*; use std::collections::BTreeMap; @@ -89,19 +90,19 @@ fn run_linter( config.get_rules() } else if let Some(rule_name) = filter_rule_name { let include = vec![rule_name.to_string()]; - get_filtered_rules(Some(vec![]), None, Some(include)) + filtered_rules(get_all_rules(), Some(vec![]), None, Some(include)) } else { - get_recommended_rules() + recommended_rules(get_all_rules()) }; - let file_diagnostics = Arc::new(Mutex::new(BTreeMap::new())); - let linter_builder = LinterBuilder::default().rules(rules.clone()); - - let linter = linter_builder.build(); if rules.is_empty() { bail!("No lint rules configured"); } else { debug!("Configured rules: {}", rules.len()); } + let file_diagnostics = Arc::new(Mutex::new(BTreeMap::new())); + let linter_builder = LinterBuilder::default().rules(rules); + + let linter = linter_builder.build(); paths .par_iter() diff --git a/src/lib.rs b/src/lib.rs index 58d869fd..280fbab9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,7 +30,7 @@ pub use deno_ast::view::ProgramRef; mod lint_tests { use crate::diagnostic::LintDiagnostic; use crate::linter::*; - use crate::rules::{get_recommended_rules, LintRule}; + use crate::rules::{get_all_rules, recommended_rules, LintRule}; use crate::test_util::{assert_diagnostic, parse}; use deno_ast::ParsedSource; use deno_ast::{MediaType, ModuleSpecifier}; @@ -67,13 +67,13 @@ mod lint_tests { } fn lint_recommended_rules(source: &str) -> Vec { - lint(source, get_recommended_rules()) + lint(source, recommended_rules(get_all_rules())) } fn lint_recommended_rules_with_ast( parsed_source: &ParsedSource, ) -> Vec { - lint_with_ast(parsed_source, get_recommended_rules()) + lint_with_ast(parsed_source, recommended_rules(get_all_rules())) } fn lint_specified_rule( diff --git a/src/rules.rs b/src/rules.rs index beb41395..33f80cd5 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -153,8 +153,11 @@ pub fn get_all_rules() -> Vec> { get_all_rules_raw() } -pub fn get_recommended_rules() -> Vec> { - get_all_rules_raw() +/// Filters the lint rules to only the recommended rules. +pub fn recommended_rules( + all_rules: Vec>, +) -> Vec> { + all_rules .into_iter() .filter(|r| r.tags().contains(&"recommended")) .collect() @@ -175,7 +178,8 @@ pub fn get_recommended_rules() -> Vec> { /// to the return list /// /// Before returning the list will sorted alphabetically. -pub fn get_filtered_rules( +pub fn filtered_rules( + all_rules: Vec>, maybe_tags: Option>, maybe_exclude: Option>, maybe_include: Option>, @@ -183,7 +187,7 @@ pub fn get_filtered_rules( let tags_set = maybe_tags.map(|tags| tags.into_iter().collect::>()); - let mut rules = get_all_rules_raw() + let mut rules = all_rules .into_iter() .filter(|rule| { let mut passes = if let Some(tags_set) = &tags_set { @@ -349,12 +353,12 @@ mod tests { #[test] fn recommended_rules_sorted_alphabetically() { - let mut sorted_recommended_rules = get_recommended_rules(); + let mut sorted_recommended_rules = recommended_rules(get_all_rules()); sorted_recommended_rules.sort_by_key(|r| r.code()); for (sorted, unsorted) in sorted_recommended_rules .iter() - .zip(get_recommended_rules().iter()) + .zip(recommended_rules(get_all_rules()).iter()) { assert_eq!(sorted.code(), unsorted.code()); } @@ -372,34 +376,41 @@ mod tests { #[test] fn test_get_filtered_rules() { // Should return recommended rules when given `recommended` tag. - let rules = - get_filtered_rules(Some(vec!["recommended".to_string()]), None, None); - for (r, rr) in rules.iter().zip(get_recommended_rules().iter()) { + let rules = filtered_rules( + get_all_rules(), + Some(vec!["recommended".to_string()]), + None, + None, + ); + for (r, rr) in rules.iter().zip(recommended_rules(get_all_rules()).iter()) { assert_eq!(r.code(), rr.code()); } // Should allow to add more rules to recommended rules. - let rules = get_filtered_rules( + let rules = filtered_rules( + get_all_rules(), Some(vec!["recommended".to_string()]), None, Some(vec!["ban-untagged-todo".to_string()]), ); - assert_eq!(rules.len(), get_recommended_rules().len() + 1); + assert_eq!(rules.len(), recommended_rules(get_all_rules()).len() + 1); // Recommended should allow to exclude some recommended rules and include more on top. - let rules = get_filtered_rules( + let rules = filtered_rules( + get_all_rules(), Some(vec!["recommended".to_string()]), Some(vec!["ban-ts-comment".to_string()]), Some(vec!["ban-untagged-todo".to_string()]), ); - assert_eq!(rules.len(), get_recommended_rules().len()); + assert_eq!(rules.len(), recommended_rules(get_all_rules()).len()); // Should skip all rules if given empty tags vec. - let rules = get_filtered_rules(Some(vec![]), None, None); + let rules = filtered_rules(get_all_rules(), Some(vec![]), None, None); assert!(rules.is_empty()); // Should still allow to include rules when passed empty tags vec. - let rules = get_filtered_rules( + let rules = filtered_rules( + get_all_rules(), Some(vec![]), None, Some(vec!["ban-untagged-todo".to_string()]), @@ -407,7 +418,8 @@ mod tests { assert_eq!(rules.len(), 1); // Excluded rules should have priority over included rules. - let rules = get_filtered_rules( + let rules = filtered_rules( + get_all_rules(), Some(vec![]), Some(vec!["ban-untagged-todo".to_string()]), Some(vec!["ban-untagged-todo".to_string()]), @@ -415,7 +427,8 @@ mod tests { assert_eq!(rules.len(), 0); // Should still allow to include other rules, when other duplicates are excluded. - let rules = get_filtered_rules( + let rules = filtered_rules( + get_all_rules(), Some(vec![]), Some(vec![ "ban-untagged-todo".to_string(), @@ -437,7 +450,7 @@ mod tests { fn ensure_lint_rules_are_sharable_across_threads() { use std::thread::spawn; - let rules = Arc::new(get_recommended_rules()); + let rules = Arc::new(recommended_rules(get_all_rules())); let handles = (0..2) .map(|_| { let rules = Arc::clone(&rules);