swc_ecma_codegen/
macros.rs

1#![allow(unused)]
2
3macro_rules! opt_leading_space {
4    ($emitter:expr, $e:expr) => {
5        if let Some(ref e) = $e {
6            formatting_space!($emitter);
7            emit!($emitter, e);
8        }
9    };
10}
11
12macro_rules! opt {
13    ($emitter:expr, $e:expr) => {{
14        if let Some(ref expr) = $e {
15            emit!($emitter, expr);
16        }
17    }};
18    ($emitter:expr, $e:expr,) => {{
19        opt!($emitter, $e)
20    }};
21}
22
23macro_rules! emit {
24    ($emitter:expr, $e:expr) => {{
25        crate::Node::emit_with(&$e, $emitter)?;
26    }};
27}
28
29macro_rules! keyword {
30    ($emitter:expr, $span:expr, $s:expr) => {
31        $emitter.wr.write_keyword(Some($span), $s)?
32    };
33    ($emitter:expr, $s:expr) => {
34        $emitter.wr.write_keyword(None, $s)?
35    };
36}
37
38macro_rules! punct {
39    ($emitter:expr, $sp:expr, ";") => {
40        $emitter.wr.write_semi(Some($sp))?;
41    };
42    ($emitter:expr, $sp:expr, $s:expr) => {
43        $emitter.wr.write_punct(Some($sp), $s)?;
44    };
45
46    ($emitter:expr, ";") => {
47        $emitter.wr.write_semi(None)?
48    };
49    ($emitter:expr, $s:expr) => {
50        $emitter.wr.write_punct(None, $s)?
51    };
52}
53
54macro_rules! operator {
55    ($emitter:expr, $sp:expr, $s:expr) => {
56        $emitter.wr.write_operator(Some($sp), $s)?;
57    };
58
59    ($emitter:expr, $s:expr) => {
60        $emitter.wr.write_operator(None, $s)?;
61    };
62}
63
64macro_rules! space {
65    ($emitter:expr) => {
66        $emitter.wr.write_space()?
67    };
68    ($emitter:expr,) => {
69        space!($emitter)
70    };
71}
72
73macro_rules! formatting_space {
74    ($emitter:expr) => {
75        if !$emitter.cfg.minify {
76            $emitter.wr.write_space()?;
77        }
78    };
79    ($emitter:expr,) => {
80        formatting_space!($emitter)
81    };
82}
83
84/// This macro *may* emit a semicolon, if it's required in this context.
85macro_rules! formatting_semi {
86    ($emitter:expr) => {
87        punct!($emitter, ";")
88    };
89    ($emitter:expr, ) => {
90        punct!($emitter, ";")
91    };
92}
93
94/// This macro *always* emits a semicolon, as it's required by the structure we
95/// emit.
96macro_rules! semi {
97    ($emitter:expr, $sp:expr) => {
98        $emitter.wr.write_semi(Some($sp))?;
99    };
100    ($emitter:expr) => {
101        $emitter.wr.write_semi(None)?;
102    };
103}
104
105///
106/// - `srcmap!(true)` for start (span.lo)
107/// - `srcmap!(false)` for end (span.hi)
108macro_rules! srcmap {
109    ($emitter:expr, $n:expr, true) => {{
110        let lo = $n.span_lo();
111        if !lo.is_dummy() {
112            $emitter.wr.add_srcmap(lo)?;
113        }
114    }};
115    ($emitter:expr, $n:expr, false) => {
116        srcmap!($emitter, $n, false, false)
117    };
118    ($emitter:expr, $n:expr, false, $subtract:expr) => {
119        let hi = $n.span_hi();
120        if !hi.is_dummy() {
121            if $subtract {
122                // hi is exclusive
123                $emitter.wr.add_srcmap(hi - swc_common::BytePos(1))?;
124            } else {
125                // TODO(kdy1): Remove this branch.
126                $emitter.wr.add_srcmap(hi)?;
127            }
128        }
129    };
130}