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: 5 additions & 0 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,9 @@ impl OxcDiagnostic {
pub fn with_source_code<T: SourceCode + Send + Sync + 'static>(self, code: T) -> Error {
Error::from(self).with_source_code(code)
}

/// Consumes the diagnostic and returns the inner owned data.
pub fn inner_owned(self) -> OxcDiagnosticInner {
*self.inner
}
}
65 changes: 22 additions & 43 deletions crates/oxc_linter/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ pub struct MessageWithPosition<'a> {
// we assume that the fix offset will not exceed 2GB in either direction
#[expect(clippy::cast_possible_truncation)]
pub fn oxc_diagnostic_to_message_with_position<'a>(
diagnostic: &OxcDiagnostic,
diagnostic: OxcDiagnostic,
source_text: &str,
rope: &Rope,
) -> MessageWithPosition<'a> {
let labels = diagnostic.labels.as_ref().map(|labels| {
let inner = diagnostic.inner_owned();

let labels = inner.labels.as_ref().map(|labels| {
labels
.iter()
.map(|labeled_span| {
Expand All @@ -89,56 +91,33 @@ pub fn oxc_diagnostic_to_message_with_position<'a>(
});

MessageWithPosition {
message: diagnostic.message.clone(),
severity: diagnostic.severity,
help: diagnostic.help.clone(),
url: diagnostic.url.clone(),
code: diagnostic.code.clone(),
message: inner.message,
severity: inner.severity,
help: inner.help,
url: inner.url,
code: inner.code,
labels,
fixes: PossibleFixesWithPosition::None,
}
}

// clippy: the source field is checked and assumed to be less than 4GB, and
// we assume that the fix offset will not exceed 2GB in either direction
#[expect(clippy::cast_possible_truncation)]
pub fn message_to_message_with_position<'a>(
message: &Message<'a>,
message: Message<'a>,
source_text: &str,
rope: &Rope,
) -> MessageWithPosition<'a> {
let labels = message.error.labels.as_ref().map(|labels| {
labels
.iter()
.map(|labeled_span| {
let offset = labeled_span.offset() as u32;
let start_position = offset_to_position(rope, offset, source_text);
let end_position =
offset_to_position(rope, offset + labeled_span.len() as u32, source_text);
let message = labeled_span.label().map(|label| Cow::Owned(label.to_string()));

SpanPositionMessage::new(start_position, end_position).with_message(message)
})
.collect::<Vec<_>>()
});

MessageWithPosition {
message: message.error.message.clone(),
severity: message.error.severity,
help: message.error.help.clone(),
url: message.error.url.clone(),
code: message.error.code.clone(),
labels,
fixes: match &message.fixes {
PossibleFixes::None => PossibleFixesWithPosition::None,
PossibleFixes::Single(fix) => {
PossibleFixesWithPosition::Single(fix_to_fix_with_position(fix, rope, source_text))
}
PossibleFixes::Multiple(fixes) => PossibleFixesWithPosition::Multiple(
fixes.iter().map(|fix| fix_to_fix_with_position(fix, rope, source_text)).collect(),
),
},
}
let mut result = oxc_diagnostic_to_message_with_position(message.error, source_text, rope);
result.fixes = match &message.fixes {
PossibleFixes::None => PossibleFixesWithPosition::None,
PossibleFixes::Single(fix) => {
PossibleFixesWithPosition::Single(fix_to_fix_with_position(fix, rope, source_text))
}
PossibleFixes::Multiple(fixes) => PossibleFixesWithPosition::Multiple(
fixes.iter().map(|fix| fix_to_fix_with_position(fix, rope, source_text)).collect(),
),
};

result
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ impl Runtime {
messages.lock().unwrap().extend(
diagnostics.into_iter().map(|diagnostic| {
oxc_diagnostic_to_message_with_position(
&diagnostic,
diagnostic,
source_text,
rope,
)
Expand All @@ -754,7 +754,7 @@ impl Runtime {
messages.lock().unwrap().extend(section_messages.iter().map(|message| {
let message = message_cloner.clone_message(message);

message_to_message_with_position(&message, source_text, rope)
message_to_message_with_position(message, source_text, rope)
}));
},
);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/tsgolint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl TsGoLintState {

let mut message_with_position: MessageWithPosition<'_> =
message_to_message_with_position(
&Message::from_tsgo_lint_diagnostic(
Message::from_tsgo_lint_diagnostic(
tsgolint_diagnostic,
&source_text,
),
Expand Down
Loading