From 6a03775c4930dc458554aa5b7d0be92f482ae966 Mon Sep 17 00:00:00 2001 From: Wenzhe Wang Date: Tue, 7 Nov 2023 10:48:16 +0800 Subject: [PATCH] chore(benchmark): reopen jest and jsx-a11y for benchmark (#1169) --- crates/oxc_linter/src/options.rs | 26 ++++++++++++++------------ tasks/benchmark/benches/linter.rs | 6 ++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/oxc_linter/src/options.rs b/crates/oxc_linter/src/options.rs index 6d38ecdb0..dc6b78b3c 100644 --- a/crates/oxc_linter/src/options.rs +++ b/crates/oxc_linter/src/options.rs @@ -89,6 +89,7 @@ const JSX_A11Y_PLUGIN_NAME: &str = "jsx_a11y"; impl LintOptions { pub fn derive_rules(&self) -> Vec { let mut rules: FxHashSet = FxHashSet::default(); + let all_rules = self.get_filtered_rules(); for (allow_warn_deny, name_or_category) in &self.filter { let maybe_category = RuleCategory::from(name_or_category.as_str()); @@ -96,14 +97,14 @@ impl LintOptions { AllowWarnDeny::Deny => { match maybe_category { Some(category) => rules.extend( - RULES.iter().filter(|rule| rule.category() == category).cloned(), + all_rules.iter().filter(|rule| rule.category() == category).cloned(), ), None => { if name_or_category == "all" { - rules.extend(RULES.iter().cloned()); + rules.extend(all_rules.iter().cloned()); } else { rules.extend( - RULES + all_rules .iter() .filter(|rule| rule.name() == name_or_category) .cloned(), @@ -127,24 +128,25 @@ impl LintOptions { } } - self.extends_or_exclude_plugins(&mut rules); - let mut rules = rules.into_iter().collect::>(); // for stable diagnostics output ordering rules.sort_unstable_by_key(RuleEnum::name); rules } - fn extends_or_exclude_plugins(&self, rules: &mut FxHashSet) { - let mut extends_or_exclude = |yes: bool, name: &str| { - if yes { - rules.extend(RULES.iter().filter(|rule| rule.plugin_name() == name).cloned()); - } else { + // get final filtered rules by reading `self.jest_plugin` and `self.jsx_a11y_plugin` + fn get_filtered_rules(&self) -> Vec { + let mut rules = RULES.clone(); + + let mut may_exclude_plugin_rules = |yes: bool, name: &str| { + if !yes { rules.retain(|rule| rule.plugin_name() != name); } }; - extends_or_exclude(self.jest_plugin, JEST_PLUGIN_NAME); - extends_or_exclude(self.jsx_a11y_plugin, JSX_A11Y_PLUGIN_NAME); + may_exclude_plugin_rules(self.jest_plugin, JEST_PLUGIN_NAME); + may_exclude_plugin_rules(self.jsx_a11y_plugin, JSX_A11Y_PLUGIN_NAME); + + rules } } diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index ec06355d0..a21d12a2a 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -31,8 +31,10 @@ fn bench_linter(criterion: &mut Criterion) { .with_trivias(ret.trivias) .build_module_record(PathBuf::new(), program) .build(program); - let lint_options = - LintOptions::default().with_filter(vec![(AllowWarnDeny::Deny, "all".into())]); + let lint_options = LintOptions::default() + .with_filter(vec![(AllowWarnDeny::Deny, "all".into())]) + .with_jest_plugin(true) + .with_jsx_a11y_plugin(true); let linter = Linter::from_options(lint_options); let semantic = Rc::new(semantic_ret.semantic); b.iter(|| {