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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::{
    cmp::PartialEq,
    fmt::{self, Debug, Display, Formatter},
};

use rnode::NodeId;
use serde::{Deserialize, Serialize};
use stc_ts_ast_rnode::{RIdent, RModuleExportName, RTsEntityName};
use stc_visit::Visit;
use swc_atoms::JsWord;
use swc_common::{EqIgnoreSpan, SyntaxContext, TypeEq, DUMMY_SP};
use swc_ecma_ast::Ident;
use swc_ecma_utils::ident::IdentLike;

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EqIgnoreSpan, TypeEq, Visit, Serialize, Deserialize)]
pub struct Id {
    sym: JsWord,
    ctxt: SyntaxContext,
}

impl Id {
    pub const fn new(sym: JsWord, ctxt: SyntaxContext) -> Self {
        Self { sym, ctxt }
    }

    pub fn as_str(&self) -> &str {
        &self.sym
    }

    pub const fn ctxt(&self) -> SyntaxContext {
        self.ctxt
    }

    pub fn sym(&self) -> &JsWord {
        &self.sym
    }

    pub fn word(sym: JsWord) -> Self {
        Id {
            sym,
            ctxt: SyntaxContext::empty(),
        }
    }
}

impl From<&'_ RIdent> for Id {
    fn from(i: &RIdent) -> Self {
        Id {
            sym: i.sym.clone(),
            ctxt: i.span.ctxt(),
        }
    }
}

impl From<RIdent> for Id {
    fn from(i: RIdent) -> Self {
        Id {
            sym: i.sym,
            ctxt: i.span.ctxt(),
        }
    }
}

impl From<Id> for RIdent {
    fn from(i: Id) -> Self {
        RIdent {
            node_id: NodeId::invalid(),
            span: DUMMY_SP.with_ctxt(i.ctxt),
            sym: i.sym,
            optional: false,
        }
    }
}

impl From<Id> for RTsEntityName {
    fn from(i: Id) -> Self {
        RTsEntityName::Ident(i.into())
    }
}

impl PartialEq<&'_ str> for Id {
    fn eq(&self, other: &&'_ str) -> bool {
        *self.sym == **other
    }
}

/// This makes life easier.
impl Display for Id {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}{:?}", self.sym, self.ctxt)
    }
}

impl PartialEq<Ident> for Id {
    fn eq(&self, other: &Ident) -> bool {
        self.sym == other.sym && self.ctxt == other.span.ctxt()
    }
}

impl PartialEq<&'_ Ident> for Id {
    fn eq(&self, other: &&Ident) -> bool {
        self.sym == other.sym && self.ctxt == other.span.ctxt()
    }
}

impl PartialEq<RIdent> for Id {
    fn eq(&self, other: &RIdent) -> bool {
        self.sym == other.sym && self.ctxt == other.span.ctxt()
    }
}

impl PartialEq<&'_ RIdent> for Id {
    fn eq(&self, other: &&RIdent) -> bool {
        self.sym == other.sym && self.ctxt == other.span.ctxt()
    }
}

impl IdentLike for Id {
    fn from_ident(i: &Ident) -> Self {
        Self {
            sym: i.sym.clone(),
            ctxt: i.span.ctxt,
        }
    }

    fn to_id(&self) -> (JsWord, SyntaxContext) {
        (self.sym.clone(), self.ctxt)
    }

    fn into_id(self) -> (JsWord, SyntaxContext) {
        (self.sym, self.ctxt)
    }
}

impl Debug for Id {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(self, f)
    }
}

impl PartialEq<str> for Id {
    fn eq(&self, other: &str) -> bool {
        self.sym == *other
    }
}

impl From<RModuleExportName> for Id {
    fn from(v: RModuleExportName) -> Self {
        match v {
            RModuleExportName::Ident(i) => i.into(),
            RModuleExportName::Str(s) => Id::word(s.value),
        }
    }
}

impl From<&'_ RModuleExportName> for Id {
    fn from(v: &RModuleExportName) -> Self {
        match v {
            RModuleExportName::Ident(i) => i.into(),
            RModuleExportName::Str(s) => Id::word(s.value.clone()),
        }
    }
}