swc_ecma_compat_es2015/classes/
prop_name.rs1use swc_atoms::Wtf8Atom;
2use swc_common::{Span, Spanned};
3use swc_ecma_ast::*;
4
5#[derive(Debug, PartialEq, Eq, Hash)]
6pub enum HashKey {
7 Str(Wtf8Atom),
8 Computed(Span),
10}
11
12impl From<&PropName> for HashKey {
13 fn from(p: &PropName) -> Self {
14 match p {
15 PropName::Ident(IdentName { sym: value, .. }) => HashKey::Str(value.clone().into()),
16 PropName::Str(Str { value, .. }) => HashKey::Str(value.clone()),
17 PropName::Num(Number { value, .. }) => HashKey::Str(value.to_string().into()),
18 PropName::BigInt(BigInt { value, .. }) => HashKey::Str(value.to_string().into()),
19 PropName::Computed(expr) => HashKey::Computed(expr.span()),
20 #[cfg(swc_ast_unknown)]
21 _ => panic!("unable to access unknown nodes"),
22 }
23 }
24}
25
26pub fn is_pure_prop_name(p: &PropName) -> bool {
27 match p {
28 PropName::Ident(..) => true,
29 PropName::Str(..) => true,
30 PropName::Num(..) => true,
31 PropName::BigInt(..) => true,
32 PropName::Computed(ComputedPropName { expr, .. }) => match &**expr {
33 Expr::This(..) => true,
34 Expr::Lit(..) => true,
35 Expr::Ident(..) => true,
36 Expr::PrivateName(..) => true,
37 Expr::Tpl(tpl) => tpl.exprs.is_empty(),
38 _ => false,
39 },
40 #[cfg(swc_ast_unknown)]
41 _ => false,
42 }
43}
44
45pub fn should_extract_class_prop_key(methods: &[ClassMethod]) -> bool {
46 let mut has_static = false;
47
48 for m in methods {
49 if is_pure_prop_name(&m.key) {
50 continue;
51 }
52
53 if m.is_static {
54 has_static = true
55 } else if has_static {
56 return true;
57 }
58 }
59
60 false
61}