swc_ecma_compat_es2015/classes/
prop_name.rs

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
use swc_atoms::JsWord;
use swc_common::{Span, Spanned};
use swc_ecma_ast::*;

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum HashKey {
    Str(JsWord),
    /// Not for key merging
    Computed(Span),
}

impl From<&PropName> for HashKey {
    fn from(p: &PropName) -> Self {
        match p {
            PropName::Ident(IdentName { sym: value, .. }) | PropName::Str(Str { value, .. }) => {
                HashKey::Str(value.clone())
            }
            PropName::Num(Number { value, .. }) => HashKey::Str(value.to_string().into()),
            PropName::BigInt(BigInt { value, .. }) => HashKey::Str(value.to_string().into()),
            PropName::Computed(expr) => HashKey::Computed(expr.span()),
        }
    }
}

pub fn is_pure_prop_name(p: &PropName) -> bool {
    match p {
        PropName::Ident(..) => true,
        PropName::Str(..) => true,
        PropName::Num(..) => true,
        PropName::BigInt(..) => true,
        PropName::Computed(ComputedPropName { expr, .. }) => match &**expr {
            Expr::This(..) => true,
            Expr::Lit(..) => true,
            Expr::Ident(..) => true,
            Expr::PrivateName(..) => true,
            Expr::Tpl(tpl) => tpl.exprs.is_empty(),
            _ => false,
        },
    }
}

pub fn should_extract_class_prop_key(methods: &[ClassMethod]) -> bool {
    let mut has_static = false;

    for m in methods {
        if is_pure_prop_name(&m.key) {
            continue;
        }

        if m.is_static {
            has_static = true
        } else if has_static {
            return true;
        }
    }

    false
}