Skip to content

Commit

Permalink
refactor(lint,actions): avoid duplicated filters (#4434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Oct 30, 2024
1 parent 40cecb8 commit d2b3b7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 54 deletions.
23 changes: 1 addition & 22 deletions crates/biome_configuration/src/analyzer/assists/actions.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 20 additions & 22 deletions crates/biome_service/src/file_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use biome_string_case::StrLikeExtension;
use grit::GritFileHandler;
use html::HtmlFileHandler;
pub use javascript::JsFormatterSettings;
use rustc_hash::FxHashSet;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::Path;
Expand Down Expand Up @@ -825,46 +826,43 @@ impl<'a> RegistryVisitor<GraphqlLanguage> for SyntaxVisitor<'a> {
///
#[derive(Debug)]
struct LintVisitor<'a, 'b> {
pub(crate) enabled_rules: Vec<RuleFilter<'a>>,
pub(crate) disabled_rules: Vec<RuleFilter<'a>>,
pub(crate) enabled_rules: FxHashSet<RuleFilter<'a>>,
pub(crate) disabled_rules: FxHashSet<RuleFilter<'a>>,
// lint_params: &'b LintParams<'a>,
only: &'b Vec<RuleSelector>,
skip: &'b Vec<RuleSelector>,
only: &'b [RuleSelector],
skip: &'b [RuleSelector],
settings: Option<&'b Settings>,
path: &'b Path,
}

impl<'a, 'b> LintVisitor<'a, 'b> {
pub(crate) fn new(
only: &'b Vec<RuleSelector>,
skip: &'b Vec<RuleSelector>,
only: &'b [RuleSelector],
skip: &'b [RuleSelector],
settings: Option<&'b Settings>,
path: &'b Path,
) -> Self {
Self {
enabled_rules: vec![],
disabled_rules: vec![],
enabled_rules: Default::default(),
disabled_rules: Default::default(),
only,
skip,
settings,
path,
}
}

fn finish(mut self) -> (Vec<RuleFilter<'a>>, Vec<RuleFilter<'a>>) {
fn finish(mut self) -> (FxHashSet<RuleFilter<'a>>, FxHashSet<RuleFilter<'a>>) {
let has_only_filter = !self.only.is_empty();
let enabled_rules = if !has_only_filter {
self.settings
if !has_only_filter {
let enabled_rules = self
.settings
.and_then(|settings| settings.as_linter_rules(self.path))
.as_ref()
.map(|rules| rules.as_enabled_rules())
.unwrap_or_default()
.into_iter()
.collect::<Vec<_>>()
} else {
vec![]
};
self.enabled_rules.extend(enabled_rules);
.unwrap_or_default();
self.enabled_rules.extend(enabled_rules);
}
(self.enabled_rules, self.disabled_rules)
}

Expand All @@ -878,13 +876,13 @@ impl<'a, 'b> LintVisitor<'a, 'b> {
for selector in self.only {
let filter = RuleFilter::from(selector);
if filter.match_rule::<R>() {
self.enabled_rules.push(filter)
self.enabled_rules.insert(filter);
}
}
for selector in self.skip {
let filter = RuleFilter::from(selector);
if filter.match_rule::<R>() {
self.disabled_rules.push(filter)
self.disabled_rules.insert(filter);
}
}
}
Expand Down Expand Up @@ -1171,8 +1169,8 @@ impl<'a, 'b> AnalyzerVisitorBuilder<'a, 'b> {
#[must_use]
pub(crate) fn with_linter_rules(
mut self,
only: &'b Vec<RuleSelector>,
skip: &'b Vec<RuleSelector>,
only: &'b [RuleSelector],
skip: &'b [RuleSelector],
path: &'b Path,
) -> Self {
self.lint = Some(LintVisitor::new(only, skip, self.settings, path));
Expand Down
11 changes: 1 addition & 10 deletions xtask/codegen/src/generate_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ fn generate_for_groups(
quote! {
if let Some(group) = self.#group_ident.as_ref() {
enabled_rules.extend(&group.get_enabled_rules());
disabled_rules.extend(&group.get_disabled_rules());
}
}
});
Expand Down Expand Up @@ -418,10 +417,8 @@ fn generate_for_groups(
/// The enabled rules are calculated from the difference with the disabled rules.
pub fn as_enabled_rules(&self) -> FxHashSet<RuleFilter<'static>> {
let mut enabled_rules = FxHashSet::default();
let mut disabled_rules = FxHashSet::default();
#( #group_as_default_rules )*

enabled_rules.difference(&disabled_rules).copied().collect()
enabled_rules
}
}

Expand Down Expand Up @@ -841,12 +838,6 @@ fn generate_group_struct(
index_set
}

pub(crate) fn get_disabled_rules(&self) -> FxHashSet<RuleFilter<'static>> {
let mut index_set = FxHashSet::default();
#( #rule_disabled_check_line )*
index_set
}

/// Checks if, given a rule name, matches one of the rules contained in this category
pub(crate) fn has_rule(rule_name: &str) -> Option<&'static str> {
Some(Self::GROUP_RULES[Self::GROUP_RULES.binary_search(&rule_name).ok()?])
Expand Down

0 comments on commit d2b3b7f

Please sign in to comment.