swc/common/src/pos/mod.rs

55 lines
1.0 KiB
Rust
Raw Normal View History

use fold::FoldWith;
pub use swc_macros::Spanned;
pub use syntax_pos::{
hygiene, BytePos, ExpnFormat, ExpnInfo, FileName, MultiSpan, SourceFile, Span, SpanData,
SyntaxContext, DUMMY_SP, NO_EXPANSION,
};
///
/// # Derive
/// This trait can be derived with `#[derive(Spanned)]`.
pub trait Spanned {
/// Get span of `self`.
fn span(&self) -> Span;
}
2018-03-04 08:41:59 +03:00
impl Spanned for Span {
#[inline(always)]
fn span(&self) -> Span {
*self
}
}
impl Spanned for BytePos {
/// Creates a new single-byte span.
#[inline(always)]
fn span(&self) -> Span {
Span::new(*self, *self, Default::default())
}
}
impl<S> Spanned for Box<S>
where
S: ?Sized + Spanned,
{
fn span(&self) -> Span {
<S as Spanned>::span(&*self)
}
}
2018-03-04 08:12:17 +03:00
impl<'a, S> Spanned for &'a S
where
S: ?Sized + Spanned,
{
fn span(&self) -> Span {
<S as Spanned>::span(&*self)
}
}
impl<F> FoldWith<F> for Span {
/// No op as span does not have any child.
fn fold_children(self, _: &mut F) -> Span {
self
}
}