Skip to content

Commit

Permalink
refactor(linter): start internal/external split of OxlintConfig (#5595
Browse files Browse the repository at this point in the history
)

Re-creation of #5140
  • Loading branch information
DonIsaac committed Sep 9, 2024
1 parent 89bdf55 commit 0b3c1d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
17 changes: 16 additions & 1 deletion crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,29 @@ 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,
/// Enabled or disabled specific global variables.
pub(crate) globals: OxlintGlobals,
}

impl From<OxlintConfig> for LintConfig {
fn from(config: OxlintConfig) -> Self {
Self { settings: config.settings, env: config.env, globals: config.globals }
}
}

impl OxlintConfig {
/// # Errors
///
Expand Down
22 changes: 9 additions & 13 deletions crates/oxc_linter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -38,7 +38,7 @@ pub struct LintContext<'a> {

file_path: Rc<Path>,

eslint_config: Arc<OxlintConfig>,
config: Arc<LintConfig>,

// states
current_plugin_name: &'static str,
Expand Down Expand Up @@ -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: "",
Expand All @@ -100,8 +100,8 @@ impl<'a> LintContext<'a> {
self
}

pub fn with_eslint_config(mut self, eslint_config: &Arc<OxlintConfig>) -> Self {
self.eslint_config = Arc::clone(eslint_config);
pub(crate) fn with_config(mut self, config: &Arc<LintConfig>) -> Self {
self.config = Arc::clone(config);
self
}

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -54,7 +55,7 @@ fn size_asserts() {
pub struct Linter {
rules: Vec<RuleWithSeverity>,
options: LintOptions,
eslint_config: Arc<OxlintConfig>,
config: Arc<LintConfig>,
}

impl Default for Linter {
Expand All @@ -68,8 +69,8 @@ impl Linter {
///
/// Returns `Err` if there are any errors parsing the configuration file.
pub fn from_options(options: LintOptions) -> Result<Self, Error> {
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)]
Expand All @@ -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
}

Expand Down Expand Up @@ -153,7 +156,7 @@ impl Linter {
fn create_ctx<'a>(&self, path: &Path, semantic: Rc<Semantic<'a>>) -> 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
Expand Down
11 changes: 8 additions & 3 deletions crates/oxc_linter/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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<RuleWithSeverity>, OxlintConfig), Error> {
pub(crate) fn derive_rules_and_config(
&self,
) -> Result<(Vec<RuleWithSeverity>, LintConfig), Error> {
let config =
self.config_path.as_ref().map(|path| OxlintConfig::from_file(path)).transpose()?;

Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0b3c1d7

Please sign in to comment.