Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(linter): move LintPlugins from LintOptions to LintConfig #6932

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions crates/oxc_linter/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,24 +34,28 @@ 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`.
/// This is the kitchen sink.
///
/// 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,
}
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)]
Expand Down
10 changes: 9 additions & 1 deletion crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ mod categories;
mod env;
mod globals;
mod oxlintrc;
mod plugins;
mod rules;
mod settings;

pub use self::{
env::OxlintEnv,
globals::OxlintGlobals,
oxlintrc::Oxlintrc,
plugins::LintPlugins,
rules::ESLintRule,
rules::OxlintRules,
settings::{jsdoc::JSDocPluginSettings, OxlintSettings},
};

#[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,
Expand All @@ -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,
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/config/oxlintrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
18 changes: 14 additions & 4 deletions crates/oxc_linter/src/context/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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;

Expand All @@ -82,24 +83,33 @@ 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,
disable_directives,
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
}

Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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
Expand All @@ -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:
//
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/options/filter.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_linter/src/options/mod.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -14,5 +12,4 @@ use crate::{fixer::FixKind, FrameworkFlags};
pub(crate) struct LintOptions {
pub fix: FixKind,
pub framework_hints: FrameworkFlags,
pub plugins: LintPlugins,
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
11 changes: 8 additions & 3 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down
Loading