mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
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).
This commit is contained in:
parent
1691cab507
commit
8f1460eba3
10 changed files with 58 additions and 34 deletions
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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<Oxlintrc> 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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<Semantic<'a>>,
|
||||
options: LintOptions,
|
||||
config: Arc<LintConfig>,
|
||||
) -> 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<LintConfig>) -> Self {
|
||||
let plugins = config.plugins;
|
||||
self.config = Arc::clone(config);
|
||||
|
||||
if self.plugins != plugins {
|
||||
self.plugins = plugins;
|
||||
return self.sniff_for_frameworks();
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Semantic<'a>>) -> Vec<Message<'a>> {
|
||||
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:
|
||||
//
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue