1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![allow(clippy::wrong_self_convention)]

use swc_common::{Span, TypeEq};

use crate::dev_span;

pub trait ValueExt: Sized {
    fn as_ok<E>(self) -> Result<Self, E> {
        Ok(self)
    }

    fn as_some(self) -> Option<Self> {
        Some(self)
    }
}

impl<T> ValueExt for T {}

pub trait SpanExt: Into<Span> + Copy {
    /// If `self` is dummy, use span from `other`.
    fn or_else<F>(self, other: F) -> Span
    where
        F: FnOnce() -> Span,
    {
        let s = self.into();
        if !s.is_dummy() {
            return s;
        }

        other()
    }
}

impl<T> SpanExt for T where T: Into<Span> + Copy {}

pub trait TypeVecExt {
    fn dedup_type(&mut self);
}

impl<T> TypeVecExt for Vec<T>
where
    T: TypeEq,
{
    fn dedup_type(&mut self) {
        let _tracing = dev_span!("dedup_type");

        let mut types: Vec<T> = Vec::with_capacity(self.capacity());
        for ty in self.drain(..) {
            if types.iter().any(|stored| stored.type_eq(&ty)) {
                continue;
            }
            types.push(ty);
        }
        *self = types;
    }
}