Skip to content

Commit 4c0e32f

Browse files
committed
refactor(language_server): linter runtime returns FxHashMap
1 parent 7708d56 commit 4c0e32f

File tree

3 files changed

+68
-80
lines changed

3 files changed

+68
-80
lines changed

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ impl IsolatedLintHandler {
142142
LintService::new(&self.linter, lint_service_options).with_file_system(Box::new(
143143
IsolatedLintHandlerFileSystem::new(path.to_path_buf(), source_text),
144144
));
145-
let result = lint_service.run_source(allocator);
146-
147-
Some(result)
145+
let mut result = lint_service.run_source(allocator);
146+
result.remove(path)
148147
}
149148

150149
fn should_lint_path(path: &Path) -> bool {

crates/oxc_linter/src/service/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_hash::FxHashMap;
12
use std::{
23
ffi::OsStr,
34
path::{Path, PathBuf},
@@ -95,7 +96,7 @@ impl<'l> LintService<'l> {
9596
pub fn run_source<'a>(
9697
&mut self,
9798
allocator: &'a oxc_allocator::Allocator,
98-
) -> Vec<crate::MessageWithPosition<'a>> {
99+
) -> FxHashMap<PathBuf, Vec<crate::MessageWithPosition<'a>>> {
99100
self.runtime.run_source(allocator)
100101
}
101102

crates/oxc_linter/src/service/runtime.rs

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,11 @@ impl<'l> Runtime<'l> {
552552
pub(super) fn run_source<'a>(
553553
&mut self,
554554
allocator: &'a oxc_allocator::Allocator,
555-
) -> Vec<MessageWithPosition<'a>> {
556-
use oxc_allocator::CloneIn;
555+
) -> FxHashMap<PathBuf, Vec<MessageWithPosition<'a>>> {
557556
use std::sync::Mutex;
558557

558+
use oxc_allocator::CloneIn;
559+
559560
use crate::{
560561
FixWithPosition,
561562
fixer::{Fix, PossibleFixesWithPosition},
@@ -576,21 +577,69 @@ impl<'l> Runtime<'l> {
576577
}
577578
}
578579

579-
let messages = Mutex::new(Vec::<MessageWithPosition<'a>>::new());
580+
fn message_to_message_with_position<'a>(
581+
message: &Message<'a>,
582+
source_text: &str,
583+
section_offset: u32,
584+
) -> MessageWithPosition<'a> {
585+
let labels = &message.error.labels.clone().map(|labels| {
586+
labels
587+
.into_iter()
588+
.map(|labeled_span| {
589+
let offset = labeled_span.offset() as u32;
590+
let start_position =
591+
offset_to_position(offset + section_offset, source_text);
592+
let end_position = offset_to_position(
593+
section_offset + offset + labeled_span.len() as u32,
594+
source_text,
595+
);
596+
let message =
597+
labeled_span.label().map(|label| Cow::Owned(label.to_string()));
598+
599+
SpanPositionMessage::new(start_position, end_position).with_message(message)
600+
})
601+
.collect::<Vec<_>>()
602+
});
603+
604+
MessageWithPosition {
605+
message: message.error.message.clone(),
606+
severity: message.error.severity,
607+
help: message.error.help.clone(),
608+
url: message.error.url.clone(),
609+
code: message.error.code.clone(),
610+
labels: labels.clone(),
611+
fixes: match &message.fixes {
612+
PossibleFixes::None => PossibleFixesWithPosition::None,
613+
PossibleFixes::Single(fix) => PossibleFixesWithPosition::Single(
614+
fix_to_fix_with_position(fix, section_offset, source_text),
615+
),
616+
PossibleFixes::Multiple(fixes) => PossibleFixesWithPosition::Multiple(
617+
fixes
618+
.iter()
619+
.map(|fix| fix_to_fix_with_position(fix, section_offset, source_text))
620+
.collect(),
621+
),
622+
},
623+
}
624+
}
625+
626+
let messages = Mutex::new(FxHashMap::<PathBuf, Vec<MessageWithPosition<'a>>>::with_hasher(
627+
FxBuildHasher,
628+
));
580629
let (sender, _receiver) = mpsc::channel();
581630
rayon::scope(|scope| {
582631
self.resolve_modules(scope, true, &sender, |me, mut module| {
583632
module.content.with_dependent_mut(|owner, dependent| {
584633
assert_eq!(module.section_module_records.len(), dependent.len());
634+
let path = Path::new(&module.path);
635+
let mut all_module_messages = Vec::new();
585636

586637
for (record_result, section) in
587638
module.section_module_records.into_iter().zip(dependent.drain(..))
588639
{
589640
match record_result {
590641
Err(diagnostics) => {
591-
messages
592-
.lock()
593-
.unwrap()
642+
all_module_messages
594643
.extend(diagnostics.into_iter().map(std::convert::Into::into));
595644
}
596645
Ok(module_record) => {
@@ -600,79 +649,18 @@ impl<'l> Runtime<'l> {
600649
Arc::clone(&module_record),
601650
);
602651

603-
messages.lock().unwrap().extend(section_message.iter().map(
604-
|message| {
605-
let message = message.clone_in(allocator);
606-
607-
let labels = &message.error.labels.clone().map(|labels| {
608-
labels
609-
.into_iter()
610-
.map(|labeled_span| {
611-
let offset = labeled_span.offset() as u32;
612-
let start_position = offset_to_position(
613-
offset + section.source.start,
614-
&owner.source_text,
615-
);
616-
let end_position = offset_to_position(
617-
offset
618-
+ section.source.start
619-
+ labeled_span.len() as u32,
620-
&owner.source_text,
621-
);
622-
let message = labeled_span
623-
.label()
624-
.map(|label| Cow::Owned(label.to_string()));
625-
626-
SpanPositionMessage::new(
627-
start_position,
628-
end_position,
629-
)
630-
.with_message(message)
631-
})
632-
.collect::<Vec<_>>()
633-
});
634-
635-
MessageWithPosition {
636-
message: message.error.message.clone(),
637-
severity: message.error.severity,
638-
help: message.error.help.clone(),
639-
url: message.error.url.clone(),
640-
code: message.error.code.clone(),
641-
labels: labels.clone(),
642-
fixes: match &message.fixes {
643-
PossibleFixes::None => {
644-
PossibleFixesWithPosition::None
645-
}
646-
PossibleFixes::Single(fix) => {
647-
PossibleFixesWithPosition::Single(
648-
fix_to_fix_with_position(
649-
fix,
650-
section.source.start,
651-
&owner.source_text,
652-
),
653-
)
654-
}
655-
PossibleFixes::Multiple(fixes) => {
656-
PossibleFixesWithPosition::Multiple(
657-
fixes
658-
.iter()
659-
.map(|fix| {
660-
fix_to_fix_with_position(
661-
fix,
662-
section.source.start,
663-
&owner.source_text,
664-
)
665-
})
666-
.collect(),
667-
)
668-
}
669-
},
670-
}
671-
},
672-
));
652+
all_module_messages.extend(section_message.iter().map(|message| {
653+
message_to_message_with_position(
654+
&message.clone_in(allocator),
655+
&owner.source_text,
656+
section.source.start,
657+
)
658+
}));
673659
}
674660
}
675661
}
662+
663+
messages.lock().unwrap().insert(path.to_path_buf(), all_module_messages);
676664
});
677665
});
678666
});

0 commit comments

Comments
 (0)