From 0b3c1d72a3ba9bd7dc92a5db56f30ace5a8cf8b6 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Mon, 9 Sep 2024 07:17:50 +0000 Subject: [PATCH] refactor(linter): start internal/external split of `OxlintConfig` (#5595) Re-creation of #5140 --- crates/oxc_linter/src/config/mod.rs | 17 ++++++++++++++++- crates/oxc_linter/src/context.rs | 22 +++++++++------------- crates/oxc_linter/src/lib.rs | 15 +++++++++------ crates/oxc_linter/src/options/mod.rs | 11 ++++++++--- crates/oxc_linter/src/tester.rs | 2 +- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/crates/oxc_linter/src/config/mod.rs b/crates/oxc_linter/src/config/mod.rs index 60bbc34052602..01858c8ddac76 100644 --- a/crates/oxc_linter/src/config/mod.rs +++ b/crates/oxc_linter/src/config/mod.rs @@ -57,7 +57,16 @@ use crate::{ #[serde(default)] pub struct OxlintConfig { /// See [Oxlint Rules](https://oxc.rs/docs/guide/usage/linter/rules.html). - pub(crate) rules: OxlintRules, + pub rules: OxlintRules, + pub settings: OxlintSettings, + /// Environments enable and disable collections of global variables. + pub env: OxlintEnv, + /// Enabled or disabled specific global variables. + pub globals: OxlintGlobals, +} + +#[derive(Debug, Default)] +pub(crate) struct LintConfig { pub(crate) settings: OxlintSettings, /// Environments enable and disable collections of global variables. pub(crate) env: OxlintEnv, @@ -65,6 +74,12 @@ pub struct OxlintConfig { pub(crate) globals: OxlintGlobals, } +impl From for LintConfig { + fn from(config: OxlintConfig) -> Self { + Self { settings: config.settings, env: config.env, globals: config.globals } + } +} + impl OxlintConfig { /// # Errors /// diff --git a/crates/oxc_linter/src/context.rs b/crates/oxc_linter/src/context.rs index 2acf631e493e0..65100736e964c 100644 --- a/crates/oxc_linter/src/context.rs +++ b/crates/oxc_linter/src/context.rs @@ -10,11 +10,11 @@ use oxc_syntax::module_record::ModuleRecord; #[cfg(debug_assertions)] use crate::rule::RuleFixMeta; use crate::{ - config::OxlintRules, + config::LintConfig, disable_directives::{DisableDirectives, DisableDirectivesBuilder}, fixer::{FixKind, Message, RuleFix, RuleFixer}, javascript_globals::GLOBALS, - AllowWarnDeny, FrameworkFlags, OxlintConfig, OxlintEnv, OxlintGlobals, OxlintSettings, + AllowWarnDeny, FrameworkFlags, OxlintEnv, OxlintGlobals, OxlintSettings, }; #[derive(Clone)] @@ -38,7 +38,7 @@ pub struct LintContext<'a> { file_path: Rc, - eslint_config: Arc, + config: Arc, // states current_plugin_name: &'static str, @@ -83,7 +83,7 @@ impl<'a> LintContext<'a> { disable_directives: Rc::new(disable_directives), fix: FixKind::None, file_path: file_path.into(), - eslint_config: Arc::new(OxlintConfig::default()), + config: Arc::new(LintConfig::default()), current_plugin_name: "eslint", current_plugin_prefix: "eslint", current_rule_name: "", @@ -100,8 +100,8 @@ impl<'a> LintContext<'a> { self } - pub fn with_eslint_config(mut self, eslint_config: &Arc) -> Self { - self.eslint_config = Arc::clone(eslint_config); + pub(crate) fn with_config(mut self, config: &Arc) -> Self { + self.config = Arc::clone(config); self } @@ -176,22 +176,18 @@ impl<'a> LintContext<'a> { /// Plugin settings pub fn settings(&self) -> &OxlintSettings { - &self.eslint_config.settings + &self.config.settings } pub fn globals(&self) -> &OxlintGlobals { - &self.eslint_config.globals + &self.config.globals } /// Runtime environments turned on/off by the user. /// /// Examples of environments are `builtin`, `browser`, `node`, etc. pub fn env(&self) -> &OxlintEnv { - &self.eslint_config.env - } - - pub fn rules(&self) -> &OxlintRules { - &self.eslint_config.rules + &self.config.env } pub fn env_contains_var(&self, var: &str) -> bool { diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 20b790f62266e..56e698cc72752 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -22,6 +22,7 @@ pub mod table; use std::{io::Write, path::Path, rc::Rc, sync::Arc}; +use config::LintConfig; use oxc_diagnostics::Error; use oxc_semantic::{AstNode, Semantic}; @@ -54,7 +55,7 @@ fn size_asserts() { pub struct Linter { rules: Vec, options: LintOptions, - eslint_config: Arc, + config: Arc, } impl Default for Linter { @@ -68,8 +69,8 @@ impl Linter { /// /// Returns `Err` if there are any errors parsing the configuration file. pub fn from_options(options: LintOptions) -> Result { - let (rules, eslint_config) = options.derive_rules_and_config()?; - Ok(Self { rules, options, eslint_config: Arc::new(eslint_config) }) + let (rules, config) = options.derive_rules_and_config()?; + Ok(Self { rules, options, config: Arc::new(config) }) } #[cfg(test)] @@ -79,9 +80,11 @@ impl Linter { self } + /// Used for testing + #[cfg(test)] #[must_use] - pub fn with_eslint_config(mut self, eslint_config: OxlintConfig) -> Self { - self.eslint_config = Arc::new(eslint_config); + pub(crate) fn with_eslint_config(mut self, config: LintConfig) -> Self { + self.config = Arc::new(config); self } @@ -153,7 +156,7 @@ impl Linter { fn create_ctx<'a>(&self, path: &Path, semantic: Rc>) -> LintContext<'a> { let mut ctx = LintContext::new(path.to_path_buf().into_boxed_path(), semantic) .with_fix(self.options.fix) - .with_eslint_config(&self.eslint_config) + .with_config(&self.config) .with_frameworks(self.options.framework_hints); // set file-specific jest/vitest flags diff --git a/crates/oxc_linter/src/options/mod.rs b/crates/oxc_linter/src/options/mod.rs index 5e42cda837e1d..9a58357bb6fa2 100644 --- a/crates/oxc_linter/src/options/mod.rs +++ b/crates/oxc_linter/src/options/mod.rs @@ -9,7 +9,10 @@ pub use plugins::LintPluginOptions; use rustc_hash::FxHashSet; use crate::{ - config::OxlintConfig, fixer::FixKind, rules::RULES, utils::is_jest_rule_adapted_to_vitest, + config::{LintConfig, OxlintConfig}, + fixer::FixKind, + rules::RULES, + utils::is_jest_rule_adapted_to_vitest, FrameworkFlags, RuleCategory, RuleEnum, RuleWithSeverity, }; @@ -155,7 +158,9 @@ impl LintOptions { /// # Errors /// /// * Returns `Err` if there are any errors parsing the configuration file. - pub fn derive_rules_and_config(&self) -> Result<(Vec, OxlintConfig), Error> { + pub(crate) fn derive_rules_and_config( + &self, + ) -> Result<(Vec, LintConfig), Error> { let config = self.config_path.as_ref().map(|path| OxlintConfig::from_file(path)).transpose()?; @@ -216,7 +221,7 @@ impl LintOptions { // for stable diagnostics output ordering rules.sort_unstable_by_key(|rule| rule.id()); - Ok((rules, config.unwrap_or_default())) + Ok((rules, config.map(Into::into).unwrap_or_default())) } /// Get final filtered rules by reading `self.xxx_plugin` diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 21c63c886951b..f6fbf83216522 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -364,7 +364,7 @@ impl Tester { let linter = Linter::from_options(options) .unwrap() .with_rules(vec![RuleWithSeverity::new(rule, AllowWarnDeny::Warn)]) - .with_eslint_config(eslint_config); + .with_eslint_config(eslint_config.into()); let path_to_lint = if self.plugins.import { assert!(path.is_none(), "import plugin does not support path"); self.current_working_directory.join(&self.rule_path)