Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jul 22, 2024
1 parent df13ff1 commit 3be9ff1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
14 changes: 9 additions & 5 deletions examples/dlint/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,7 +32,8 @@ pub struct Config {

impl Config {
pub fn get_rules(&self) -> Vec<Box<dyn LintRule>> {
get_filtered_rules(
filtered_rules(
get_all_rules(),
Some(self.rules.tags.clone()),
Some(self.rules.exclude.clone()),
Some(self.rules.include.clone()),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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`
Expand Down
15 changes: 8 additions & 7 deletions examples/dlint/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -67,13 +67,13 @@ mod lint_tests {
}

fn lint_recommended_rules(source: &str) -> Vec<LintDiagnostic> {
lint(source, get_recommended_rules())
lint(source, recommended_rules(get_all_rules()))
}

fn lint_recommended_rules_with_ast(
parsed_source: &ParsedSource,
) -> Vec<LintDiagnostic> {
lint_with_ast(parsed_source, get_recommended_rules())
lint_with_ast(parsed_source, recommended_rules(get_all_rules()))
}

fn lint_specified_rule(
Expand Down
49 changes: 31 additions & 18 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ pub fn get_all_rules() -> Vec<Box<dyn LintRule>> {
get_all_rules_raw()
}

pub fn get_recommended_rules() -> Vec<Box<dyn LintRule>> {
get_all_rules_raw()
/// Filters the lint rules to only the recommended rules.
pub fn recommended_rules(
all_rules: Vec<Box<dyn LintRule>>,
) -> Vec<Box<dyn LintRule>> {
all_rules
.into_iter()
.filter(|r| r.tags().contains(&"recommended"))
.collect()
Expand All @@ -175,15 +178,16 @@ pub fn get_recommended_rules() -> Vec<Box<dyn LintRule>> {
/// to the return list
///
/// Before returning the list will sorted alphabetically.
pub fn get_filtered_rules(
pub fn filtered_rules(
all_rules: Vec<Box<dyn LintRule>>,
maybe_tags: Option<Vec<String>>,
maybe_exclude: Option<Vec<String>>,
maybe_include: Option<Vec<String>>,
) -> Vec<Box<dyn LintRule>> {
let tags_set =
maybe_tags.map(|tags| tags.into_iter().collect::<HashSet<_>>());

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 {
Expand Down Expand Up @@ -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());
}
Expand All @@ -372,50 +376,59 @@ 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()]),
);
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()]),
);
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(),
Expand All @@ -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);
Expand Down

0 comments on commit 3be9ff1

Please sign in to comment.