Skip to content

Commit 7695393

Browse files
committed
refactor(linter): simplify offset adjustment by using Message.move_offset (#12595)
1 parent 2ceb835 commit 7695393

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

crates/oxc_diagnostics/src/service.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{
66
};
77

88
use cow_utils::CowUtils;
9-
use miette::LabeledSpan;
109
use percent_encoding::AsciiSet;
1110
#[cfg(not(windows))]
1211
use std::fs::canonicalize as strict_canonicalize;
@@ -122,7 +121,6 @@ impl DiagnosticService {
122121
cwd: C,
123122
path: P,
124123
source_text: &str,
125-
source_start: u32,
126124
diagnostics: Vec<OxcDiagnostic>,
127125
) -> Vec<Error> {
128126
// TODO: This causes snapshots to fail when running tests through a JetBrains terminal.
@@ -141,29 +139,7 @@ impl DiagnosticService {
141139
let source = Arc::new(NamedSource::new(path_display, source_text.to_owned()));
142140
diagnostics
143141
.into_iter()
144-
.map(|diagnostic| {
145-
if source_start == 0 {
146-
return diagnostic.with_source_code(Arc::clone(&source));
147-
}
148-
149-
match &diagnostic.labels {
150-
None => diagnostic.with_source_code(Arc::clone(&source)),
151-
Some(labels) => {
152-
let new_labels = labels
153-
.iter()
154-
.map(|labeled_span| {
155-
LabeledSpan::new(
156-
labeled_span.label().map(std::string::ToString::to_string),
157-
labeled_span.offset() + source_start as usize,
158-
labeled_span.len(),
159-
)
160-
})
161-
.collect::<Vec<_>>();
162-
163-
diagnostic.with_labels(new_labels).with_source_code(Arc::clone(&source))
164-
}
165-
}
166-
})
142+
.map(|diagnostic| diagnostic.with_source_code(Arc::clone(&source)))
167143
.collect()
168144
}
169145

crates/oxc_linter/src/service/runtime.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl Runtime {
494494

495495
// clippy: the source field is checked and assumed to be less than 4GB, and
496496
// we assume that the fix offset will not exceed 2GB in either direction
497-
#[expect(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
497+
#[expect(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
498498
pub(super) fn run(&mut self, tx_error: &DiagnosticSender) {
499499
rayon::scope(|scope| {
500500
self.resolve_modules(scope, true, tx_error, |me, mut module_to_lint| {
@@ -532,13 +532,19 @@ impl Runtime {
532532
.collect(),
533533
};
534534

535+
// adjust offset for multiple source text in a single file
536+
if section.source.start != 0 {
537+
for message in &mut messages {
538+
message.move_offset(section.source.start);
539+
}
540+
}
541+
535542
let source_text = section.source.source_text;
536543
if me.linter.options().fix.is_some() {
537544
let fix_result = Fixer::new(source_text, messages).fix();
538545
if fix_result.fixed {
539546
// write to file, replacing only the changed part
540-
let start =
541-
section.source.start.saturating_add_signed(fix_offset) as usize;
547+
let start = fix_offset as usize;
542548
let end = start + source_text.len();
543549
new_source_text
544550
.to_mut()
@@ -557,7 +563,6 @@ impl Runtime {
557563
&me.cwd,
558564
path,
559565
dep.source_text,
560-
section.source.start,
561566
errors,
562567
);
563568
tx_error.send((path.to_path_buf(), diagnostics)).unwrap();

0 commit comments

Comments
 (0)