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
18 changes: 12 additions & 6 deletions crates/oxc_language_server/src/linter/isolated_lint_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -120,15 +122,19 @@ impl IsolatedLintHandler {
source_text: &str,
) -> Vec<MessageWithPosition<'a>> {
debug!("lint {}", path.display());
let rope = &Rope::from_str(source_text);

let mut messages = self
let mut messages: Vec<MessageWithPosition<'a>> = 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
Expand All @@ -138,6 +144,7 @@ impl IsolatedLintHandler {
&directives,
severity,
source_text,
rope,
));
}

Expand All @@ -150,18 +157,17 @@ impl IsolatedLintHandler {
directives: &DisableDirectives,
severity: AllowWarnDeny,
source_text: &str,
rope: &Rope,
) -> Vec<MessageWithPosition<'static>> {
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()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_linter/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl LintService {
pub fn run_source<'a>(
&mut self,
allocator: &'a mut oxc_allocator::Allocator,
) -> Vec<crate::MessageWithPosition<'a>> {
) -> Vec<crate::Message<'a>> {
self.runtime.run_source(allocator)
}

Expand Down
36 changes: 13 additions & 23 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -710,30 +711,22 @@ impl Runtime {
pub(super) fn run_source<'a>(
&mut self,
allocator: &'a mut oxc_allocator::Allocator,
) -> Vec<MessageWithPosition<'a>> {
) -> Vec<Message<'a>> {
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::<MessageWithPosition<'a>>::new());
let messages = Mutex::new(Vec::<Message<'a>>::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<ContextSubHost<'_>> = module_to_lint
.section_module_records
.into_iter()
Expand All @@ -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)
}),
);
}
Expand All @@ -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)),
);
},
);
});
Expand Down
Loading