swc_bundler/bundler/
keywords.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
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
use swc_common::{collections::AHashMap, util::take::Take};
use swc_ecma_ast::*;
use swc_ecma_utils::private_ident;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};

use crate::id::Id;

#[derive(Default)]
pub struct KeywordRenamer {
    renamed: AHashMap<Id, Ident>,
}

impl KeywordRenamer {
    /// Returns `Some(new_ident)` if it should be renamed.
    fn renamed(&mut self, id: &Ident) -> Option<Ident> {
        if id.sym == "import" {
            return None;
        }

        if !(id.is_reserved() || id.is_reserved_in_strict_mode(true) || id.is_reserved_in_es3()) {
            return None;
        }

        Some(
            self.renamed
                .entry(id.into())
                .or_insert_with(|| private_ident!(id.span, format!("__{}", id.sym)))
                .clone(),
        )
    }
}

impl VisitMut for KeywordRenamer {
    noop_visit_mut_type!(fail);

    fn visit_mut_binding_ident(&mut self, n: &mut BindingIdent) {
        if let Some(new) = self.renamed(&n.id) {
            n.ctxt = new.ctxt;
            n.sym = new.sym;
        }
    }

    fn visit_mut_class_decl(&mut self, c: &mut ClassDecl) {
        c.class.visit_mut_with(self);
        if let Some(renamed) = self.renamed(&c.ident) {
            c.ident = renamed;
        }
    }

    fn visit_mut_class_prop(&mut self, n: &mut ClassProp) {
        if n.key.is_computed() {
            n.key.visit_mut_with(self);
        }

        n.decorators.visit_mut_with(self);
        n.value.visit_mut_with(self);
    }

    fn visit_mut_export_named_specifier(&mut self, n: &mut ExportNamedSpecifier) {
        let orig = match &n.orig {
            ModuleExportName::Ident(ident) => ident,
            ModuleExportName::Str(..) => unimplemented!("module string names unimplemented"),
        };
        if let Some(renamed) = self.renamed(orig) {
            n.orig = ModuleExportName::Ident(renamed);
        }
    }

    fn visit_mut_expr(&mut self, n: &mut Expr) {
        if let Expr::Ident(n) = n {
            if let Some(renamed) = self.renamed(n) {
                *n = renamed;
            }
            return;
        }

        n.visit_mut_children_with(self);
    }

    fn visit_mut_fn_decl(&mut self, f: &mut FnDecl) {
        f.function.visit_mut_with(self);
        if let Some(renamed) = self.renamed(&f.ident) {
            f.ident = renamed;
        }
    }

    fn visit_mut_object_pat_prop(&mut self, n: &mut ObjectPatProp) {
        if let ObjectPatProp::Assign(pat) = n {
            if let Some(renamed) = self.renamed(&pat.key) {
                match &mut pat.value {
                    Some(default) => {
                        *n = ObjectPatProp::KeyValue(KeyValuePatProp {
                            key: PropName::Ident(pat.key.take().into()),
                            value: AssignPat {
                                span: pat.span,
                                left: Box::new(renamed.into()),
                                right: default.take(),
                            }
                            .into(),
                        });
                    }
                    None => {
                        *n = ObjectPatProp::KeyValue(KeyValuePatProp {
                            key: PropName::Ident(pat.key.take().into()),
                            value: renamed.into(),
                        })
                    }
                }
            }
            return;
        }

        n.visit_mut_children_with(self);
    }

    fn visit_mut_pat(&mut self, n: &mut Pat) {
        if let Pat::Ident(n) = n {
            if let Some(renamed) = self.renamed(&n.id) {
                *n = renamed.into();
            }

            return;
        }
        n.visit_mut_children_with(self);
    }

    fn visit_mut_private_prop(&mut self, n: &mut PrivateProp) {
        n.decorators.visit_mut_with(self);
        n.value.visit_mut_with(self);
    }

    fn visit_mut_prop(&mut self, n: &mut Prop) {
        match n {
            Prop::Shorthand(i) => {
                if let Some(renamed) = self.renamed(i) {
                    *n = Prop::KeyValue(KeyValueProp {
                        key: PropName::Ident(i.clone().into()),
                        value: renamed.into(),
                    });
                }
            }
            _ => {
                n.visit_mut_children_with(self);
            }
        }
    }
}