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
57
58
59
60
61
62
use std::panic::Location;

use rnode::{FoldWith, VisitMutWith, VisitWith};
use serde::{Deserialize, Serialize};
use stc_visit::Visitable;
use swc_common::{EqIgnoreSpan, TypeEq};
use tracing::trace;

/// A type used to track all type creations. You can construct this type using
/// `Default::default()` and it will print a log message.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Tracker<const N: &'static str> {
    _priv: (),
}

impl<const N: &'static str> EqIgnoreSpan for Tracker<N> {
    #[inline]
    fn eq_ignore_span(&self, _: &Self) -> bool {
        true
    }
}

impl<const N: &'static str> TypeEq for Tracker<N> {
    #[inline]
    fn type_eq(&self, _: &Self) -> bool {
        true
    }
}

impl<const N: &'static str> Default for Tracker<N> {
    #[cfg_attr(debug_assertions, inline(never))]
    #[cfg_attr(not(debug_assertions), inline(always))]
    #[track_caller]
    fn default() -> Self {
        #[cfg(debug_assertions)]
        {
            let loc = Location::caller();
            trace!("Creating `{}` from {}", N, loc);
        }

        Self { _priv: () }
    }
}

impl<const N: &'static str> Visitable for Tracker<N> {}

impl<const N: &'static str, V: ?Sized> VisitWith<V> for Tracker<N> {
    #[inline]
    fn visit_children_with(&self, _: &mut V) {}
}

impl<const N: &'static str, V: ?Sized> VisitMutWith<V> for Tracker<N> {
    #[inline]
    fn visit_mut_children_with(&mut self, _: &mut V) {}
}

impl<const N: &'static str, V: ?Sized> FoldWith<V> for Tracker<N> {
    #[inline]
    fn fold_children_with(self, _: &mut V) -> Self {
        self
    }
}