Skip to content

Commit 9b475ad

Browse files
committed
refactor(linter): use one instance of rope per file (#11552)
1 parent cb17dae commit 9b475ad

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

crates/oxc_linter/src/service/offset_to_position.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ impl SpanPosition {
4646
}
4747
}
4848

49-
pub fn offset_to_position(offset: u32, source_text: &str) -> SpanPosition {
50-
// TODO(perf): share a single instance of `Rope`
51-
let rope = Rope::from_str(source_text);
52-
let (line, column) = get_line_column(&rope, offset, source_text);
49+
pub fn offset_to_position(rope: &Rope, offset: u32, source_text: &str) -> SpanPosition {
50+
let (line, column) = get_line_column(rope, offset, source_text);
5351
SpanPosition::new(line, column)
5452
}
5553

5654
#[cfg(test)]
5755
mod test {
56+
use oxc_data_structures::rope::Rope;
57+
5858
use super::offset_to_position;
5959

6060
#[test]
@@ -89,11 +89,11 @@ mod test {
8989
#[test]
9090
#[should_panic(expected = "out of bounds")]
9191
fn out_of_bounds() {
92-
offset_to_position(100, "foo");
92+
offset_to_position(&Rope::from_str("foo"), 100, "foo");
9393
}
9494

9595
fn assert_position(source: &str, offset: u32, expected: (u32, u32)) {
96-
let position = offset_to_position(offset, source);
96+
let position = offset_to_position(&Rope::from_str(source), offset, source);
9797
assert_eq!(position.line, expected.0);
9898
assert_eq!(position.character, expected.1);
9999
}

crates/oxc_linter/src/service/runtime.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ impl<'l> Runtime<'l> {
554554
allocator: &'a oxc_allocator::Allocator,
555555
) -> Vec<MessageWithPosition<'a>> {
556556
use oxc_allocator::CloneIn;
557+
use oxc_data_structures::rope::Rope;
557558
use std::sync::Mutex;
558559

559560
use crate::{
@@ -564,11 +565,12 @@ impl<'l> Runtime<'l> {
564565

565566
fn fix_to_fix_with_position<'a>(
566567
fix: &Fix<'a>,
568+
rope: &Rope,
567569
offset: u32,
568570
source_text: &str,
569571
) -> FixWithPosition<'a> {
570-
let start_position = offset_to_position(offset + fix.span.start, source_text);
571-
let end_position = offset_to_position(offset + fix.span.end, source_text);
572+
let start_position = offset_to_position(rope, offset + fix.span.start, source_text);
573+
let end_position = offset_to_position(rope, offset + fix.span.end, source_text);
572574
FixWithPosition {
573575
content: fix.content.clone(),
574576
span: SpanPositionMessage::new(start_position, end_position)
@@ -583,6 +585,8 @@ impl<'l> Runtime<'l> {
583585
module.content.with_dependent_mut(|owner, dependent| {
584586
assert_eq!(module.section_module_records.len(), dependent.len());
585587

588+
let rope = &Rope::from_str(&owner.source_text);
589+
586590
for (record_result, section) in
587591
module.section_module_records.into_iter().zip(dependent.drain(..))
588592
{
@@ -610,10 +614,12 @@ impl<'l> Runtime<'l> {
610614
.map(|labeled_span| {
611615
let offset = labeled_span.offset() as u32;
612616
let start_position = offset_to_position(
617+
rope,
613618
offset + section.source.start,
614619
&owner.source_text,
615620
);
616621
let end_position = offset_to_position(
622+
rope,
617623
offset
618624
+ section.source.start
619625
+ labeled_span.len() as u32,
@@ -647,6 +653,7 @@ impl<'l> Runtime<'l> {
647653
PossibleFixesWithPosition::Single(
648654
fix_to_fix_with_position(
649655
fix,
656+
rope,
650657
section.source.start,
651658
&owner.source_text,
652659
),
@@ -659,6 +666,7 @@ impl<'l> Runtime<'l> {
659666
.map(|fix| {
660667
fix_to_fix_with_position(
661668
fix,
669+
rope,
662670
section.source.start,
663671
&owner.source_text,
664672
)

0 commit comments

Comments
 (0)