1use swc_atoms::Atom;
2use swc_common::SyntaxContext;
3use swc_ecma_ast::{unsafe_id_from_ident, BindingIdent, Id, Ident, UnsafeId};
4
5pub trait IdentLike: Sized + Send + Sync + 'static {
6 fn from_ident(i: &Ident) -> Self;
7 fn to_id(&self) -> Id;
8 fn into_id(self) -> Id;
9}
10
11impl IdentLike for Atom {
12 fn from_ident(i: &Ident) -> Self {
13 i.sym.clone()
14 }
15
16 fn to_id(&self) -> Id {
17 (self.clone(), Default::default())
18 }
19
20 fn into_id(self) -> Id {
21 (self, Default::default())
22 }
23}
24
25impl IdentLike for BindingIdent {
26 fn from_ident(i: &Ident) -> Self {
27 i.clone().into()
28 }
29
30 fn to_id(&self) -> Id {
31 (self.sym.clone(), self.ctxt)
32 }
33
34 fn into_id(self) -> Id {
35 self.id.into_id()
36 }
37}
38
39impl IdentLike for (Atom, SyntaxContext) {
40 #[inline]
41 fn from_ident(i: &Ident) -> Self {
42 (i.sym.clone(), i.ctxt)
43 }
44
45 #[inline]
46 fn to_id(&self) -> Id {
47 (self.0.clone(), self.1)
48 }
49
50 #[inline]
51 fn into_id(self) -> Id {
52 self
53 }
54}
55
56impl IdentLike for Ident {
57 #[inline]
58 fn from_ident(i: &Ident) -> Self {
59 Ident::new(i.sym.clone(), i.span, i.ctxt)
60 }
61
62 #[inline]
63 fn to_id(&self) -> Id {
64 (self.sym.clone(), self.ctxt)
65 }
66
67 #[inline]
68 fn into_id(self) -> Id {
69 (self.sym, self.ctxt)
70 }
71}
72
73impl IdentLike for UnsafeId {
74 fn from_ident(i: &Ident) -> Self {
75 unsafe { unsafe_id_from_ident(i) }
76 }
77
78 fn to_id(&self) -> Id {
79 unreachable!("UnsafeId.to_id() is not allowed because it is very likely to be unsafe")
80 }
81
82 fn into_id(self) -> Id {
83 unreachable!("UnsafeId.into_id() is not allowed because it is very likely to be unsafe")
84 }
85}