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
37 changes: 10 additions & 27 deletions crates/oxc_language_server/src/linter/error_with_position.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::{borrow::Cow, str::FromStr};

use oxc_linter::{FixWithPosition, MessageWithPosition, PossibleFixesWithPosition};
use oxc_linter::{
FixWithPosition, MessageWithPosition, PossibleFixesWithPosition, SpanPositionMessage,
};
use tower_lsp_server::lsp_types::{
self, CodeDescription, DiagnosticRelatedInformation, DiagnosticSeverity, NumberOrString,
Position, Range, Uri,
};

use oxc_diagnostics::Severity;

use crate::LSP_MAX_INT;

#[derive(Debug, Clone, Default)]
pub struct DiagnosticReport {
pub diagnostic: lsp_types::Diagnostic,
Expand All @@ -31,13 +31,6 @@ pub enum PossibleFixContent {
Multiple(Vec<FixedContent>),
}

fn cmp_range(first: &Range, other: &Range) -> std::cmp::Ordering {
match first.start.cmp(&other.start) {
std::cmp::Ordering::Equal => first.end.cmp(&other.end),
o => o,
}
}

fn message_with_position_to_lsp_diagnostic(
message: &MessageWithPosition<'_>,
uri: &Uri,
Expand Down Expand Up @@ -71,24 +64,14 @@ fn message_with_position_to_lsp_diagnostic(
.collect()
});

let range = related_information.as_ref().map_or(
let range = message.labels.as_ref().map_or(Range::default(), |labels| {
let start = labels.first().map(SpanPositionMessage::start).cloned().unwrap_or_default();
let end = labels.first().map(SpanPositionMessage::end).cloned().unwrap_or_default();
Comment on lines +68 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you double check this is correct? I think it should be:

Suggested change
let start = labels.first().map(SpanPositionMessage::start).cloned().unwrap_or_default();
let end = labels.first().map(SpanPositionMessage::end).cloned().unwrap_or_default();
let (start, end) = labels
.iter()
.find(|span| span.primary())
.or_else(|| labels.first())
.map(|span| ((SpanPositionMessage::start(span), SpanPositionMessage::end(span))));

The reasoning, is that some rules - e.g. react/exhaustive deps, mark the primary span (out of the two), and this is where the red line should be

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh these aren't spans, and don't have a primary(), method on them.

Range {
start: Position { line: LSP_MAX_INT, character: LSP_MAX_INT },
end: Position { line: LSP_MAX_INT, character: LSP_MAX_INT },
},
|infos: &Vec<DiagnosticRelatedInformation>| {
let mut ret_range = Range {
start: Position { line: LSP_MAX_INT, character: LSP_MAX_INT },
end: Position { line: LSP_MAX_INT, character: LSP_MAX_INT },
};
for info in infos {
if cmp_range(&ret_range, &info.location.range) == std::cmp::Ordering::Greater {
ret_range = info.location.range;
}
}
ret_range
},
);
start: Position::new(start.line, start.character),
end: Position::new(end.line, end.character),
}
});
let code = message.code.to_string();
let code_description =
message.url.as_ref().map(|url| CodeDescription { href: Uri::from_str(url).ok().unwrap() });
Expand Down
4 changes: 0 additions & 4 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ type ConcurrentHashMap<K, V> = papaya::HashMap<K, V, FxBuildHasher>;

const OXC_CONFIG_FILE: &str = ".oxlintrc.json";

// max range for LSP integer is 2^31 - 1
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#baseTypes
const LSP_MAX_INT: u32 = 2u32.pow(31) - 1;

#[tokio::main]
async fn main() {
env_logger::init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fixed: Multiple(
code: "typescript-eslint(no-non-null-asserted-optional-chain)"
code_description.href: "https://oxc.rs/docs/guide/usage/linter/rules/typescript/no-non-null-asserted-optional-chain.html"
message: "Optional chain expressions can return undefined by design: using a non-null assertion is unsafe and wrong.\nhelp: Remove the non-null assertion."
range: Range { start: Position { line: 11, character: 18 }, end: Position { line: 11, character: 19 } }
range: Range { start: Position { line: 11, character: 21 }, end: Position { line: 11, character: 22 } }
related_information[0].message: "non-null assertion made after optional chain"
related_information[0].location.uri: "file://<variable>/fixtures/linter/issue_9958/issue.ts"
related_information[0].location.range: Range { start: Position { line: 11, character: 21 }, end: Position { line: 11, character: 22 } }
Expand Down Expand Up @@ -106,11 +106,11 @@ fixed: Multiple(

code: "None"
code_description.href: "None"
message: "non-null assertion made after optional chain"
range: Range { start: Position { line: 11, character: 21 }, end: Position { line: 11, character: 22 } }
message: "optional chain used"
range: Range { start: Position { line: 11, character: 18 }, end: Position { line: 11, character: 19 } }
related_information[0].message: "original diagnostic"
related_information[0].location.uri: "file://<variable>/fixtures/linter/issue_9958/issue.ts"
related_information[0].location.range: Range { start: Position { line: 11, character: 18 }, end: Position { line: 11, character: 19 } }
related_information[0].location.range: Range { start: Position { line: 11, character: 21 }, end: Position { line: 11, character: 22 } }
severity: Some(Hint)
source: Some("oxc")
tags: None
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 @@ -88,7 +88,7 @@ use crate::{

#[cfg(feature = "language_server")]
pub use crate::lsp::{
FixWithPosition, MessageWithPosition, PossibleFixesWithPosition,
FixWithPosition, MessageWithPosition, PossibleFixesWithPosition, SpanPositionMessage,
oxc_diagnostic_to_message_with_position,
};

Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl<'a> SpanPositionMessage<'a> {
Self { start, end, message: None }
}

#[must_use]
pub fn with_message(mut self, message: Option<Cow<'a, str>>) -> Self {
self.message = message;
self
Expand All @@ -38,7 +39,7 @@ impl<'a> SpanPositionMessage<'a> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct SpanPosition {
pub line: u32,
pub character: u32,
Expand Down
Loading