From 773fd888a69b242a119f86d9a9528b8503315247 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 18 Jul 2025 08:48:37 +0000 Subject: [PATCH] refactor(linter): pass `&Allocator` into `Linter::run_external_rules` (#12374) Pass `&Allocator` into `Linter::run_external_rules`. It's not used there for now, but it will be required for passing AST over to JS via raw transfer. --- crates/oxc_linter/src/lib.rs | 12 ++++++++---- crates/oxc_linter/src/service/runtime.rs | 9 ++++++--- napi/playground/src/lib.rs | 12 ++++++++++-- tasks/benchmark/benches/linter.rs | 4 +++- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 3011ca0a09511..bf6679d370e6e 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -3,6 +3,7 @@ use std::{path::Path, rc::Rc, sync::Arc}; +use oxc_allocator::Allocator; use oxc_semantic::{AstNode, Semantic}; #[cfg(test)] @@ -116,12 +117,10 @@ impl Linter { path: &Path, semantic: Rc>, module_record: Arc, + allocator: &Allocator, ) -> Vec> { 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)); @@ -205,7 +204,11 @@ impl Linter { } #[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))] - self.run_external_rules(&external_rules, path, &ctx_host); + self.run_external_rules(&external_rules, path, &ctx_host, allocator); + + // Stop clippy complaining about unused vars + #[cfg(not(all(feature = "oxlint2", not(feature = "disable_oxlint2"))))] + let (_, _) = (external_rules, allocator); if let Some(severity) = self.options.report_unused_directive { if severity.is_warn_deny() { @@ -222,6 +225,7 @@ impl Linter { external_rules: &[(ExternalRuleId, AllowWarnDeny)], path: &Path, ctx_host: &ContextHost, + _allocator: &Allocator, ) { use oxc_diagnostics::OxcDiagnostic; use oxc_span::Span; diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index d7d8fd48f7f98..169c47d46349a 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -498,7 +498,7 @@ impl Runtime { pub(super) fn run(&mut self, tx_error: &DiagnosticSender) { rayon::scope(|scope| { self.resolve_modules(scope, true, tx_error, |me, mut module_to_lint| { - module_to_lint.content.with_dependent_mut(|_allocator_guard, dep| { + module_to_lint.content.with_dependent_mut(|allocator_guard, dep| { // If there are fixes, we will accumulate all of them and write to the file at the end. // This means we do not write multiple times to the same file if there are multiple sources // in the same file (for example, multiple scripts in an `.astro` file). @@ -524,6 +524,7 @@ impl Runtime { path, Rc::new(section.semantic.unwrap()), Arc::clone(&module_record), + allocator_guard, ), Err(errors) => errors .into_iter() @@ -613,7 +614,7 @@ impl Runtime { rayon::scope(|scope| { self.resolve_modules(scope, true, &sender, |me, mut module| { module.content.with_dependent_mut( - |_allocator_guard, ModuleContentDependent { source_text, section_contents }| { + |allocator_guard, ModuleContentDependent { source_text, section_contents }| { assert_eq!(module.section_module_records.len(), section_contents.len()); let rope = &Rope::from_str(source_text); @@ -634,6 +635,7 @@ impl Runtime { Path::new(&module.path), Rc::new(section.semantic.unwrap()), Arc::clone(&module_record), + allocator_guard, ); messages.lock().unwrap().extend(section_message.iter().map( @@ -750,7 +752,7 @@ impl Runtime { rayon::scope(|scope| { self.resolve_modules(scope, check_syntax_errors, tx_error, |me, mut module| { module.content.with_dependent_mut( - |_allocator_guard, ModuleContentDependent { source_text: _, section_contents }| { + |allocator_guard, ModuleContentDependent { source_text: _, section_contents }| { assert_eq!(module.section_module_records.len(), section_contents.len()); for (record_result, section) in module .section_module_records @@ -763,6 +765,7 @@ impl Runtime { Path::new(&module.path), Rc::new(section.semantic.unwrap()), Arc::clone(&module_record), + allocator_guard, ), Err(errors) => errors .into_iter() diff --git a/napi/playground/src/lib.rs b/napi/playground/src/lib.rs index 52ddb50054346..28c46918fd9c3 100644 --- a/napi/playground/src/lib.rs +++ b/napi/playground/src/lib.rs @@ -154,7 +154,14 @@ impl Oxc { } let linter_module_record = Arc::new(ModuleRecord::new(&path, &module_record, &semantic)); - self.run_linter(&run_options, &linter_options, &path, &program, &linter_module_record); + self.run_linter( + &run_options, + &linter_options, + &path, + &program, + &linter_module_record, + &allocator, + ); self.run_formatter(&run_options, &source_text, source_type); @@ -282,6 +289,7 @@ impl Oxc { path: &Path, program: &Program, module_record: &Arc, + allocator: &Allocator, ) { // Only lint if there are no syntax errors if run_options.lint.unwrap_or_default() && self.diagnostics.is_empty() { @@ -307,7 +315,7 @@ impl Oxc { ConfigStore::new(lint_config, FxHashMap::default(), ExternalPluginStore::default()), None, ) - .run(path, Rc::clone(&semantic), Arc::clone(module_record)); + .run(path, Rc::clone(&semantic), Arc::clone(module_record), allocator); self.diagnostics.extend(linter_ret.into_iter().map(|e| e.error)); } } diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index 5097fcb481ca7..f5ce0d0b39d8e 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -38,7 +38,9 @@ fn bench_linter(criterion: &mut Criterion) { ) .with_fix(FixKind::All); group.bench_function(id, |b| { - b.iter(|| linter.run(path, Rc::clone(&semantic), Arc::clone(&module_record))); + b.iter(|| { + linter.run(path, Rc::clone(&semantic), Arc::clone(&module_record), &allocator) + }); }); } group.finish();