swc_common/
pass.rs

1//! This module reexports items from `swc_visit` with some swc-specific traits.
2
3use std::borrow::Cow;
4
5pub use swc_visit::*;
6
7/// A named compiler pass.
8pub trait CompilerPass {
9    ///
10    /// - name should follow hyphen-case.
11    /// - an implementation should return same name
12    fn name(&self) -> Cow<'static, str>;
13}
14
15impl<V> CompilerPass for Repeat<V>
16where
17    V: CompilerPass + Repeated,
18{
19    fn name(&self) -> Cow<'static, str> {
20        Cow::Owned(format!("repeat({})", self.pass.name()))
21    }
22}
23
24macro_rules! impl_compiler_pass_for_tuple {
25    (
26        [$idx:tt, $name:ident], $([$idx_rest:tt, $name_rest:ident]),*
27    ) => {
28        impl<$name, $($name_rest),*> CompilerPass for ($name, $($name_rest),*)
29        where
30            $name: CompilerPass,
31            $($name_rest: CompilerPass),*
32        {
33            #[inline]
34            fn name(&self) -> Cow<'static, str> {
35                use std::fmt::Write;
36
37                let mut name = self.$idx.name().to_string();
38                $(
39                    write!(name, " / {}", self.$idx_rest.name()).unwrap();
40                )*
41                Cow::Owned(name)
42            }
43        }
44    };
45}
46
47impl_compiler_pass_for_tuple!([0, A], [1, B]);
48impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C]);
49impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D]);
50impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E]);
51impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F]);
52impl_compiler_pass_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F], [6, G]);
53impl_compiler_pass_for_tuple!(
54    [0, A],
55    [1, B],
56    [2, C],
57    [3, D],
58    [4, E],
59    [5, F],
60    [6, G],
61    [7, H]
62);
63impl_compiler_pass_for_tuple!(
64    [0, A],
65    [1, B],
66    [2, C],
67    [3, D],
68    [4, E],
69    [5, F],
70    [6, G],
71    [7, H],
72    [8, I]
73);
74impl_compiler_pass_for_tuple!(
75    [0, A],
76    [1, B],
77    [2, C],
78    [3, D],
79    [4, E],
80    [5, F],
81    [6, G],
82    [7, H],
83    [8, I],
84    [9, J]
85);
86impl_compiler_pass_for_tuple!(
87    [0, A],
88    [1, B],
89    [2, C],
90    [3, D],
91    [4, E],
92    [5, F],
93    [6, G],
94    [7, H],
95    [8, I],
96    [9, J],
97    [10, K]
98);
99impl_compiler_pass_for_tuple!(
100    [0, A],
101    [1, B],
102    [2, C],
103    [3, D],
104    [4, E],
105    [5, F],
106    [6, G],
107    [7, H],
108    [8, I],
109    [9, J],
110    [10, K],
111    [11, L]
112);
113impl_compiler_pass_for_tuple!(
114    [0, A],
115    [1, B],
116    [2, C],
117    [3, D],
118    [4, E],
119    [5, F],
120    [6, G],
121    [7, H],
122    [8, I],
123    [9, J],
124    [10, K],
125    [11, L],
126    [12, M]
127);