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
64 changes: 64 additions & 0 deletions crates/oxc_linter/src/fixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::service::offset_to_position::SpanPositionMessage;
#[cfg(feature = "language_server")]
pub use fix::{FixWithPosition, PossibleFixesWithPosition};
#[cfg(feature = "language_server")]
use oxc_data_structures::rope::Rope;
#[cfg(feature = "language_server")]
use oxc_diagnostics::{OxcCode, Severity};

mod fix;
Expand Down Expand Up @@ -260,6 +262,68 @@ impl From<OxcDiagnostic> for MessageWithPosition<'_> {
}
}

// 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
#[cfg(feature = "language_server")]
#[expect(clippy::cast_possible_truncation)]
pub fn message_to_message_with_position<'a>(
message: &Message<'a>,
source_text: &str,
rope: &Rope,
) -> MessageWithPosition<'a> {
use crate::service::offset_to_position::offset_to_position;

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(),
),
},
}
}

#[cfg(feature = "language_server")]
fn fix_to_fix_with_position<'a>(
fix: &Fix<'a>,
rope: &Rope,
source_text: &str,
) -> FixWithPosition<'a> {
use crate::service::offset_to_position::offset_to_position;

let start_position = offset_to_position(rope, fix.span.start, source_text);
let end_position = offset_to_position(rope, fix.span.end, source_text);
FixWithPosition {
content: fix.content.clone(),
span: SpanPositionMessage::new(start_position, end_position)
.with_message(fix.message.as_ref().map(|label| Cow::Owned(label.to_string()))),
}
}

impl<'new> CloneIn<'new> for Message<'_> {
type Cloned = Message<'new>;

Expand Down
83 changes: 2 additions & 81 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,9 @@ impl Runtime {
});
}

// 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
// language_server: the language server needs line and character position
// the struct not using `oxc_diagnostic::Error, because we are just collecting information
// and returning it to the client to let him display it.
#[expect(clippy::cast_possible_truncation)]
#[cfg(feature = "language_server")]
pub(super) fn run_source<'a>(
&mut self,
Expand All @@ -682,25 +679,7 @@ impl Runtime {

use oxc_data_structures::rope::Rope;

use crate::{
FixWithPosition,
fixer::{Fix, PossibleFixesWithPosition},
service::offset_to_position::{SpanPositionMessage, offset_to_position},
};

fn fix_to_fix_with_position<'a>(
fix: &Fix<'a>,
rope: &Rope,
source_text: &str,
) -> FixWithPosition<'a> {
let start_position = offset_to_position(rope, fix.span.start, source_text);
let end_position = offset_to_position(rope, fix.span.end, source_text);
FixWithPosition {
content: fix.content.clone(),
span: SpanPositionMessage::new(start_position, end_position)
.with_message(fix.message.as_ref().map(|label| Cow::Owned(label.to_string()))),
}
}
use crate::fixer::message_to_message_with_position;

// Wrap allocator in `MessageCloner` so can clone `Message`s into it
let message_cloner = MessageCloner::new(allocator);
Expand Down Expand Up @@ -748,65 +727,7 @@ impl Runtime {
|message| {
let message = message_cloner.clone_message(message);

let labels = &message.error.labels.clone().map(|labels| {
labels
.into_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: labels.clone(),
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(),
)
}
},
}
message_to_message_with_position(&message, source_text, rope)
},
));
}
Expand Down
Loading