Skip to content
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
1 change: 1 addition & 0 deletions crates/oxc_linter/src/config/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ impl ConfigStore {
None
}

#[cfg_attr(not(all(feature = "oxlint2", not(feature = "disable_oxlint2"))), expect(dead_code))]
pub(crate) fn resolve_plugin_rule_names(&self, external_rule_id: u32) -> Option<(&str, &str)> {
self.external_plugin_store.resolve_plugin_rule_names(external_rule_id)
}
Expand Down
104 changes: 60 additions & 44 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

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

use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{AstNode, Semantic};
use oxc_span::Span;

#[cfg(test)]
mod tester;
Expand Down Expand Up @@ -39,7 +37,7 @@ pub use crate::{
external_linter::{
ExternalLinter, ExternalLinterCb, ExternalLinterLoadPluginCb, LintResult, PluginLoadResult,
},
external_plugin_store::ExternalPluginStore,
external_plugin_store::{ExternalPluginStore, ExternalRuleId},
fixer::FixKind,
frameworks::FrameworkFlags,
loader::LINTABLE_EXTENSIONS,
Expand All @@ -54,7 +52,7 @@ pub use crate::{
use crate::{
config::{LintConfig, OxlintEnv, OxlintGlobals, OxlintSettings, ResolvedLinterState},
context::ContextHost,
fixer::{Fixer, Message, PossibleFixes},
fixer::{Fixer, Message},
rules::RuleEnum,
utils::iter_possible_jest_call_node,
};
Expand All @@ -76,6 +74,7 @@ fn size_asserts() {
pub struct Linter {
options: LintOptions,
config: ConfigStore,
#[cfg_attr(not(all(feature = "oxlint2", not(feature = "disable_oxlint2"))), expect(dead_code))]
external_linter: Option<ExternalLinter>,
}

Expand Down Expand Up @@ -120,6 +119,9 @@ impl Linter {
) -> Vec<Message<'a>> {
let ResolvedLinterState { rules, config, external_rules } = self.config.resolve(path);

#[cfg(not(all(feature = "oxlint2", not(feature = "disable_oxlint2"))))]
let _ = external_rules;

let ctx_host =
Rc::new(ContextHost::new(path, semantic, module_record, self.options, config));

Expand Down Expand Up @@ -202,46 +204,8 @@ impl Linter {
}
}

if !external_rules.is_empty() {
if let Some(external_linter) = self.external_linter.as_ref() {
let result = (external_linter.run)(
#[expect(clippy::missing_panics_doc)]
path.to_str().unwrap().to_string(),
external_rules.iter().map(|(rule_id, _)| rule_id.raw()).collect(),
);
match result {
Ok(diagnostics) => {
for diagnostic in diagnostics {
match self.config.resolve_plugin_rule_names(diagnostic.external_rule_id)
{
Some((plugin_name, rule_name)) => {
ctx_host.push_diagnostic(Message::new(
// TODO: `error` isn't right, we need to get the severity from `external_rules`
OxcDiagnostic::error(diagnostic.message)
.with_label(Span::new(
diagnostic.loc.start,
diagnostic.loc.end,
))
.with_error_code(
plugin_name.to_string(),
rule_name.to_string(),
),
PossibleFixes::None,
));
}
None => {
// TODO: report diagnostic, this should be unreachable
debug_assert!(false);
}
}
}
}
Err(_err) => {
// TODO: report diagnostic
}
}
}
}
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
self.run_external_rules(&external_rules, path, &ctx_host);

if let Some(severity) = self.options.report_unused_directive {
if severity.is_warn_deny() {
Expand All @@ -251,6 +215,58 @@ impl Linter {

ctx_host.take_diagnostics()
}

#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
fn run_external_rules(
&self,
external_rules: &[(ExternalRuleId, AllowWarnDeny)],
path: &Path,
ctx_host: &ContextHost,
) {
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

use crate::fixer::PossibleFixes;

if external_rules.is_empty() {
return;
}

// TODO: Error or `unwrap` instead of `return`?
let Some(external_linter) = &self.external_linter else { return };

let result = (external_linter.run)(
path.to_str().unwrap().to_string(),
external_rules.iter().map(|(rule_id, _)| rule_id.raw()).collect(),
);
match result {
Ok(diagnostics) => {
for diagnostic in diagnostics {
match self.config.resolve_plugin_rule_names(diagnostic.external_rule_id) {
Some((plugin_name, rule_name)) => {
ctx_host.push_diagnostic(Message::new(
// TODO: `error` isn't right, we need to get the severity from `external_rules`
OxcDiagnostic::error(diagnostic.message)
.with_label(Span::new(diagnostic.loc.start, diagnostic.loc.end))
.with_error_code(
plugin_name.to_string(),
rule_name.to_string(),
),
PossibleFixes::None,
));
}
None => {
// TODO: report diagnostic, this should be unreachable
debug_assert!(false);
}
}
}
}
Err(_err) => {
// TODO: report diagnostic
}
}
}
}

#[cfg(test)]
Expand Down
Loading