swc_ecma_minifier/pass/
postcompress.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
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::{Parallel, ParallelExt};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};

use crate::{maybe_par, option::CompressOptions, LIGHT_TASK_PARALLELS};

pub fn postcompress_optimizer(options: &CompressOptions) -> impl '_ + VisitMut {
    PostcompressOptimizer {
        options,
        ctx: Default::default(),
    }
}

struct PostcompressOptimizer<'a> {
    options: &'a CompressOptions,

    ctx: Ctx,
}

#[derive(Default, Clone, Copy)]
struct Ctx {
    is_module: bool,
    is_top_level: bool,
}

impl Parallel for PostcompressOptimizer<'_> {
    fn create(&self) -> Self {
        Self {
            options: self.options,
            ctx: self.ctx,
        }
    }

    fn merge(&mut self, _: Self) {}
}

impl VisitMut for PostcompressOptimizer<'_> {
    noop_visit_mut_type!();

    fn visit_mut_export_decl(&mut self, export: &mut ExportDecl) {
        match &mut export.decl {
            Decl::Var(decl) => {
                // Don't change constness of exported variables.
                decl.visit_mut_children_with(self);
            }
            _ => {
                export.decl.visit_mut_with(self);
            }
        }
    }

    fn visit_mut_module_items(&mut self, nodes: &mut Vec<ModuleItem>) {
        self.ctx.is_module = maybe_par!(
            nodes
                .iter()
                .any(|s| matches!(s, ModuleItem::ModuleDecl(..))),
            *crate::LIGHT_TASK_PARALLELS
        );
        self.ctx.is_top_level = true;

        self.maybe_par(*crate::LIGHT_TASK_PARALLELS, nodes, |v, n| {
            n.visit_mut_with(v);
        });
    }

    fn visit_mut_stmts(&mut self, nodes: &mut Vec<Stmt>) {
        let old = self.ctx;

        self.ctx.is_top_level = false;

        self.maybe_par(*crate::LIGHT_TASK_PARALLELS, nodes, |v, n| {
            n.visit_mut_with(v);
        });

        self.ctx = old;
    }

    fn visit_mut_var_decl(&mut self, v: &mut VarDecl) {
        v.visit_mut_children_with(self);

        if self.options.const_to_let {
            if self.ctx.is_module || !self.ctx.is_top_level {
                // We don't change constness of top-level variables in a script

                if let VarDeclKind::Const = v.kind {
                    v.kind = VarDeclKind::Let;
                }
            }
        }
    }

    fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
        self.maybe_par(*LIGHT_TASK_PARALLELS, n, |v, n| {
            n.visit_mut_with(v);
        });
    }

    fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
        self.maybe_par(*LIGHT_TASK_PARALLELS, n, |v, n| {
            n.visit_mut_with(v);
        });
    }

    fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
        self.maybe_par(*LIGHT_TASK_PARALLELS, n, |v, n| {
            n.visit_mut_with(v);
        });
    }

    fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
        self.maybe_par(*LIGHT_TASK_PARALLELS, n, |v, n| {
            n.visit_mut_with(v);
        });
    }
}