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
5 changes: 1 addition & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions crates/oxc_language_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_data_structures = { workspace = true, features = ["rope"] }
oxc_diagnostics = { workspace = true }
oxc_linter = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
oxc_linter = { workspace = true, features = ["language_server"] }

cow-utils = { workspace = true }
#
env_logger = { workspace = true, features = ["humantime"] }
futures = { workspace = true }
globset = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"./config/.oxlintrc.json"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugins": [
"import"
],
"rules": {
"import/no-cycle": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// should report cycle detected
import { b } from './dep-b.ts';

b();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this file is also included in dep-a.ts and dep-a.ts should report a no-cycle diagnostic
import './dep-a.ts';

export function b() { /* ... */ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// should report cycle detected
import { b } from './dep-b.ts';

b();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this file is also included in dep-a.ts and dep-a.ts should report a no-cycle diagnostic
import './dep-a.ts';

export function b() { /* ... */ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugins": [
"import"
],
"rules": {
"import/no-cycle": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// should report cycle detected
import { b } from './folder-dep-b.ts';

b();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this file is also included in folder-dep-a.ts and folder-dep-a.ts should report a no-cycle diagnostic
import './folder-dep-a.ts';

export function b() { /* ... */ }
220 changes: 88 additions & 132 deletions crates/oxc_language_server/src/linter/error_with_position.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,20 @@
use std::{path::PathBuf, str::FromStr};
use std::{borrow::Cow, path::PathBuf, str::FromStr};

use oxc_linter::MessageWithPosition;
use tower_lsp_server::{
UriExt,
lsp_types::{
self, CodeDescription, DiagnosticRelatedInformation, NumberOrString, Position, Range, Uri,
},
};

use cow_utils::CowUtils;
use oxc_diagnostics::{Error, Severity};

use crate::linter::offset_to_position;

const LINT_DOC_LINK_PREFIX: &str = "https://oxc.rs/docs/guide/usage/linter/rules";

#[derive(Debug)]
pub struct ErrorWithPosition {
pub start_pos: Position,
pub end_pos: Position,
pub miette_err: Error,
pub fixed_content: Option<FixedContent>,
pub labels_with_pos: Vec<LabeledSpanWithPosition>,
}

#[derive(Debug)]
pub struct LabeledSpanWithPosition {
pub start_pos: Position,
pub end_pos: Position,
pub message: Option<String>,
}
use oxc_diagnostics::Severity;

#[derive(Debug, Clone)]
pub struct DiagnosticReport {
pub diagnostic: lsp_types::Diagnostic,
pub fixed_content: Option<FixedContent>,
}
#[derive(Debug)]
pub struct ErrorReport {
pub error: Error,
pub fixed_content: Option<FixedContent>,
}

#[derive(Debug, Clone)]
pub struct FixedContent {
Expand All @@ -55,115 +30,96 @@ fn cmp_range(first: &Range, other: &Range) -> std::cmp::Ordering {
}
}

/// parse `OxcCode` to `Option<(scope, number)>`
fn parse_diagnostic_code(code: &str) -> Option<(&str, &str)> {
if !code.ends_with(')') {
return None;
}
let right_parenthesis_pos = code.rfind('(')?;
Some((&code[0..right_parenthesis_pos], &code[right_parenthesis_pos + 1..code.len() - 1]))
}

impl ErrorWithPosition {
pub fn new(
error: Error,
text: &str,
fixed_content: Option<FixedContent>,
start: usize,
) -> Self {
let labels = error.labels().map_or(vec![], Iterator::collect);
let labels_with_pos: Vec<LabeledSpanWithPosition> = labels
fn message_with_position_to_lsp_diagnostic(
message: &MessageWithPosition<'_>,
path: &PathBuf,
) -> lsp_types::Diagnostic {
let severity = match message.severity {
Severity::Error => Some(lsp_types::DiagnosticSeverity::ERROR),
_ => Some(lsp_types::DiagnosticSeverity::WARNING),
};
let uri = lsp_types::Uri::from_file_path(path).unwrap();

let related_information = message.labels.as_ref().map(|spans| {
spans
.iter()
.map(|labeled_span| LabeledSpanWithPosition {
start_pos: offset_to_position(labeled_span.offset() + start, text),
end_pos: offset_to_position(
labeled_span.offset() + start + labeled_span.len(),
text,
),
message: labeled_span.label().map(ToString::to_string),
})
.collect();

let start_pos = labels_with_pos[0].start_pos;
let end_pos = labels_with_pos[labels_with_pos.len() - 1].end_pos;

Self { miette_err: error, start_pos, end_pos, labels_with_pos, fixed_content }
}

fn to_lsp_diagnostic(&self, path: &PathBuf) -> lsp_types::Diagnostic {
let severity = match self.miette_err.severity() {
Some(Severity::Error) => Some(lsp_types::DiagnosticSeverity::ERROR),
_ => Some(lsp_types::DiagnosticSeverity::WARNING),
};
let related_information = Some(
self.labels_with_pos
.iter()
.map(|labeled_span| lsp_types::DiagnosticRelatedInformation {
location: lsp_types::Location {
uri: lsp_types::Uri::from_file_path(path).unwrap(),
range: lsp_types::Range {
start: lsp_types::Position {
line: labeled_span.start_pos.line,
character: labeled_span.start_pos.character,
},
end: lsp_types::Position {
line: labeled_span.end_pos.line,
character: labeled_span.end_pos.character,
},
.map(|span| lsp_types::DiagnosticRelatedInformation {
location: lsp_types::Location {
uri: uri.clone(),
range: lsp_types::Range {
start: lsp_types::Position {
line: span.start().line,
character: span.start().character,
},
end: lsp_types::Position {
line: span.end().line,
character: span.end().character,
},
},
message: labeled_span.message.clone().unwrap_or_default(),
})
.collect(),
);
let range = related_information.as_ref().map_or(
Range { start: self.start_pos, end: self.end_pos },
|infos: &Vec<DiagnosticRelatedInformation>| {
let mut ret_range = Range {
start: Position { line: u32::MAX, character: u32::MAX },
end: Position { line: u32::MAX, character: u32::MAX },
};
for info in infos {
if cmp_range(&ret_range, &info.location.range) == std::cmp::Ordering::Greater {
ret_range = info.location.range;
}
}
ret_range
},
);
let code = self.miette_err.code().map(|item| item.to_string());
let code_description = code.as_ref().and_then(|code| {
let (scope, number) = parse_diagnostic_code(code)?;
Some(CodeDescription {
href: Uri::from_str(&format!(
"{LINT_DOC_LINK_PREFIX}/{}/{number}",
scope.strip_prefix("eslint-plugin-").unwrap_or(scope).cow_replace("-", "_")
))
.ok()?,
},
message: span.message().unwrap_or(&Cow::Borrowed("")).to_string(),
})
});
let message = self.miette_err.help().map_or_else(
|| self.miette_err.to_string(),
|help| format!("{}\nhelp: {}", self.miette_err, help),
);

lsp_types::Diagnostic {
range,
severity,
code: code.map(NumberOrString::String),
message,
source: Some("oxc".into()),
code_description,
related_information,
tags: None,
data: None,
}
.collect()
});

let range = related_information.as_ref().map_or(
Range {
start: Position { line: u32::MAX, character: u32::MAX },
end: Position { line: u32::MAX, character: u32::MAX },
},
|infos: &Vec<DiagnosticRelatedInformation>| {
let mut ret_range = Range {
start: Position { line: u32::MAX, character: u32::MAX },
end: Position { line: u32::MAX, character: u32::MAX },
};
for info in infos {
if cmp_range(&ret_range, &info.location.range) == std::cmp::Ordering::Greater {
ret_range = info.location.range;
}
}
ret_range
},
);
let code = message.code.to_string();
let code_description =
message.url.as_ref().map(|url| CodeDescription { href: Uri::from_str(url).ok().unwrap() });
let message = message.help.as_ref().map_or_else(
|| message.message.to_string(),
|help| format!("{}\nhelp: {}", message.message, help),
);

lsp_types::Diagnostic {
range,
severity,
code: Some(NumberOrString::String(code)),
message,
source: Some("oxc".into()),
code_description,
related_information,
tags: None,
data: None,
}
}

pub fn into_diagnostic_report(self, path: &PathBuf) -> DiagnosticReport {
DiagnosticReport {
diagnostic: self.to_lsp_diagnostic(path),
fixed_content: self.fixed_content,
}
pub fn message_with_position_to_lsp_diagnostic_report(
message: &MessageWithPosition<'_>,
path: &PathBuf,
) -> DiagnosticReport {
DiagnosticReport {
diagnostic: message_with_position_to_lsp_diagnostic(message, path),
fixed_content: message.fix.as_ref().map(|infos| FixedContent {
message: infos.span.message().map(std::string::ToString::to_string),
code: infos.content.to_string(),
range: Range {
start: Position {
line: infos.span.start().line,
character: infos.span.start().character,
},
end: Position {
line: infos.span.end().line,
character: infos.span.end().character,
},
},
}),
}
}
Loading
Loading