Skip to main content

rspack_core/dependency/
dependency_location.rs

1use std::fmt::Debug;
2
3use rspack_cacheable::cacheable;
4use rspack_util::SpanExt;
5
6/// Represents a range in a dependency, typically used for tracking the span of code in a source file.
7/// It stores the start and end positions (as offsets) of the range, typically using base-0 indexing.
8#[cacheable]
9#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Default, rspack_hash::RspackHash)]
10pub struct DependencyRange {
11  pub start: u32,
12  pub end: u32,
13}
14
15impl From<(u32, u32)> for DependencyRange {
16  fn from(range: (u32, u32)) -> Self {
17    Self {
18      start: range.0,
19      end: range.1,
20    }
21  }
22}
23
24impl From<swc_core::common::Span> for DependencyRange {
25  fn from(span: swc_core::common::Span) -> Self {
26    Self {
27      start: span.real_lo(),
28      end: span.real_hi(),
29    }
30  }
31}
32
33impl From<swc_experimental_ecma_ast::Span> for DependencyRange {
34  fn from(span: swc_experimental_ecma_ast::Span) -> Self {
35    Self {
36      start: span.real_lo(),
37      end: span.real_hi(),
38    }
39  }
40}
41
42impl DependencyRange {
43  pub fn new(start: u32, end: u32) -> Self {
44    DependencyRange { start, end }
45  }
46}