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): start internal/external split of OxlintOptions #5141

Closed
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
15 changes: 9 additions & 6 deletions apps/oxlint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ mod result;
mod runner;
mod walk;

pub use crate::{
command::*,
lint::LintRunner,
result::{CliRunResult, LintResult},
runner::Runner,
};
pub mod cli {

pub use crate::{
command::*,
lint::LintRunner,
result::{CliRunResult, LintResult},
runner::Runner,
};
}
12 changes: 7 additions & 5 deletions apps/oxlint/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ use std::{env, io::BufWriter, time::Instant};
use ignore::gitignore::Gitignore;
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
use oxc_linter::{
partial_loader::LINT_PARTIAL_LOADER_EXT, LintOptions, LintService, LintServiceOptions, Linter,
partial_loader::LINT_PARTIAL_LOADER_EXT, LintService, LintServiceOptions, Linter, OxlintOptions,
};
use oxc_span::VALID_EXTENSIONS;

use crate::{
command::{LintCommand, OutputFormat, OutputOptions, WarningOptions},
cli::{
CliRunResult, LintCommand, LintResult, MiscOptions, OutputFormat, OutputOptions, Runner,
WarningOptions,
},
walk::{Extensions, Walk},
CliRunResult, LintResult, MiscOptions, Runner,
};

pub struct LintRunner {
Expand Down Expand Up @@ -90,7 +92,7 @@ impl Runner for LintRunner {
let number_of_files = paths.len();

let cwd = std::env::current_dir().unwrap().into_boxed_path();
let lint_options = LintOptions::default()
let lint_options = OxlintOptions::default()
.with_filter(filter)
.with_config_path(basic_options.config)
.with_fix(fix_options.fix_kind())
Expand Down Expand Up @@ -184,7 +186,7 @@ impl LintRunner {
#[cfg(all(test, not(target_os = "windows")))]
mod test {
use super::LintRunner;
use crate::{lint_command, CliRunResult, LintResult, Runner};
use crate::cli::{lint_command, CliRunResult, LintResult, Runner};

fn test(args: &[&str]) -> LintResult {
let mut new_args = vec!["--silent"];
Expand Down
4 changes: 2 additions & 2 deletions apps/oxlint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use oxlint::{CliRunResult, LintRunner, Runner};
use oxlint::cli::{CliRunResult, LintRunner, Runner};

fn main() -> CliRunResult {
init_tracing();
init_miette();

let command = oxlint::lint_command().run();
let command = oxlint::cli::lint_command().run();
command.handle_threads();
LintRunner::new(command).run()
}
Expand Down
2 changes: 1 addition & 1 deletion apps/oxlint/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::CliRunResult;
use crate::cli::CliRunResult;

/// A trait for exposing functionality to the CLI.
pub trait Runner {
Expand Down
4 changes: 2 additions & 2 deletions apps/oxlint/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use ignore::{overrides::OverrideBuilder, DirEntry};
use oxc_span::VALID_EXTENSIONS;

use crate::IgnoreOptions;
use crate::cli::IgnoreOptions;

#[derive(Clone)]
pub struct Extensions(pub Vec<&'static str>);
Expand Down Expand Up @@ -142,7 +142,7 @@ mod test {
use std::{env, ffi::OsString};

use super::{Extensions, Walk};
use crate::IgnoreOptions;
use crate::cli::IgnoreOptions;

#[test]
fn test_walk_with_extensions() {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::future::join_all;
use globset::Glob;
use ignore::gitignore::Gitignore;
use log::{debug, error, info};
use oxc_linter::{FixKind, LintOptions, Linter};
use oxc_linter::{FixKind, Linter, OxlintOptions};
use serde::{Deserialize, Serialize};
use tokio::sync::{Mutex, OnceCell, RwLock, SetError};
use tower_lsp::{
Expand Down Expand Up @@ -345,7 +345,7 @@ impl Backend {
let mut linter = self.server_linter.write().await;
*linter = ServerLinter::new_with_linter(
Linter::from_options(
LintOptions::default()
OxlintOptions::default()
.with_fix(FixKind::SafeFix)
.with_config_path(Some(config_path)),
)
Expand Down
22 changes: 21 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,34 @@ 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,
}

/// Configuration used by the linter, fixer, and rules.
///
/// This is a mapping from the public [`OxlintConfig`] API to a trimmed down
/// version that is also better suited for internal use. Do not expose this
/// struct outside this crate.
#[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 @@ -82,7 +82,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 @@ -99,8 +99,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 @@ -175,22 +175,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
24 changes: 14 additions & 10 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub mod table;

use std::{io::Write, path::Path, rc::Rc, sync::Arc};

use config::LintConfig;
use options::LintOptions;
use oxc_diagnostics::Error;
use oxc_semantic::{AstNode, Semantic};

Expand All @@ -30,7 +32,7 @@ pub use crate::{
context::LintContext,
fixer::FixKind,
frameworks::FrameworkFlags,
options::{AllowWarnDeny, LintOptions},
options::{AllowWarnDeny, OxlintOptions},
rule::{RuleCategory, RuleFixMeta, RuleMeta, RuleWithSeverity},
service::{LintService, LintServiceOptions},
};
Expand All @@ -54,22 +56,22 @@ fn size_asserts() {
pub struct Linter {
rules: Vec<RuleWithSeverity>,
options: LintOptions,
eslint_config: Arc<OxlintConfig>,
config: Arc<LintConfig>,
}

impl Default for Linter {
fn default() -> Self {
Self::from_options(LintOptions::default()).unwrap()
Self::from_options(OxlintOptions::default()).unwrap()
}
}

impl Linter {
/// # Errors
///
/// 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) })
pub fn from_options(options: OxlintOptions) -> Result<Self, Error> {
let (rules, config) = options.derive_rules_and_config()?;
Ok(Self { rules, options: options.into(), config: Arc::new(config) })
}

#[cfg(test)]
Expand All @@ -79,9 +81,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 All @@ -101,7 +105,7 @@ impl Linter {
self
}

pub fn options(&self) -> &LintOptions {
pub(crate) fn options(&self) -> &LintOptions {
&self.options
}

Expand Down Expand Up @@ -153,7 +157,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
Loading