diff --git a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs index f08b583874ec2..d754cc07641e3 100644 --- a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs +++ b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs @@ -4,13 +4,15 @@ use std::{ }; use log::debug; +use oxc_data_structures::rope::Rope; use rustc_hash::FxHashSet; use tower_lsp_server::{UriExt, lsp_types::Uri}; use oxc_allocator::Allocator; use oxc_linter::{ AllowWarnDeny, ConfigStore, DirectivesStore, DisableDirectives, LINTABLE_EXTENSIONS, - LintOptions, LintService, LintServiceOptions, Linter, MessageWithPosition, read_to_arena_str, + LintOptions, LintService, LintServiceOptions, Linter, MessageWithPosition, + message_to_message_with_position, read_to_arena_str, }; use oxc_linter::{RuntimeFileSystem, read_to_string}; @@ -120,15 +122,19 @@ impl IsolatedLintHandler { source_text: &str, ) -> Vec> { debug!("lint {}", path.display()); + let rope = &Rope::from_str(source_text); - let mut messages = self + let mut messages: Vec> = self .service .with_file_system(Box::new(IsolatedLintHandlerFileSystem::new( path.to_path_buf(), Arc::from(source_text), ))) .with_paths(vec![Arc::from(path.as_os_str())]) - .run_source(allocator); + .run_source(allocator) + .into_iter() + .map(|message| message_to_message_with_position(message, source_text, rope)) + .collect(); // Add unused directives if configured if let Some(severity) = self.unused_directives_severity @@ -138,6 +144,7 @@ impl IsolatedLintHandler { &directives, severity, source_text, + rope, )); } @@ -150,18 +157,17 @@ impl IsolatedLintHandler { directives: &DisableDirectives, severity: AllowWarnDeny, source_text: &str, + rope: &Rope, ) -> Vec> { - use oxc_data_structures::rope::Rope; use oxc_linter::{ create_unused_directives_diagnostics, oxc_diagnostic_to_message_with_position, }; - let rope = Rope::from_str(source_text); let diagnostics = create_unused_directives_diagnostics(directives, severity); diagnostics .into_iter() .map(|diagnostic| { - oxc_diagnostic_to_message_with_position(diagnostic, source_text, &rope) + oxc_diagnostic_to_message_with_position(diagnostic, source_text, rope) }) .collect() } diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 9d0a7f8a39e8e..847874b67f824 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -89,7 +89,7 @@ use crate::{ #[cfg(feature = "language_server")] pub use crate::lsp::{ FixWithPosition, MessageWithPosition, PossibleFixesWithPosition, SpanPositionMessage, - oxc_diagnostic_to_message_with_position, + message_to_message_with_position, oxc_diagnostic_to_message_with_position, }; #[cfg(target_pointer_width = "64")] diff --git a/crates/oxc_linter/src/lsp.rs b/crates/oxc_linter/src/lsp.rs index 399511d61cf97..9c0d7b55ab67b 100644 --- a/crates/oxc_linter/src/lsp.rs +++ b/crates/oxc_linter/src/lsp.rs @@ -199,7 +199,13 @@ fn add_ignore_fixes<'a>( new_fixes.push(disable_for_this_section(rule_name, section_offset, rope, source_text)); } - PossibleFixesWithPosition::Multiple(new_fixes) + if new_fixes.is_empty() { + PossibleFixesWithPosition::None + } else if new_fixes.len() == 1 { + PossibleFixesWithPosition::Single(new_fixes.remove(0)) + } else { + PossibleFixesWithPosition::Multiple(new_fixes) + } } fn disable_for_this_line<'a>( diff --git a/crates/oxc_linter/src/service/mod.rs b/crates/oxc_linter/src/service/mod.rs index f4517c1dca516..2d919e1f44c71 100644 --- a/crates/oxc_linter/src/service/mod.rs +++ b/crates/oxc_linter/src/service/mod.rs @@ -99,7 +99,7 @@ impl LintService { pub fn run_source<'a>( &mut self, allocator: &'a mut oxc_allocator::Allocator, - ) -> Vec> { + ) -> Vec> { self.runtime.run_source(allocator) } diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index 390cce9e5d5f2..0b0fd989e4ef3 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -23,10 +23,11 @@ use oxc_semantic::{Semantic, SemanticBuilder}; use oxc_span::{CompactStr, SourceType, VALID_EXTENSIONS}; #[cfg(feature = "language_server")] -use crate::lsp::MessageWithPosition; +use crate::Message; + +#[cfg(any(test, feature = "language_server"))] +use crate::fixer::PossibleFixes; -#[cfg(test)] -use crate::fixer::{Message, PossibleFixes}; use crate::{ Fixer, Linter, context::ContextSubHost, @@ -710,30 +711,22 @@ impl Runtime { pub(super) fn run_source<'a>( &mut self, allocator: &'a mut oxc_allocator::Allocator, - ) -> Vec> { + ) -> Vec> { use std::sync::Mutex; - use oxc_data_structures::rope::Rope; - - use crate::lsp::{ - message_to_message_with_position, oxc_diagnostic_to_message_with_position, - }; - // Wrap allocator in `MessageCloner` so can clone `Message`s into it let message_cloner = MessageCloner::new(allocator); - let messages = Mutex::new(Vec::>::new()); + let messages = Mutex::new(Vec::>::new()); rayon::scope(|scope| { self.resolve_modules(scope, true, None, |me, mut module_to_lint| { module_to_lint.content.with_dependent_mut( - |allocator_guard, ModuleContentDependent { source_text, section_contents }| { + |allocator_guard, ModuleContentDependent { source_text: _, section_contents }| { assert_eq!( module_to_lint.section_module_records.len(), section_contents.len() ); - let rope = &Rope::from_str(source_text); - let context_sub_hosts: Vec> = module_to_lint .section_module_records .into_iter() @@ -751,11 +744,7 @@ impl Runtime { if !diagnostics.is_empty() { messages.lock().unwrap().extend( diagnostics.into_iter().map(|diagnostic| { - oxc_diagnostic_to_message_with_position( - diagnostic, - source_text, - rope, - ) + Message::new(diagnostic, PossibleFixes::None) }), ); } @@ -780,10 +769,11 @@ impl Runtime { .insert(path.to_path_buf(), disable_directives); } - messages.lock().unwrap().extend(section_messages.iter().map(|message| { - let message = message_cloner.clone_message(message); - message_to_message_with_position(message, source_text, rope) - })); + messages.lock().unwrap().extend( + section_messages + .iter() + .map(|message| message_cloner.clone_message(message)), + ); }, ); });