Skip to content

Commit e5b7fb2

Browse files
committed
refactor(linter/language_server): oxc_linter::Runtime::run_source returns Message (#14428)
Because `oxc_linter::fixer::Message` has now `section_offset` and thanks to #12647 in combination with #12724, the server has everything to calculate what it needs, all relevant code can be moved to the language_server crate. Next steps are to simplify the transformation to `DiagnosticReport` noticed by https://github.com/oxc-project/backlog/issues/175
1 parent 493082c commit e5b7fb2

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use std::{
44
};
55

66
use log::debug;
7+
use oxc_data_structures::rope::Rope;
78
use rustc_hash::FxHashSet;
89
use tower_lsp_server::{UriExt, lsp_types::Uri};
910

1011
use oxc_allocator::Allocator;
1112
use oxc_linter::{
1213
AllowWarnDeny, ConfigStore, DirectivesStore, DisableDirectives, LINTABLE_EXTENSIONS,
13-
LintOptions, LintService, LintServiceOptions, Linter, MessageWithPosition, read_to_arena_str,
14+
LintOptions, LintService, LintServiceOptions, Linter, MessageWithPosition,
15+
message_to_message_with_position, read_to_arena_str,
1416
};
1517
use oxc_linter::{RuntimeFileSystem, read_to_string};
1618

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

124-
let mut messages = self
127+
let mut messages: Vec<MessageWithPosition<'a>> = self
125128
.service
126129
.with_file_system(Box::new(IsolatedLintHandlerFileSystem::new(
127130
path.to_path_buf(),
128131
Arc::from(source_text),
129132
)))
130133
.with_paths(vec![Arc::from(path.as_os_str())])
131-
.run_source(allocator);
134+
.run_source(allocator)
135+
.into_iter()
136+
.map(|message| message_to_message_with_position(message, source_text, rope))
137+
.collect();
132138

133139
// Add unused directives if configured
134140
if let Some(severity) = self.unused_directives_severity
@@ -138,6 +144,7 @@ impl IsolatedLintHandler {
138144
&directives,
139145
severity,
140146
source_text,
147+
rope,
141148
));
142149
}
143150

@@ -150,18 +157,17 @@ impl IsolatedLintHandler {
150157
directives: &DisableDirectives,
151158
severity: AllowWarnDeny,
152159
source_text: &str,
160+
rope: &Rope,
153161
) -> Vec<MessageWithPosition<'static>> {
154-
use oxc_data_structures::rope::Rope;
155162
use oxc_linter::{
156163
create_unused_directives_diagnostics, oxc_diagnostic_to_message_with_position,
157164
};
158165

159-
let rope = Rope::from_str(source_text);
160166
let diagnostics = create_unused_directives_diagnostics(directives, severity);
161167
diagnostics
162168
.into_iter()
163169
.map(|diagnostic| {
164-
oxc_diagnostic_to_message_with_position(diagnostic, source_text, &rope)
170+
oxc_diagnostic_to_message_with_position(diagnostic, source_text, rope)
165171
})
166172
.collect()
167173
}

crates/oxc_linter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use crate::{
8989
#[cfg(feature = "language_server")]
9090
pub use crate::lsp::{
9191
FixWithPosition, MessageWithPosition, PossibleFixesWithPosition, SpanPositionMessage,
92-
oxc_diagnostic_to_message_with_position,
92+
message_to_message_with_position, oxc_diagnostic_to_message_with_position,
9393
};
9494

9595
#[cfg(target_pointer_width = "64")]

crates/oxc_linter/src/lsp.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ fn add_ignore_fixes<'a>(
199199
new_fixes.push(disable_for_this_section(rule_name, section_offset, rope, source_text));
200200
}
201201

202-
PossibleFixesWithPosition::Multiple(new_fixes)
202+
if new_fixes.is_empty() {
203+
PossibleFixesWithPosition::None
204+
} else if new_fixes.len() == 1 {
205+
PossibleFixesWithPosition::Single(new_fixes.remove(0))
206+
} else {
207+
PossibleFixesWithPosition::Multiple(new_fixes)
208+
}
203209
}
204210

205211
fn disable_for_this_line<'a>(

crates/oxc_linter/src/service/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl LintService {
9999
pub fn run_source<'a>(
100100
&mut self,
101101
allocator: &'a mut oxc_allocator::Allocator,
102-
) -> Vec<crate::MessageWithPosition<'a>> {
102+
) -> Vec<crate::Message<'a>> {
103103
self.runtime.run_source(allocator)
104104
}
105105

crates/oxc_linter/src/service/runtime.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ use oxc_semantic::{Semantic, SemanticBuilder};
2323
use oxc_span::{CompactStr, SourceType, VALID_EXTENSIONS};
2424

2525
#[cfg(feature = "language_server")]
26-
use crate::lsp::MessageWithPosition;
26+
use crate::Message;
27+
28+
#[cfg(any(test, feature = "language_server"))]
29+
use crate::fixer::PossibleFixes;
2730

28-
#[cfg(test)]
29-
use crate::fixer::{Message, PossibleFixes};
3031
use crate::{
3132
Fixer, Linter,
3233
context::ContextSubHost,
@@ -710,30 +711,22 @@ impl Runtime {
710711
pub(super) fn run_source<'a>(
711712
&mut self,
712713
allocator: &'a mut oxc_allocator::Allocator,
713-
) -> Vec<MessageWithPosition<'a>> {
714+
) -> Vec<Message<'a>> {
714715
use std::sync::Mutex;
715716

716-
use oxc_data_structures::rope::Rope;
717-
718-
use crate::lsp::{
719-
message_to_message_with_position, oxc_diagnostic_to_message_with_position,
720-
};
721-
722717
// Wrap allocator in `MessageCloner` so can clone `Message`s into it
723718
let message_cloner = MessageCloner::new(allocator);
724719

725-
let messages = Mutex::new(Vec::<MessageWithPosition<'a>>::new());
720+
let messages = Mutex::new(Vec::<Message<'a>>::new());
726721
rayon::scope(|scope| {
727722
self.resolve_modules(scope, true, None, |me, mut module_to_lint| {
728723
module_to_lint.content.with_dependent_mut(
729-
|allocator_guard, ModuleContentDependent { source_text, section_contents }| {
724+
|allocator_guard, ModuleContentDependent { source_text: _, section_contents }| {
730725
assert_eq!(
731726
module_to_lint.section_module_records.len(),
732727
section_contents.len()
733728
);
734729

735-
let rope = &Rope::from_str(source_text);
736-
737730
let context_sub_hosts: Vec<ContextSubHost<'_>> = module_to_lint
738731
.section_module_records
739732
.into_iter()
@@ -751,11 +744,7 @@ impl Runtime {
751744
if !diagnostics.is_empty() {
752745
messages.lock().unwrap().extend(
753746
diagnostics.into_iter().map(|diagnostic| {
754-
oxc_diagnostic_to_message_with_position(
755-
diagnostic,
756-
source_text,
757-
rope,
758-
)
747+
Message::new(diagnostic, PossibleFixes::None)
759748
}),
760749
);
761750
}
@@ -780,10 +769,11 @@ impl Runtime {
780769
.insert(path.to_path_buf(), disable_directives);
781770
}
782771

783-
messages.lock().unwrap().extend(section_messages.iter().map(|message| {
784-
let message = message_cloner.clone_message(message);
785-
message_to_message_with_position(message, source_text, rope)
786-
}));
772+
messages.lock().unwrap().extend(
773+
section_messages
774+
.iter()
775+
.map(|message| message_cloner.clone_message(message)),
776+
);
787777
},
788778
);
789779
});

0 commit comments

Comments
 (0)