Skip to content

Commit b58f99d

Browse files
committed
refactor(linter): run linter with ContextSubHost
1 parent 23e3b37 commit b58f99d

File tree

5 files changed

+201
-183
lines changed

5 files changed

+201
-183
lines changed

crates/oxc_linter/src/context/host.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub struct ContextSubHost<'a> {
3131
#[expect(dead_code)]
3232
pub(super) framework_options: FrameworkOptions,
3333
/// The source text offset of the sub host
34-
#[expect(dead_code)]
3534
pub(super) source_text_offset: u32,
3635
}
3736

@@ -197,12 +196,21 @@ impl<'a> ContextHost<'a> {
197196
/// Add a diagnostic message to the end of the list of diagnostics. Can be used
198197
/// by any rule to report issues.
199198
#[inline]
200-
pub(crate) fn push_diagnostic(&self, diagnostic: Message<'a>) {
199+
pub(crate) fn push_diagnostic(&self, mut diagnostic: Message<'a>) {
200+
if self.current_sub_host().source_text_offset != 0 {
201+
diagnostic.move_offset(self.current_sub_host().source_text_offset);
202+
}
201203
self.diagnostics.borrow_mut().push(diagnostic);
202204
}
203205

204206
// Append a list of diagnostics. Only used in report_unused_directives.
205-
fn append_diagnostics(&self, diagnostics: Vec<Message<'a>>) {
207+
fn append_diagnostics(&self, mut diagnostics: Vec<Message<'a>>) {
208+
if self.current_sub_host().source_text_offset != 0 {
209+
let offset = self.current_sub_host().source_text_offset;
210+
for diagnostic in &mut diagnostics {
211+
diagnostic.move_offset(offset);
212+
}
213+
}
206214
self.diagnostics.borrow_mut().extend(diagnostics);
207215
}
208216

crates/oxc_linter/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#![expect(clippy::self_named_module_files)] // for rules.rs
22
#![allow(clippy::literal_string_with_formatting_args)]
33

4-
use std::{path::Path, rc::Rc, sync::Arc};
4+
use std::{path::Path, rc::Rc};
55

66
use oxc_allocator::Allocator;
7-
use oxc_semantic::{AstNode, Semantic};
7+
use oxc_semantic::AstNode;
88

99
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
1010
use oxc_ast_macros::ast;
11+
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
12+
use oxc_semantic::Semantic;
1113

1214
#[cfg(test)]
1315
mod tester;
@@ -43,7 +45,7 @@ pub use crate::{
4345
BuiltinLintPlugins, Config, ConfigBuilderError, ConfigStore, ConfigStoreBuilder,
4446
ESLintRule, LintPlugins, Oxlintrc,
4547
},
46-
context::LintContext,
48+
context::{ContextSubHost, LintContext},
4749
external_linter::{
4850
ExternalLinter, ExternalLinterLintFileCb, ExternalLinterLoadPluginCb, LintFileResult,
4951
PluginLoadResult,
@@ -62,7 +64,7 @@ pub use crate::{
6264
};
6365
use crate::{
6466
config::{LintConfig, OxlintEnv, OxlintGlobals, OxlintSettings, ResolvedLinterState},
65-
context::{ContextHost, ContextSubHost},
67+
context::ContextHost,
6668
fixer::{Fixer, Message},
6769
rules::RuleEnum,
6870
utils::iter_possible_jest_call_node,
@@ -125,18 +127,12 @@ impl Linter {
125127
pub fn run<'a>(
126128
&self,
127129
path: &Path,
128-
semantic: Rc<Semantic<'a>>,
129-
module_record: Arc<ModuleRecord>,
130+
context_sub_hosts: Vec<ContextSubHost<'a>>,
130131
allocator: &Allocator,
131132
) -> Vec<Message<'a>> {
132133
let ResolvedLinterState { rules, config, external_rules } = self.config.resolve(path);
133134

134-
let ctx_host = Rc::new(ContextHost::new(
135-
path,
136-
vec![ContextSubHost::new(semantic, module_record, 0)],
137-
self.options,
138-
config,
139-
));
135+
let ctx_host = Rc::new(ContextHost::new(path, context_sub_hosts, self.options, config));
140136

141137
loop {
142138
let rules = rules

0 commit comments

Comments
 (0)