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
26 changes: 1 addition & 25 deletions crates/oxc_diagnostics/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
};

use cow_utils::CowUtils;
use miette::LabeledSpan;
use percent_encoding::AsciiSet;
#[cfg(not(windows))]
use std::fs::canonicalize as strict_canonicalize;
Expand Down Expand Up @@ -122,7 +121,6 @@ impl DiagnosticService {
cwd: C,
path: P,
source_text: &str,
source_start: u32,
diagnostics: Vec<OxcDiagnostic>,
) -> Vec<Error> {
// TODO: This causes snapshots to fail when running tests through a JetBrains terminal.
Expand All @@ -141,29 +139,7 @@ impl DiagnosticService {
let source = Arc::new(NamedSource::new(path_display, source_text.to_owned()));
diagnostics
.into_iter()
.map(|diagnostic| {
if source_start == 0 {
return diagnostic.with_source_code(Arc::clone(&source));
}

match &diagnostic.labels {
None => diagnostic.with_source_code(Arc::clone(&source)),
Some(labels) => {
let new_labels = labels
.iter()
.map(|labeled_span| {
LabeledSpan::new(
labeled_span.label().map(std::string::ToString::to_string),
labeled_span.offset() + source_start as usize,
labeled_span.len(),
)
})
.collect::<Vec<_>>();

diagnostic.with_labels(new_labels).with_source_code(Arc::clone(&source))
}
}
})
.map(|diagnostic| diagnostic.with_source_code(Arc::clone(&source)))
.collect()
}

Expand Down
13 changes: 9 additions & 4 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ 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
#[expect(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
#[expect(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
pub(super) fn run(&mut self, tx_error: &DiagnosticSender) {
rayon::scope(|scope| {
self.resolve_modules(scope, true, tx_error, |me, mut module_to_lint| {
Expand Down Expand Up @@ -532,13 +532,19 @@ impl Runtime {
.collect(),
};

// adjust offset for multiple source text in a single file
if section.source.start != 0 {
for message in &mut messages {
message.move_offset(section.source.start);
}
}

let source_text = section.source.source_text;
if me.linter.options().fix.is_some() {
let fix_result = Fixer::new(source_text, messages).fix();
if fix_result.fixed {
// write to file, replacing only the changed part
let start =
section.source.start.saturating_add_signed(fix_offset) as usize;
let start = fix_offset as usize;
let end = start + source_text.len();
new_source_text
.to_mut()
Expand All @@ -557,7 +563,6 @@ impl Runtime {
&me.cwd,
path,
dep.source_text,
section.source.start,
errors,
);
tx_error.send((path.to_path_buf(), diagnostics)).unwrap();
Expand Down
Loading