Skip to content

Commit

Permalink
refactor(linter): refactor LintBuilder to prep for nested configs
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 authored and camchenry committed Jan 4, 2025
1 parent 65f46f5 commit 5c25148
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 124 deletions.
28 changes: 20 additions & 8 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use ignore::gitignore::Gitignore;

use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
use oxc_linter::{
loader::LINT_PARTIAL_LOADER_EXT, AllowWarnDeny, InvalidFilterKind, LintFilter, LintService,
LintServiceOptions, Linter, LinterBuilder, Oxlintrc,
loader::LINT_PARTIAL_LOADER_EXT, AllowWarnDeny, ConfigStoreBuilder, InvalidFilterKind,
LintFilter, LintOptions, LintService, LintServiceOptions, Linter, Oxlintrc,
};
use oxc_span::VALID_EXTENSIONS;

Expand Down Expand Up @@ -128,20 +128,32 @@ impl Runner for LintRunner {

let oxlintrc_for_print =
if misc_options.print_config { Some(oxlintrc.clone()) } else { None };
let builder = LinterBuilder::from_oxlintrc(false, oxlintrc)
.with_filters(filter)
.with_fix(fix_options.fix_kind());
let config_builder =
ConfigStoreBuilder::from_oxlintrc(false, oxlintrc).with_filters(filter);

if let Some(basic_config_file) = oxlintrc_for_print {
return CliRunResult::PrintConfigResult {
config_file: builder.resolve_final_config_file(basic_config_file),
config_file: config_builder.resolve_final_config_file(basic_config_file),
};
}

let mut options = LintServiceOptions::new(self.cwd, paths)
.with_cross_module(builder.plugins().has_import());
.with_cross_module(config_builder.plugins().has_import());

let lint_config = match config_builder.build() {
Ok(config) => config,
Err(diagnostic) => {
let handler = GraphicalReportHandler::new();
let mut err = String::new();
handler.render_report(&mut err, &diagnostic).unwrap();
return CliRunResult::InvalidOptions {
message: format!("Failed to parse configuration file.\n{err}"),
};
}
};

let linter = builder.build();
let linter =
Linter::new(LintOptions::default(), lint_config).with_fix(fix_options.fix_kind());

let tsconfig = basic_options.tsconfig;
if let Some(path) = tsconfig.as_ref() {
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_language_server/src/linter/server_linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use tower_lsp::lsp_types::Url;

use oxc_linter::{FixKind, Linter};
use oxc_linter::{ConfigStoreBuilder, FixKind, LintOptions, Linter};

use crate::linter::error_with_position::DiagnosticReport;
use crate::linter::isolated_lint_handler::IsolatedLintHandler;
Expand All @@ -13,7 +13,9 @@ pub struct ServerLinter {

impl ServerLinter {
pub fn new() -> Self {
let linter = Linter::default().with_fix(FixKind::SafeFix);
let config_store =
ConfigStoreBuilder::default().build().expect("Failed to build config store");
let linter = Linter::new(LintOptions::default(), config_store).with_fix(FixKind::SafeFix);
Self { linter: Arc::new(linter) }
}

Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tower_lsp::{
Client, LanguageServer, LspService, Server,
};

use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};
use oxc_linter::{ConfigStoreBuilder, FixKind, LintOptions, Linter, Oxlintrc};

use crate::capabilities::{Capabilities, CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC};
use crate::linter::error_with_position::DiagnosticReport;
Expand Down Expand Up @@ -488,10 +488,11 @@ impl Backend {
let mut linter = self.server_linter.write().await;
let config = Oxlintrc::from_file(&config_path)
.expect("should have initialized linter with new options");
let config_store = ConfigStoreBuilder::from_oxlintrc(true, config.clone())
.build()
.expect("failed to build config");
*linter = ServerLinter::new_with_linter(
LinterBuilder::from_oxlintrc(true, config.clone())
.with_fix(FixKind::SafeFix)
.build(),
Linter::new(LintOptions::default(), config_store).with_fix(FixKind::SafeFix),
);
return Some(config);
}
Expand Down
Loading

0 comments on commit 5c25148

Please sign in to comment.