Skip to content

Commit 993f1a9

Browse files
committed
refactor(language-server): move generate_inverted_diagnostics to error_with_position
1 parent b7fb7ba commit 993f1a9

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

crates/oxc_language_server/src/linter/error_with_position.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,43 @@ pub fn message_with_position_to_lsp_diagnostic_report(
147147
rule_name: message.code.number.as_ref().map(std::string::ToString::to_string),
148148
}
149149
}
150+
151+
pub fn generate_inverted_diagnostics(
152+
diagnostics: &[DiagnosticReport],
153+
uri: &Uri,
154+
) -> Vec<DiagnosticReport> {
155+
let mut inverted_diagnostics = vec![];
156+
for d in diagnostics {
157+
let Some(related_info) = &d.diagnostic.related_information else {
158+
continue;
159+
};
160+
let related_information = Some(vec![DiagnosticRelatedInformation {
161+
location: lsp_types::Location { uri: uri.clone(), range: d.diagnostic.range },
162+
message: "original diagnostic".to_string(),
163+
}]);
164+
for r in related_info {
165+
if r.location.range == d.diagnostic.range {
166+
continue;
167+
}
168+
if r.message.is_empty() {
169+
continue;
170+
}
171+
inverted_diagnostics.push(DiagnosticReport {
172+
diagnostic: lsp_types::Diagnostic {
173+
range: r.location.range,
174+
severity: Some(lsp_types::DiagnosticSeverity::HINT),
175+
code: None,
176+
message: r.message.clone(),
177+
source: d.diagnostic.source.clone(),
178+
code_description: None,
179+
related_information: related_information.clone(),
180+
tags: None,
181+
data: None,
182+
},
183+
fixed_content: PossibleFixContent::None,
184+
rule_name: None,
185+
});
186+
}
187+
}
188+
inverted_diagnostics
189+
}

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use std::{
55

66
use log::debug;
77
use rustc_hash::FxHashSet;
8-
use tower_lsp_server::{
9-
UriExt,
10-
lsp_types::{self, DiagnosticRelatedInformation, DiagnosticSeverity, Uri},
11-
};
8+
use tower_lsp_server::{UriExt, lsp_types::Uri};
129

1310
use oxc_allocator::Allocator;
1411
use oxc_linter::{
@@ -18,7 +15,7 @@ use oxc_linter::{
1815
use oxc_linter::{RuntimeFileSystem, read_to_string};
1916

2017
use super::error_with_position::{
21-
DiagnosticReport, PossibleFixContent, message_with_position_to_lsp_diagnostic_report,
18+
DiagnosticReport, generate_inverted_diagnostics, message_with_position_to_lsp_diagnostic_report,
2219
};
2320

2421
/// smaller subset of LintServiceOptions, which is used by IsolatedLintHandler
@@ -99,42 +96,7 @@ impl IsolatedLintHandler {
9996
let mut diagnostics: Vec<DiagnosticReport> =
10097
errors.iter().map(|e| message_with_position_to_lsp_diagnostic_report(e, uri)).collect();
10198

102-
// a diagnostics connected from related_info to original diagnostic
103-
let mut inverted_diagnostics = vec![];
104-
for d in &diagnostics {
105-
let Some(related_info) = &d.diagnostic.related_information else {
106-
continue;
107-
};
108-
let related_information = Some(vec![DiagnosticRelatedInformation {
109-
location: lsp_types::Location { uri: uri.clone(), range: d.diagnostic.range },
110-
message: "original diagnostic".to_string(),
111-
}]);
112-
for r in related_info {
113-
if r.location.range == d.diagnostic.range {
114-
continue;
115-
}
116-
// If there is no message content for this span, then don't produce an additional diagnostic
117-
// which also has no content. This prevents issues where editors expect diagnostics to have messages.
118-
if r.message.is_empty() {
119-
continue;
120-
}
121-
inverted_diagnostics.push(DiagnosticReport {
122-
diagnostic: lsp_types::Diagnostic {
123-
range: r.location.range,
124-
severity: Some(DiagnosticSeverity::HINT),
125-
code: None,
126-
message: r.message.clone(),
127-
source: d.diagnostic.source.clone(),
128-
code_description: None,
129-
related_information: related_information.clone(),
130-
tags: None,
131-
data: None,
132-
},
133-
fixed_content: PossibleFixContent::None,
134-
rule_name: None,
135-
});
136-
}
137-
}
99+
let mut inverted_diagnostics = generate_inverted_diagnostics(&diagnostics, uri);
138100
diagnostics.append(&mut inverted_diagnostics);
139101
Some(diagnostics)
140102
}

crates/oxc_language_server/src/linter/tsgo_linter.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hash::FxHashSet;
1111
use tower_lsp_server::{UriExt, lsp_types::Uri};
1212

1313
use crate::linter::error_with_position::{
14-
DiagnosticReport, message_with_position_to_lsp_diagnostic_report,
14+
DiagnosticReport, generate_inverted_diagnostics, message_with_position_to_lsp_diagnostic_report,
1515
};
1616

1717
pub struct TsgoLinter {
@@ -35,12 +35,15 @@ impl TsgoLinter {
3535

3636
let messages = self.state.lint_source(&Arc::from(path.as_os_str()), source_text).ok()?;
3737

38-
Some(
39-
messages
40-
.iter()
41-
.map(|e| message_with_position_to_lsp_diagnostic_report(e, uri))
42-
.collect(),
43-
)
38+
let mut diagnostics: Vec<DiagnosticReport> = messages
39+
.iter()
40+
.map(|e| message_with_position_to_lsp_diagnostic_report(e, uri))
41+
.collect();
42+
43+
let mut inverted_diagnostics = generate_inverted_diagnostics(&diagnostics, uri);
44+
diagnostics.append(&mut inverted_diagnostics);
45+
46+
Some(diagnostics)
4447
}
4548

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

tasks/coverage/node-compat-table

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit ed0d6ba55790519d9ad3f6f776ca2cd303cc1e0b

0 commit comments

Comments
 (0)