From 8f1460eba3e6c08b4a20c135a2189468d763df08 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Tue, 29 Oct 2024 01:28:21 +0000 Subject: [PATCH] refactor(linter): move `LintPlugins` from `LintOptions` to `LintConfig` (#6932) Pure refactor. Moves plugin settings from linter options (which control the linter's behavior on a global level) and linter config ("which may or may not get adjusted on each file). --- crates/oxc_linter/src/builder.rs | 31 ++++++++++--------- crates/oxc_linter/src/config/mod.rs | 10 +++++- crates/oxc_linter/src/config/oxlintrc.rs | 6 ++-- .../src/{options => config}/plugins.rs | 0 crates/oxc_linter/src/context/host.rs | 18 ++++++++--- crates/oxc_linter/src/lib.rs | 8 ++--- crates/oxc_linter/src/options/filter.rs | 3 +- crates/oxc_linter/src/options/mod.rs | 3 -- crates/oxc_linter/src/tester.rs | 2 +- crates/oxc_linter/src/utils/jest.rs | 11 +++++-- 10 files changed, 58 insertions(+), 34 deletions(-) rename crates/oxc_linter/src/{options => config}/plugins.rs (100%) diff --git a/crates/oxc_linter/src/builder.rs b/crates/oxc_linter/src/builder.rs index 89b2669c17ca5..0ca05e142b763 100644 --- a/crates/oxc_linter/src/builder.rs +++ b/crates/oxc_linter/src/builder.rs @@ -7,8 +7,7 @@ use oxc_span::CompactStr; use rustc_hash::FxHashSet; use crate::{ - config::{ESLintRule, OxlintRules}, - options::LintPlugins, + config::{ESLintRule, LintPlugins, OxlintRules}, rules::RULES, AllowWarnDeny, FixKind, FrameworkFlags, LintConfig, LintFilter, LintFilterKind, LintOptions, Linter, Oxlintrc, RuleCategory, RuleEnum, RuleWithSeverity, @@ -35,8 +34,11 @@ impl LinterBuilder { /// You can think of this as `oxlint -A all`. pub fn empty() -> Self { let options = LintOptions::default(); - let cache = RulesCache::new(options.plugins); - Self { rules: FxHashSet::default(), options, config: LintConfig::default(), cache } + let config = LintConfig::default(); + let rules = FxHashSet::default(); + let cache = RulesCache::new(config.plugins); + + Self { rules, options, config, cache } } /// Warn on all rules in all plugins and categories, including those in `nursery`. @@ -44,15 +46,16 @@ impl LinterBuilder { /// /// You can think of this as `oxlint -W all -W nursery`. pub fn all() -> Self { - let options = LintOptions { plugins: LintPlugins::all(), ..LintOptions::default() }; - let cache = RulesCache::new(options.plugins); + let options = LintOptions::default(); + let config = LintConfig { plugins: LintPlugins::all(), ..LintConfig::default() }; + let cache = RulesCache::new(config.plugins); Self { rules: RULES .iter() .map(|rule| RuleWithSeverity { rule: rule.clone(), severity: AllowWarnDeny::Warn }) .collect(), options, - config: LintConfig::default(), + config, cache, } } @@ -76,11 +79,11 @@ impl LinterBuilder { let Oxlintrc { plugins, settings, env, globals, categories, rules: oxlintrc_rules } = oxlintrc; - let config = LintConfig { settings, env, globals }; - let options = LintOptions { plugins, ..Default::default() }; + let config = LintConfig { plugins, settings, env, globals }; + let options = LintOptions::default(); let rules = if start_empty { FxHashSet::default() } else { Self::warn_correctness(plugins) }; - let cache = RulesCache::new(options.plugins); + let cache = RulesCache::new(config.plugins); let mut builder = Self { rules, options, config, cache }; if !categories.is_empty() { @@ -128,7 +131,7 @@ impl LinterBuilder { /// [`and_plugins`]: LinterBuilder::and_plugins #[inline] pub fn with_plugins(mut self, plugins: LintPlugins) -> Self { - self.options.plugins = plugins; + self.config.plugins = plugins; self.cache.set_plugins(plugins); self } @@ -139,14 +142,14 @@ impl LinterBuilder { /// rules. #[inline] pub fn and_plugins(mut self, plugins: LintPlugins, enabled: bool) -> Self { - self.options.plugins.set(plugins, enabled); - self.cache.set_plugins(self.options.plugins); + self.config.plugins.set(plugins, enabled); + self.cache.set_plugins(self.config.plugins); self } #[inline] pub fn plugins(&self) -> LintPlugins { - self.options.plugins + self.config.plugins } #[cfg(test)] diff --git a/crates/oxc_linter/src/config/mod.rs b/crates/oxc_linter/src/config/mod.rs index 67f86fd1966b6..f0c87860e11b5 100644 --- a/crates/oxc_linter/src/config/mod.rs +++ b/crates/oxc_linter/src/config/mod.rs @@ -2,6 +2,7 @@ mod categories; mod env; mod globals; mod oxlintrc; +mod plugins; mod rules; mod settings; @@ -9,6 +10,7 @@ pub use self::{ env::OxlintEnv, globals::OxlintGlobals, oxlintrc::Oxlintrc, + plugins::LintPlugins, rules::ESLintRule, rules::OxlintRules, settings::{jsdoc::JSDocPluginSettings, OxlintSettings}, @@ -16,6 +18,7 @@ pub use self::{ #[derive(Debug, Default)] pub(crate) struct LintConfig { + pub(crate) plugins: LintPlugins, pub(crate) settings: OxlintSettings, /// Environments enable and disable collections of global variables. pub(crate) env: OxlintEnv, @@ -25,7 +28,12 @@ pub(crate) struct LintConfig { impl From for LintConfig { fn from(config: Oxlintrc) -> Self { - Self { settings: config.settings, env: config.env, globals: config.globals } + Self { + plugins: config.plugins, + settings: config.settings, + env: config.env, + globals: config.globals, + } } } diff --git a/crates/oxc_linter/src/config/oxlintrc.rs b/crates/oxc_linter/src/config/oxlintrc.rs index db5676da1fbe1..21121a50fc238 100644 --- a/crates/oxc_linter/src/config/oxlintrc.rs +++ b/crates/oxc_linter/src/config/oxlintrc.rs @@ -5,11 +5,11 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use super::{ - categories::OxlintCategories, env::OxlintEnv, globals::OxlintGlobals, rules::OxlintRules, - settings::OxlintSettings, + categories::OxlintCategories, env::OxlintEnv, globals::OxlintGlobals, plugins::LintPlugins, + rules::OxlintRules, settings::OxlintSettings, }; -use crate::{options::LintPlugins, utils::read_to_string}; +use crate::utils::read_to_string; /// Oxlint Configuration File /// diff --git a/crates/oxc_linter/src/options/plugins.rs b/crates/oxc_linter/src/config/plugins.rs similarity index 100% rename from crates/oxc_linter/src/options/plugins.rs rename to crates/oxc_linter/src/config/plugins.rs diff --git a/crates/oxc_linter/src/context/host.rs b/crates/oxc_linter/src/context/host.rs index 12e1e74a85fa4..c164de28bb611 100644 --- a/crates/oxc_linter/src/context/host.rs +++ b/crates/oxc_linter/src/context/host.rs @@ -3,11 +3,11 @@ use oxc_span::SourceType; use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc}; use crate::{ - config::LintConfig, + config::{LintConfig, LintPlugins}, disable_directives::{DisableDirectives, DisableDirectivesBuilder}, fixer::{FixKind, Message}, frameworks, - options::{LintOptions, LintPlugins}, + options::LintOptions, utils, FrameworkFlags, RuleWithSeverity, }; @@ -68,6 +68,7 @@ impl<'a> ContextHost<'a> { file_path: P, semantic: Rc>, options: LintOptions, + config: Arc, ) -> Self { const DIAGNOSTICS_INITIAL_CAPACITY: usize = 512; @@ -82,6 +83,7 @@ impl<'a> ContextHost<'a> { DisableDirectivesBuilder::new().build(semantic.source_text(), semantic.comments()); let file_path = file_path.as_ref().to_path_buf().into_boxed_path(); + let plugins = config.plugins; Self { semantic, @@ -89,17 +91,25 @@ impl<'a> ContextHost<'a> { diagnostics: RefCell::new(Vec::with_capacity(DIAGNOSTICS_INITIAL_CAPACITY)), fix: options.fix, file_path, - config: Arc::new(LintConfig::default()), + config, frameworks: options.framework_hints, - plugins: options.plugins, + plugins, } .sniff_for_frameworks() } /// Set the linter configuration for this context. #[inline] + #[allow(dead_code)] // will be used in up-stack PR pub fn with_config(mut self, config: &Arc) -> Self { + let plugins = config.plugins; self.config = Arc::clone(config); + + if self.plugins != plugins { + self.plugins = plugins; + return self.sniff_for_frameworks(); + } + self } diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index e8a590e605edc..03cf7542f6b7f 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -31,11 +31,11 @@ use utils::iter_possible_jest_call_node; pub use crate::{ builder::LinterBuilder, - config::Oxlintrc, + config::{LintPlugins, Oxlintrc}, context::LintContext, fixer::FixKind, frameworks::FrameworkFlags, - options::{AllowWarnDeny, InvalidFilterKind, LintFilter, LintFilterKind, LintPlugins}, + options::{AllowWarnDeny, InvalidFilterKind, LintFilter, LintFilterKind}, rule::{RuleCategory, RuleFixMeta, RuleMeta, RuleWithSeverity}, service::{LintService, LintServiceOptions}, }; @@ -115,7 +115,7 @@ impl Linter { pub fn run<'a>(&self, path: &Path, semantic: Rc>) -> Vec> { let ctx_host = - Rc::new(ContextHost::new(path, semantic, self.options).with_config(&self.config)); + Rc::new(ContextHost::new(path, semantic, self.options, Arc::clone(&self.config))); let rules = self .rules @@ -126,7 +126,7 @@ impl Linter { let semantic = ctx_host.semantic(); let should_run_on_jest_node = - self.options.plugins.has_test() && ctx_host.frameworks().is_test(); + self.config.plugins.has_test() && ctx_host.frameworks().is_test(); // IMPORTANT: We have two branches here for performance reasons: // diff --git a/crates/oxc_linter/src/options/filter.rs b/crates/oxc_linter/src/options/filter.rs index b09f4df0a3e72..6ac054ba2287f 100644 --- a/crates/oxc_linter/src/options/filter.rs +++ b/crates/oxc_linter/src/options/filter.rs @@ -1,6 +1,7 @@ use crate::RuleCategory; -use super::{plugins::LintPlugins, AllowWarnDeny}; +use super::AllowWarnDeny; +use crate::LintPlugins; use std::{borrow::Cow, fmt}; /// Enables, disables, and sets the severity of lint rules. diff --git a/crates/oxc_linter/src/options/mod.rs b/crates/oxc_linter/src/options/mod.rs index 4ce020dc5f984..7012ac83995f5 100644 --- a/crates/oxc_linter/src/options/mod.rs +++ b/crates/oxc_linter/src/options/mod.rs @@ -1,10 +1,8 @@ mod allow_warn_deny; mod filter; -mod plugins; pub use allow_warn_deny::AllowWarnDeny; pub use filter::{InvalidFilterKind, LintFilter, LintFilterKind}; -pub use plugins::LintPlugins; use crate::{fixer::FixKind, FrameworkFlags}; @@ -14,5 +12,4 @@ use crate::{fixer::FixKind, FrameworkFlags}; pub(crate) struct LintOptions { pub fix: FixKind, pub framework_hints: FrameworkFlags, - pub plugins: LintPlugins, } diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 981a07e3647c8..8403dd394b44d 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -10,7 +10,7 @@ use serde::Deserialize; use serde_json::Value; use crate::{ - fixer::FixKind, options::LintPlugins, rules::RULES, AllowWarnDeny, Fixer, LintService, + fixer::FixKind, rules::RULES, AllowWarnDeny, Fixer, LintPlugins, LintService, LintServiceOptions, LinterBuilder, Oxlintrc, RuleEnum, RuleWithSeverity, }; diff --git a/crates/oxc_linter/src/utils/jest.rs b/crates/oxc_linter/src/utils/jest.rs index 4710c9a6476a8..8a16b773f9a41 100644 --- a/crates/oxc_linter/src/utils/jest.rs +++ b/crates/oxc_linter/src/utils/jest.rs @@ -303,7 +303,7 @@ pub fn is_equality_matcher(matcher: &KnownMemberExpressionProperty) -> bool { #[cfg(test)] mod test { - use std::rc::Rc; + use std::{rc::Rc, sync::Arc}; use oxc_allocator::Allocator; use oxc_parser::Parser; @@ -322,8 +322,13 @@ mod test { let semantic_ret = Rc::new(semantic_ret); let build_ctx = |path: &'static str| { - Rc::new(ContextHost::new(path, Rc::clone(&semantic_ret), LintOptions::default())) - .spawn_for_test() + Rc::new(ContextHost::new( + path, + Rc::clone(&semantic_ret), + LintOptions::default(), + Arc::default(), + )) + .spawn_for_test() }; let ctx = build_ctx("foo.js");