diff --git a/crates/oxc_diagnostics/src/service.rs b/crates/oxc_diagnostics/src/service.rs index 4ef7387068f1d..e2422de582b71 100644 --- a/crates/oxc_diagnostics/src/service.rs +++ b/crates/oxc_diagnostics/src/service.rs @@ -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; @@ -122,7 +121,6 @@ impl DiagnosticService { cwd: C, path: P, source_text: &str, - source_start: u32, diagnostics: Vec, ) -> Vec { // TODO: This causes snapshots to fail when running tests through a JetBrains terminal. @@ -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::>(); - - diagnostic.with_labels(new_labels).with_source_code(Arc::clone(&source)) - } - } - }) + .map(|diagnostic| diagnostic.with_source_code(Arc::clone(&source))) .collect() } diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index b52355a6f6d68..a221384aa4649 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -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| { @@ -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() @@ -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();