swc_ecma_minifier/util/
unit.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
148
149
150
use std::fmt::Debug;

use swc_common::Mark;
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene};
use swc_ecma_utils::DropSpan;
#[cfg(debug_assertions)]
use swc_ecma_visit::VisitWith;
use swc_ecma_visit::{as_folder, FoldWith, VisitMut, VisitMutWith};

use crate::debug::dump;
#[cfg(debug_assertions)]
use crate::debug::AssertValid;

/// Indicates a unit of minifaction.
pub(crate) trait CompileUnit:
    swc_ecma_codegen::Node
    + Clone
    + VisitMutWith<DropSpan>
    + VisitMutWith<crate::debug::Debugger>
    + Debug
{
    fn is_module() -> bool;

    fn dump(&self) -> String {
        #[cfg(feature = "debug")]
        {
            self.force_dump()
        }
        #[cfg(not(feature = "debug"))]
        {
            String::new()
        }
    }

    fn force_dump(&self) -> String;

    fn apply<V>(&mut self, visitor: &mut V)
    where
        V: VisitMut;

    fn remove_mark(&mut self) -> Mark;
}

impl CompileUnit for Module {
    fn is_module() -> bool {
        true
    }

    fn force_dump(&self) -> String {
        let _noop_sub =
            tracing::subscriber::set_default(tracing::subscriber::NoSubscriber::default());

        dump(
            &self
                .clone()
                .fold_with(&mut fixer(None))
                .fold_with(&mut hygiene())
                .fold_with(&mut as_folder(DropSpan {
                    preserve_ctxt: false,
                })),
            true,
        )
    }

    fn apply<V>(&mut self, visitor: &mut V)
    where
        V: VisitMut,
    {
        self.visit_mut_with(&mut *visitor);

        crate::debug::invoke_module(self);
    }

    fn remove_mark(&mut self) -> Mark {
        Mark::root()
    }
}

impl CompileUnit for Script {
    fn is_module() -> bool {
        false
    }

    fn force_dump(&self) -> String {
        let _noop_sub =
            tracing::subscriber::set_default(tracing::subscriber::NoSubscriber::default());

        dump(
            &self
                .clone()
                .fold_with(&mut fixer(None))
                .fold_with(&mut hygiene())
                .fold_with(&mut as_folder(DropSpan {
                    preserve_ctxt: false,
                })),
            true,
        )
    }

    fn apply<V>(&mut self, visitor: &mut V)
    where
        V: VisitMut,
    {
        self.visit_mut_with(&mut *visitor);

        crate::debug::invoke_script(self);
    }

    fn remove_mark(&mut self) -> Mark {
        Mark::root()
    }
}

impl CompileUnit for FnExpr {
    fn is_module() -> bool {
        false
    }

    fn force_dump(&self) -> String {
        let _noop_sub =
            tracing::subscriber::set_default(tracing::subscriber::NoSubscriber::default());

        dump(
            &self
                .clone()
                .fold_with(&mut fixer(None))
                .fold_with(&mut hygiene())
                .fold_with(&mut as_folder(DropSpan {
                    preserve_ctxt: false,
                })),
            true,
        )
    }

    fn apply<V>(&mut self, visitor: &mut V)
    where
        V: VisitMut,
    {
        self.visit_mut_with(&mut *visitor);
        #[cfg(debug_assertions)]
        {
            self.visit_with(&mut AssertValid);
        }
    }

    fn remove_mark(&mut self) -> Mark {
        self.function.span.remove_mark()
    }
}