Skip to content

Commit f599bef

Browse files
committed
refactor(language_server): move Message to DiagnosticReport transformation to one place (#14447)
next PR removes `MessageWithPosition` and all the code which used to be double.
1 parent fb4a20d commit f599bef

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ use tower_lsp_server::{UriExt, lsp_types::Uri};
1111
use oxc_allocator::Allocator;
1212
use oxc_linter::{
1313
AllowWarnDeny, ConfigStore, DirectivesStore, DisableDirectives, LINTABLE_EXTENSIONS,
14-
LintOptions, LintService, LintServiceOptions, Linter, RuntimeFileSystem,
14+
LintOptions, LintService, LintServiceOptions, Linter, Message, RuntimeFileSystem,
1515
create_unused_directives_diagnostics, read_to_arena_str, read_to_string,
1616
};
1717

1818
use super::error_with_position::{
19-
DiagnosticReport, MessageWithPosition, generate_inverted_diagnostics,
20-
message_to_message_with_position, message_with_position_to_lsp_diagnostic_report,
21-
oxc_diagnostic_to_message_with_position,
19+
DiagnosticReport, generate_inverted_diagnostics, message_to_message_with_position,
20+
message_with_position_to_lsp_diagnostic_report,
2221
};
2322

2423
/// smaller subset of LintServiceOptions, which is used by IsolatedLintHandler
@@ -104,49 +103,49 @@ impl IsolatedLintHandler {
104103
return None;
105104
}
106105

107-
let mut allocator = Allocator::default();
108106
let source_text = content.or_else(|| read_to_string(&path).ok())?;
109-
let errors = self.lint_path(&mut allocator, &path, &source_text);
110-
111-
let mut diagnostics: Vec<DiagnosticReport> =
112-
errors.iter().map(|e| message_with_position_to_lsp_diagnostic_report(e, uri)).collect();
113107

114-
let mut inverted_diagnostics = generate_inverted_diagnostics(&diagnostics, uri);
115-
diagnostics.append(&mut inverted_diagnostics);
108+
let mut diagnostics = self.lint_path(&path, uri, &source_text);
109+
diagnostics.append(&mut generate_inverted_diagnostics(&diagnostics, uri));
116110
Some(diagnostics)
117111
}
118112

119-
fn lint_path<'a>(
120-
&mut self,
121-
allocator: &'a mut Allocator,
122-
path: &Path,
123-
source_text: &str,
124-
) -> Vec<MessageWithPosition<'a>> {
113+
fn lint_path(&mut self, path: &Path, uri: &Uri, source_text: &str) -> Vec<DiagnosticReport> {
125114
debug!("lint {}", path.display());
115+
let mut allocator = Allocator::default();
126116
let rope = &Rope::from_str(source_text);
127117

128-
let mut messages: Vec<MessageWithPosition<'a>> = self
118+
let mut messages: Vec<DiagnosticReport> = self
129119
.service
130120
.with_file_system(Box::new(IsolatedLintHandlerFileSystem::new(
131121
path.to_path_buf(),
132122
Arc::from(source_text),
133123
)))
134124
.with_paths(vec![Arc::from(path.as_os_str())])
135-
.run_source(allocator)
125+
.run_source(&mut allocator)
136126
.into_iter()
137-
.map(|message| message_to_message_with_position(message, source_text, rope))
127+
.map(|message| {
128+
message_with_position_to_lsp_diagnostic_report(
129+
&message_to_message_with_position(message, source_text, rope),
130+
uri,
131+
)
132+
})
138133
.collect();
139134

140135
// Add unused directives if configured
141136
if let Some(severity) = self.unused_directives_severity
142137
&& let Some(directives) = self.directives_coordinator.get(path)
143138
{
144-
messages.extend(self.create_unused_directives_messages(
145-
&directives,
146-
severity,
147-
source_text,
148-
rope,
149-
));
139+
messages.extend(
140+
self.create_unused_directives_messages(&directives, severity).into_iter().map(
141+
|message| {
142+
message_with_position_to_lsp_diagnostic_report(
143+
&message_to_message_with_position(message, source_text, rope),
144+
uri,
145+
)
146+
},
147+
),
148+
);
150149
}
151150

152151
messages
@@ -157,15 +156,12 @@ impl IsolatedLintHandler {
157156
&self,
158157
directives: &DisableDirectives,
159158
severity: AllowWarnDeny,
160-
source_text: &str,
161-
rope: &Rope,
162-
) -> Vec<MessageWithPosition<'static>> {
159+
) -> Vec<Message<'_>> {
163160
let diagnostics = create_unused_directives_diagnostics(directives, severity);
164161
diagnostics
165162
.into_iter()
166-
.map(|diagnostic| {
167-
oxc_diagnostic_to_message_with_position(diagnostic, source_text, rope)
168-
})
163+
// TODO: unused directives should be fixable, `RuleCommentRule.create_fix()` can be used
164+
.map(|diagnostic| Message::new(diagnostic, oxc_linter::PossibleFixes::None))
169165
.collect()
170166
}
171167

0 commit comments

Comments
 (0)