mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(linter): start internal/external split of OxlintConfig (#5595)
Re-creation of #5140
This commit is contained in:
parent
89bdf5536f
commit
0b3c1d72a3
5 changed files with 43 additions and 24 deletions
|
|
@ -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<OxlintConfig> for LintConfig {
|
||||
fn from(config: OxlintConfig) -> Self {
|
||||
Self { settings: config.settings, env: config.env, globals: config.globals }
|
||||
}
|
||||
}
|
||||
|
||||
impl OxlintConfig {
|
||||
/// # Errors
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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<Path>,
|
||||
|
||||
eslint_config: Arc<OxlintConfig>,
|
||||
config: Arc<LintConfig>,
|
||||
|
||||
// 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<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
|
||||
}
|
||||
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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<RuleWithSeverity>,
|
||||
options: LintOptions,
|
||||
eslint_config: Arc<OxlintConfig>,
|
||||
config: Arc<LintConfig>,
|
||||
}
|
||||
|
||||
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<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)]
|
||||
|
|
@ -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<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
|
||||
|
|
|
|||
|
|
@ -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<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()?;
|
||||
|
||||
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue