Skip to content

Commit 5adcb98

Browse files
committed
refactor(linter): use u32 to keep track of last fixed source text position (#12696)
Only use case I could think about, where something bigger then u32 is needed: Parsing an under `u32` size file and fixing it will exceed `u32` file size.
1 parent a47cbbc commit 5adcb98

File tree

1 file changed

+5
-6
lines changed
  • crates/oxc_linter/src/fixer

1 file changed

+5
-6
lines changed

crates/oxc_linter/src/fixer/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'a> Fixer<'a> {
372372
self.messages.sort_unstable_by_key(|m| m.fixes.span());
373373
let mut fixed = false;
374374
let mut output = String::with_capacity(source_text.len());
375-
let mut last_pos: i64 = -1;
375+
let mut last_pos: u32 = 0;
376376

377377
// only keep messages that were not fixed
378378
let mut filtered_messages = Vec::with_capacity(self.messages.len());
@@ -396,21 +396,20 @@ impl<'a> Fixer<'a> {
396396
filtered_messages.push(m);
397397
continue;
398398
}
399-
if i64::from(start) < last_pos {
399+
if start < last_pos {
400400
filtered_messages.push(m);
401401
continue;
402402
}
403403

404404
m.fixed = true;
405405
fixed = true;
406-
let offset = usize::try_from(last_pos.max(0)).ok().unwrap();
406+
let offset = last_pos as usize;
407407
output.push_str(&source_text[offset..start as usize]);
408408
output.push_str(content);
409-
last_pos = i64::from(end);
409+
last_pos = end;
410410
}
411411

412-
let offset = usize::try_from(last_pos.max(0)).ok().unwrap();
413-
output.push_str(&source_text[offset..]);
412+
output.push_str(&source_text[last_pos as usize..]);
414413

415414
filtered_messages.sort_unstable_by_key(GetSpan::span);
416415
FixResult { fixed, fixed_code: Cow::Owned(output), messages: filtered_messages }

0 commit comments

Comments
 (0)