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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
use std::{borrow::Cow, sync::Arc};

use indexmap::IndexSet;
use petgraph::{algo::tarjan_scc, Direction::Incoming};
use rustc_hash::FxHashSet;
use swc_atoms::JsWord;
use swc_common::{
    collections::{AHashMap, AHashSet, ARandomState},
    pass::{CompilerPass, Repeated},
    util::take::Take,
    Mark, SyntaxContext, DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{
    helpers::{Helpers, HELPERS},
    perf::{cpu_count, ParVisitMut, Parallel},
};
use swc_ecma_utils::{
    collect_decls, find_pat_ids, ExprCtx, ExprExt, IsEmpty, ModuleItemLike, StmtLike,
};
use swc_ecma_visit::{
    as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith,
};
use swc_fast_graph::digraph::FastDiGraphMap;
use tracing::{debug, span, Level};

use crate::debug_assert_valid;

/// Note: This becomes parallel if `concurrent` feature is enabled.
pub fn dce(
    config: Config,
    unresolved_mark: Mark,
) -> impl Fold + VisitMut + Repeated + CompilerPass {
    as_folder(TreeShaker {
        expr_ctx: ExprCtx {
            unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark),
            is_unresolved_ref_safe: false,
        },
        config,
        changed: false,
        pass: 0,
        in_fn: false,
        in_block_stmt: false,
        var_decl_kind: None,
        data: Default::default(),
        bindings: Default::default(),
    })
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Config {
    /// If this [Mark] is applied to a function expression, it's treated as a
    /// separate module.
    ///
    /// **Note**: This is hack to make operation parallel while allowing invalid
    /// module produced by the `swc_bundler`.
    pub module_mark: Option<Mark>,

    /// If true, top-level items will be removed if they are not used.
    ///
    /// Defaults to `true`.
    pub top_level: bool,

    /// Declarations with a symbol in this set will be preserved.
    pub top_retain: Vec<JsWord>,

    /// If false, imports with side effects will be removed.
    pub preserve_imports_with_side_effects: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            module_mark: Default::default(),
            top_level: true,
            top_retain: Default::default(),
            preserve_imports_with_side_effects: true,
        }
    }
}

struct TreeShaker {
    expr_ctx: ExprCtx,

    config: Config,
    changed: bool,
    pass: u16,

    in_fn: bool,
    in_block_stmt: bool,
    var_decl_kind: Option<VarDeclKind>,

    data: Arc<Data>,

    bindings: Arc<AHashSet<Id>>,
}

impl CompilerPass for TreeShaker {
    fn name() -> Cow<'static, str> {
        Cow::Borrowed("tree-shaker")
    }
}

#[derive(Default)]
struct Data {
    used_names: AHashMap<Id, VarInfo>,

    /// Variable usage graph
    ///
    /// We use `u32` because [FastDiGraphMap] stores types as `(N, 1 bit)` so if
    /// we use u32 it fits into the cache line of cpu.
    graph: FastDiGraphMap<u32, VarInfo>,
    /// Entrypoints.
    entries: FxHashSet<u32>,

    graph_ix: IndexSet<Id, ARandomState>,
}

impl Data {
    fn node(&mut self, id: &Id) -> u32 {
        self.graph_ix.get_index_of(id).unwrap_or_else(|| {
            let ix = self.graph_ix.len();
            self.graph_ix.insert_full(id.clone());
            ix
        }) as _
    }

    /// Add an edge to dependency graph
    fn add_dep_edge(&mut self, from: Id, to: Id, assign: bool) {
        let from = self.node(&from);
        let to = self.node(&to);

        match self.graph.edge_weight_mut(from, to) {
            Some(info) => {
                if assign {
                    info.assign += 1;
                } else {
                    info.usage += 1;
                }
            }
            None => {
                self.graph.add_edge(
                    from,
                    to,
                    VarInfo {
                        usage: u32::from(!assign),
                        assign: u32::from(assign),
                    },
                );
            }
        };
    }

    /// Traverse the graph and subtract usages from `used_names`.
    fn subtract_cycles(&mut self) {
        let cycles = tarjan_scc(&self.graph);

        'c: for cycle in cycles {
            if cycle.len() == 1 {
                continue;
            }

            // We have to exclude cycle from remove list if an outer node refences an item
            // of cycle.
            for &node in &cycle {
                // It's referenced by an outer node.
                if self.entries.contains(&node) {
                    continue 'c;
                }

                if self.graph.neighbors_directed(node, Incoming).any(|node| {
                    // Node in cycle does not matter
                    !cycle.contains(&node)
                }) {
                    continue 'c;
                }
            }

            for &i in &cycle {
                for &j in &cycle {
                    if i == j {
                        continue;
                    }

                    let id = self.graph_ix.get_index(j as _);
                    let id = match id {
                        Some(id) => id,
                        None => continue,
                    };

                    if let Some(w) = self.graph.edge_weight(i, j) {
                        let e = self.used_names.entry(id.clone()).or_default();
                        e.usage -= w.usage;
                        e.assign -= w.assign;
                    }
                }
            }
        }
    }
}

#[derive(Debug, Default)]
struct VarInfo {
    /// This does not include self-references in a function.
    pub usage: u32,
    /// This does not include self-references in a function.
    pub assign: u32,
}

struct Analyzer<'a> {
    #[allow(dead_code)]
    config: &'a Config,
    in_var_decl: bool,
    scope: Scope<'a>,
    data: &'a mut Data,
    cur_class_id: Option<Id>,
    cur_fn_id: Option<Id>,
}

#[derive(Debug, Default)]
struct Scope<'a> {
    parent: Option<&'a Scope<'a>>,
    kind: ScopeKind,

    bindings_affected_by_eval: AHashSet<Id>,
    found_direct_eval: bool,

    found_arguemnts: bool,
    bindings_affected_by_arguements: Vec<Id>,

    /// Used to construct a graph.
    ///
    /// This includes all bindings to current node.
    ast_path: Vec<Id>,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum ScopeKind {
    Fn,
    ArrowFn,
}

impl Default for ScopeKind {
    fn default() -> Self {
        Self::Fn
    }
}

impl Analyzer<'_> {
    fn with_ast_path<F>(&mut self, ids: Vec<Id>, op: F)
    where
        F: for<'aa> FnOnce(&mut Analyzer<'aa>),
    {
        let prev_len = self.scope.ast_path.len();

        self.scope.ast_path.extend(ids);

        op(self);

        self.scope.ast_path.truncate(prev_len);
    }

    fn with_scope<F>(&mut self, kind: ScopeKind, op: F)
    where
        F: for<'aa> FnOnce(&mut Analyzer<'aa>),
    {
        let child_scope = {
            let child = Scope {
                parent: Some(&self.scope),
                ..Default::default()
            };

            let mut v = Analyzer {
                scope: child,
                data: self.data,
                cur_fn_id: self.cur_fn_id.clone(),
                cur_class_id: self.cur_class_id.clone(),
                ..*self
            };

            op(&mut v);

            Scope {
                parent: None,
                ..v.scope
            }
        };

        // If we found eval, mark all declarations in scope and upper as used
        if child_scope.found_direct_eval {
            for id in child_scope.bindings_affected_by_eval {
                self.data.used_names.entry(id).or_default().usage += 1;
            }

            self.scope.found_direct_eval = true;
        }

        if child_scope.found_arguemnts {
            // Parameters

            for id in child_scope.bindings_affected_by_arguements {
                self.data.used_names.entry(id).or_default().usage += 1;
            }

            if !matches!(kind, ScopeKind::Fn) {
                self.scope.found_arguemnts = true;
            }
        }
    }

    /// Mark `id` as used
    fn add(&mut self, id: Id, assign: bool) {
        if id.0 == "arguments" {
            self.scope.found_arguemnts = true;
        }

        if let Some(f) = &self.cur_fn_id {
            if id == *f {
                return;
            }
        }
        if let Some(f) = &self.cur_class_id {
            if id == *f {
                return;
            }
        }

        if self.scope.is_ast_path_empty() {
            // Add references from top level items into graph
            let idx = self.data.node(&id);
            self.data.entries.insert(idx);
        } else {
            let mut scope = Some(&self.scope);

            while let Some(s) = scope {
                for component in &s.ast_path {
                    self.data
                        .add_dep_edge(component.clone(), id.clone(), assign)
                }

                if s.kind == ScopeKind::Fn && !s.ast_path.is_empty() {
                    break;
                }

                scope = s.parent;
            }
        }

        if assign {
            self.data.used_names.entry(id).or_default().assign += 1;
        } else {
            self.data.used_names.entry(id).or_default().usage += 1;
        }
    }
}

impl Visit for Analyzer<'_> {
    noop_visit_type!();

    fn visit_callee(&mut self, n: &Callee) {
        n.visit_children_with(self);

        if let Callee::Expr(e) = n {
            if e.is_ident_ref_to("eval") {
                self.scope.found_direct_eval = true;
            }
        }
    }

    fn visit_assign_pat_prop(&mut self, n: &AssignPatProp) {
        n.visit_children_with(self);

        self.add(n.key.to_id(), true);
    }

    fn visit_class_decl(&mut self, n: &ClassDecl) {
        self.with_ast_path(vec![n.ident.to_id()], |v| {
            let old = v.cur_class_id.take();
            v.cur_class_id = Some(n.ident.to_id());
            n.visit_children_with(v);
            v.cur_class_id = old;

            if !n.class.decorators.is_empty() {
                v.add(n.ident.to_id(), false);
            }
        })
    }

    fn visit_class_expr(&mut self, n: &ClassExpr) {
        n.visit_children_with(self);

        if !n.class.decorators.is_empty() {
            if let Some(i) = &n.ident {
                self.add(i.to_id(), false);
            }
        }
    }

    fn visit_export_named_specifier(&mut self, n: &ExportNamedSpecifier) {
        if let ModuleExportName::Ident(orig) = &n.orig {
            self.add(orig.to_id(), false);
        }
    }

    fn visit_export_decl(&mut self, n: &ExportDecl) {
        let name = match &n.decl {
            Decl::Class(c) => vec![c.ident.to_id()],
            Decl::Fn(f) => vec![f.ident.to_id()],
            Decl::Var(v) => v
                .decls
                .iter()
                .flat_map(|d| find_pat_ids(d).into_iter())
                .collect(),
            _ => Vec::new(),
        };
        for ident in name {
            self.add(ident, false);
        }

        n.visit_children_with(self)
    }

    fn visit_expr(&mut self, e: &Expr) {
        let old_in_var_decl = self.in_var_decl;

        self.in_var_decl = false;
        e.visit_children_with(self);

        if let Expr::Ident(i) = e {
            self.add(i.to_id(), false);
        }

        self.in_var_decl = old_in_var_decl;
    }

    fn visit_assign_expr(&mut self, n: &AssignExpr) {
        match n.op {
            op!("=") => {
                if let Some(i) = n.left.as_ident() {
                    self.add(i.to_id(), true);
                    n.right.visit_with(self);
                } else {
                    n.visit_children_with(self);
                }
            }
            _ => {
                if let Some(i) = n.left.as_ident() {
                    self.add(i.to_id(), false);
                    self.add(i.to_id(), true);
                    n.right.visit_with(self);
                } else {
                    n.visit_children_with(self);
                }
            }
        }
    }

    fn visit_jsx_element_name(&mut self, e: &JSXElementName) {
        e.visit_children_with(self);

        if let JSXElementName::Ident(i) = e {
            self.add(i.to_id(), false);
        }
    }

    fn visit_jsx_object(&mut self, e: &JSXObject) {
        e.visit_children_with(self);

        if let JSXObject::Ident(i) = e {
            self.add(i.to_id(), false);
        }
    }

    fn visit_arrow_expr(&mut self, n: &ArrowExpr) {
        self.with_scope(ScopeKind::ArrowFn, |v| {
            n.visit_children_with(v);

            if v.scope.found_direct_eval {
                v.scope.bindings_affected_by_eval = collect_decls(n);
            }
        })
    }

    fn visit_function(&mut self, n: &Function) {
        self.with_scope(ScopeKind::Fn, |v| {
            n.visit_children_with(v);

            if v.scope.found_direct_eval {
                v.scope.bindings_affected_by_eval = collect_decls(n);
            }

            if v.scope.found_arguemnts {
                v.scope.bindings_affected_by_arguements = find_pat_ids(&n.params);
            }
        })
    }

    fn visit_fn_decl(&mut self, n: &FnDecl) {
        self.with_ast_path(vec![n.ident.to_id()], |v| {
            let old = v.cur_fn_id.take();
            v.cur_fn_id = Some(n.ident.to_id());
            n.visit_children_with(v);
            v.cur_fn_id = old;

            if !n.function.decorators.is_empty() {
                v.add(n.ident.to_id(), false);
            }
        })
    }

    fn visit_fn_expr(&mut self, n: &FnExpr) {
        n.visit_children_with(self);

        if !n.function.decorators.is_empty() {
            if let Some(i) = &n.ident {
                self.add(i.to_id(), false);
            }
        }
    }

    fn visit_pat(&mut self, p: &Pat) {
        p.visit_children_with(self);

        if !self.in_var_decl {
            if let Pat::Ident(i) = p {
                self.add(i.id.to_id(), true);
            }
        }
    }

    fn visit_prop(&mut self, p: &Prop) {
        p.visit_children_with(self);

        if let Prop::Shorthand(i) = p {
            self.add(i.to_id(), false);
        }
    }

    fn visit_var_declarator(&mut self, n: &VarDeclarator) {
        let old = self.in_var_decl;

        self.in_var_decl = true;
        n.name.visit_with(self);

        self.in_var_decl = false;
        n.init.visit_with(self);

        self.in_var_decl = old;
    }
}

impl Repeated for TreeShaker {
    fn changed(&self) -> bool {
        self.changed
    }

    fn reset(&mut self) {
        self.pass += 1;
        self.changed = false;
        self.data = Default::default();
    }
}

impl Parallel for TreeShaker {
    fn create(&self) -> Self {
        Self {
            expr_ctx: self.expr_ctx.clone(),
            data: self.data.clone(),
            config: self.config.clone(),
            bindings: self.bindings.clone(),
            ..*self
        }
    }

    fn merge(&mut self, other: Self) {
        self.changed |= other.changed;
    }
}

impl TreeShaker {
    fn visit_mut_stmt_likes<T>(&mut self, stmts: &mut Vec<T>)
    where
        T: StmtLike + ModuleItemLike + VisitMutWith<Self> + Send + Sync,
        Vec<T>: VisitMutWith<Self>,
    {
        if let Some(Stmt::Expr(ExprStmt { expr, .. })) = stmts.first().and_then(|s| s.as_stmt()) {
            if let Expr::Lit(Lit::Str(v)) = &**expr {
                if &*v.value == "use asm" {
                    return;
                }
            }
        }

        self.visit_mut_par(cpu_count() * 8, stmts);

        stmts.retain(|s| match s.as_stmt() {
            Some(Stmt::Empty(..)) => false,
            Some(Stmt::Block(s)) if s.is_empty() => {
                debug!("Dropping an empty block statement");
                false
            }
            _ => true,
        });
    }

    fn can_drop_binding(&self, name: Id, is_var: bool) -> bool {
        if !self.config.top_level {
            if is_var {
                if !self.in_fn {
                    return false;
                }
            } else if !self.in_block_stmt {
                return false;
            }
        }

        if self.config.top_retain.contains(&name.0) {
            return false;
        }

        match self.data.used_names.get(&name) {
            Some(v) => v.usage == 0 && v.assign == 0,
            None => true,
        }
    }

    fn can_drop_assignment_to(&self, name: Id, is_var: bool) -> bool {
        if !self.config.top_level {
            if is_var {
                if !self.in_fn {
                    return false;
                }
            } else if !self.in_block_stmt {
                return false;
            }

            // Abort if the variable is declared on top level scope.
            let ix = self.data.graph_ix.get_index_of(&name);
            if let Some(ix) = ix {
                if self.data.entries.contains(&(ix as u32)) {
                    return false;
                }
            }
        }

        if self.config.top_retain.contains(&name.0) {
            return false;
        }

        self.bindings.contains(&name)
            && self
                .data
                .used_names
                .get(&name)
                .map(|v| v.usage == 0)
                .unwrap_or_default()
    }
}

impl VisitMut for TreeShaker {
    noop_visit_mut_type!();

    fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) {
        n.visit_mut_children_with(self);

        if let Some(id) = n.left.as_ident() {
            // TODO: `var`
            if self.can_drop_assignment_to(id.to_id(), false)
                && !n.right.may_have_side_effects(&self.expr_ctx)
            {
                self.changed = true;
                debug!("Dropping an assignment to `{}` because it's not used", id);

                n.left.take();
            }
        }
    }

    fn visit_mut_block_stmt(&mut self, n: &mut BlockStmt) {
        let old_in_block_stmt = self.in_block_stmt;
        self.in_block_stmt = true;
        n.visit_mut_children_with(self);
        self.in_block_stmt = old_in_block_stmt;
    }

    fn visit_mut_function(&mut self, n: &mut Function) {
        let old_in_fn = self.in_fn;
        self.in_fn = true;
        n.visit_mut_children_with(self);
        self.in_fn = old_in_fn;
    }

    fn visit_mut_decl(&mut self, n: &mut Decl) {
        n.visit_mut_children_with(self);

        match n {
            Decl::Fn(f) => {
                if self.can_drop_binding(f.ident.to_id(), true) {
                    debug!("Dropping function `{}` as it's not used", f.ident);
                    self.changed = true;

                    n.take();
                }
            }
            Decl::Class(c) => {
                if self.can_drop_binding(c.ident.to_id(), false)
                    && c.class.body.iter().all(|m| match m {
                        ClassMember::Method(m) => !matches!(m.key, PropName::Computed(..)),
                        ClassMember::ClassProp(m) => {
                            !matches!(m.key, PropName::Computed(..))
                                && !m
                                    .value
                                    .as_deref()
                                    .map_or(false, |e| e.may_have_side_effects(&self.expr_ctx))
                        }
                        ClassMember::AutoAccessor(m) => {
                            !matches!(m.key, Key::Public(PropName::Computed(..)))
                                && !m
                                    .value
                                    .as_deref()
                                    .map_or(false, |e| e.may_have_side_effects(&self.expr_ctx))
                        }

                        ClassMember::PrivateProp(m) => !m
                            .value
                            .as_deref()
                            .map_or(false, |e| e.may_have_side_effects(&self.expr_ctx)),

                        ClassMember::StaticBlock(_) => false,

                        ClassMember::TsIndexSignature(_)
                        | ClassMember::Empty(_)
                        | ClassMember::Constructor(_)
                        | ClassMember::PrivateMethod(_) => true,
                    })
                {
                    debug!("Dropping class `{}` as it's not used", c.ident);
                    self.changed = true;

                    n.take();
                }
            }
            _ => {}
        }
    }

    fn visit_mut_export_decl(&mut self, n: &mut ExportDecl) {
        match &mut n.decl {
            Decl::Var(v) => {
                for decl in v.decls.iter_mut() {
                    decl.init.visit_mut_with(self);
                }
            }
            _ => {
                // Bypass visit_mut_decl
                n.decl.visit_mut_children_with(self);
            }
        }
    }

    /// Noop.
    fn visit_mut_export_default_decl(&mut self, _: &mut ExportDefaultDecl) {}

    fn visit_mut_expr(&mut self, n: &mut Expr) {
        n.visit_mut_children_with(self);

        if let Expr::Call(CallExpr {
            callee: Callee::Expr(callee),
            args,
            ..
        }) = n
        {
            //
            if args.is_empty() {
                match &mut **callee {
                    Expr::Fn(FnExpr {
                        ident: None,
                        function: f,
                    }) if matches!(
                        &**f,
                        Function {
                            is_async: false,
                            is_generator: false,
                            body: Some(..),
                            ..
                        }
                    ) =>
                    {
                        if f.params.is_empty() && f.body.as_ref().unwrap().stmts.len() == 1 {
                            if let Stmt::Return(ReturnStmt { arg: Some(arg), .. }) =
                                &mut f.body.as_mut().unwrap().stmts[0]
                            {
                                if let Expr::Object(ObjectLit { props, .. }) = &**arg {
                                    if props.iter().all(|p| match p {
                                        PropOrSpread::Spread(_) => false,
                                        PropOrSpread::Prop(p) => match &**p {
                                            Prop::Shorthand(_) => true,
                                            Prop::KeyValue(p) => p.value.is_ident(),
                                            _ => false,
                                        },
                                    }) {
                                        self.changed = true;
                                        debug!("Dropping a wrapped esm");
                                        *n = *arg.take();
                                        return;
                                    }
                                }
                            }
                        }
                    }
                    _ => (),
                }
            }
        }

        if let Expr::Assign(a) = n {
            if match &a.left {
                AssignTarget::Simple(l) => l.is_invalid(),
                AssignTarget::Pat(l) => l.is_invalid(),
            } {
                *n = *a.right.take();
            }
        }

        if !n.is_invalid() {
            debug_assert_valid(n);
        }
    }

    fn visit_mut_import_specifiers(&mut self, ss: &mut Vec<ImportSpecifier>) {
        ss.retain(|s| {
            let local = match s {
                ImportSpecifier::Named(l) => &l.local,
                ImportSpecifier::Default(l) => &l.local,
                ImportSpecifier::Namespace(l) => &l.local,
            };

            if self.can_drop_binding(local.to_id(), false) {
                debug!(
                    "Dropping import specifier `{}` because it's not used",
                    local
                );
                self.changed = true;
                return false;
            }

            true
        });
    }

    fn visit_mut_module(&mut self, m: &mut Module) {
        debug_assert_valid(m);

        let _tracing = span!(Level::ERROR, "tree-shaker", pass = self.pass).entered();

        if self.bindings.is_empty() {
            self.bindings = Arc::new(collect_decls(&*m))
        }

        let mut data = Default::default();

        {
            let mut analyzer = Analyzer {
                config: &self.config,
                in_var_decl: false,
                scope: Default::default(),
                data: &mut data,
                cur_class_id: Default::default(),
                cur_fn_id: Default::default(),
            };
            m.visit_with(&mut analyzer);
        }
        data.subtract_cycles();
        self.data = Arc::new(data);

        HELPERS.set(&Helpers::new(true), || {
            m.visit_mut_children_with(self);
        })
    }

    fn visit_mut_script(&mut self, m: &mut Script) {
        let _tracing = span!(Level::ERROR, "tree-shaker", pass = self.pass).entered();

        if self.bindings.is_empty() {
            self.bindings = Arc::new(collect_decls(&*m))
        }

        let mut data = Default::default();

        {
            let mut analyzer = Analyzer {
                config: &self.config,
                in_var_decl: false,
                scope: Default::default(),
                data: &mut data,
                cur_class_id: Default::default(),
                cur_fn_id: Default::default(),
            };
            m.visit_with(&mut analyzer);
        }
        data.subtract_cycles();
        self.data = Arc::new(data);

        HELPERS.set(&Helpers::new(true), || {
            m.visit_mut_children_with(self);
        })
    }

    fn visit_mut_module_item(&mut self, n: &mut ModuleItem) {
        match n {
            ModuleItem::ModuleDecl(ModuleDecl::Import(i)) => {
                let is_for_side_effect = i.specifiers.is_empty();

                i.visit_mut_with(self);

                if !self.config.preserve_imports_with_side_effects
                    && !is_for_side_effect
                    && i.specifiers.is_empty()
                {
                    debug!("Dropping an import because it's not used");
                    self.changed = true;
                    *n = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
                }
            }
            _ => {
                n.visit_mut_children_with(self);
            }
        }
        debug_assert_valid(n);
    }

    fn visit_mut_module_items(&mut self, s: &mut Vec<ModuleItem>) {
        self.visit_mut_stmt_likes(s);
    }

    fn visit_mut_stmt(&mut self, s: &mut Stmt) {
        s.visit_mut_children_with(self);

        if let Stmt::Decl(Decl::Var(v)) = s {
            if v.decls.is_empty() {
                s.take();
                return;
            }
        }

        debug_assert_valid(s);

        if let Stmt::Decl(Decl::Var(v)) = s {
            let span = v.span;
            let cnt = v.decls.len();

            // If all name is droppable, do so.
            if cnt != 0
                && v.decls.iter().all(|vd| match &vd.name {
                    Pat::Ident(i) => self.can_drop_binding(i.to_id(), v.kind == VarDeclKind::Var),
                    _ => false,
                })
            {
                let exprs = v
                    .decls
                    .take()
                    .into_iter()
                    .filter_map(|v| v.init)
                    .collect::<Vec<_>>();

                debug!(
                    count = cnt,
                    "Dropping names of variables as they are not used",
                );
                self.changed = true;

                if exprs.is_empty() {
                    *s = Stmt::Empty(EmptyStmt { span: DUMMY_SP });
                    return;
                } else {
                    *s = Stmt::Expr(ExprStmt {
                        span,
                        expr: Expr::from_exprs(exprs),
                    });
                }
            }
        }

        if let Stmt::Decl(Decl::Var(v)) = s {
            if v.decls.is_empty() {
                *s = Stmt::Empty(EmptyStmt { span: DUMMY_SP });
            }
        }

        debug_assert_valid(s);
    }

    fn visit_mut_stmts(&mut self, s: &mut Vec<Stmt>) {
        self.visit_mut_stmt_likes(s);
    }

    fn visit_mut_var_decl_or_expr(&mut self, n: &mut VarDeclOrExpr) {
        match n {
            VarDeclOrExpr::VarDecl(..) => {}
            VarDeclOrExpr::Expr(v) => {
                v.visit_mut_with(self);
            }
        }
    }

    fn visit_mut_for_head(&mut self, n: &mut ForHead) {
        match n {
            ForHead::VarDecl(..) | ForHead::UsingDecl(..) => {}
            ForHead::Pat(v) => {
                v.visit_mut_with(self);
            }
        }
    }

    fn visit_mut_var_decl(&mut self, n: &mut VarDecl) {
        let old_var_decl_kind = self.var_decl_kind;
        self.var_decl_kind = Some(n.kind);
        n.visit_mut_children_with(self);
        self.var_decl_kind = old_var_decl_kind;
    }

    fn visit_mut_var_declarator(&mut self, v: &mut VarDeclarator) {
        v.visit_mut_children_with(self);

        if let Pat::Ident(i) = &v.name {
            let can_drop = if let Some(init) = &v.init {
                !init.may_have_side_effects(&self.expr_ctx)
            } else {
                true
            };

            if can_drop
                && self.can_drop_binding(i.id.to_id(), self.var_decl_kind == Some(VarDeclKind::Var))
            {
                self.changed = true;
                debug!("Dropping {} because it's not used", i.id);
                v.name.take();
            }
        }
    }

    fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
        self.visit_mut_par(cpu_count() * 8, n);

        n.retain(|v| {
            if v.name.is_invalid() {
                return false;
            }

            true
        });
    }

    fn visit_mut_with_stmt(&mut self, n: &mut WithStmt) {
        n.obj.visit_mut_with(self);
    }

    fn visit_mut_unary_expr(&mut self, n: &mut UnaryExpr) {
        if matches!(n.op, op!("delete")) {
            return;
        }
        n.visit_mut_children_with(self);
    }

    fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
        self.visit_mut_par(cpu_count() * 8, n);
    }

    fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
        self.visit_mut_par(cpu_count() * 8, n);
    }

    fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
        self.visit_mut_par(cpu_count() * 8, n);
    }

    fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
        self.visit_mut_par(cpu_count() * 8, n);
    }
}

impl Scope<'_> {
    /// Returns true if it's not in a function or class.
    fn is_ast_path_empty(&self) -> bool {
        if !self.ast_path.is_empty() {
            return false;
        }
        match &self.parent {
            Some(p) => p.is_ast_path_empty(),
            None => true,
        }
    }
}