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
use super::{Result, WriteJs};
use swc_common::Span;
pub fn omit_trailing_semi<W: WriteJs>(w: W) -> impl WriteJs {
OmitTrailingSemi {
inner: w,
pending_semi: false,
}
}
#[derive(Debug, Clone)]
struct OmitTrailingSemi<W: WriteJs> {
inner: W,
pending_semi: bool,
}
macro_rules! with_semi {
(
$fn_name:ident
(
$(
$arg_name:ident
:
$arg_ty:ty
),*
)
) => {
fn $fn_name(&mut self, $($arg_name: $arg_ty),* ) -> Result {
self.commit_pending_semi()?;
self.inner.$fn_name( $($arg_name),* )
}
};
}
impl<W: WriteJs> WriteJs for OmitTrailingSemi<W> {
with_semi!(increase_indent());
with_semi!(decrease_indent());
fn write_semi(&mut self, _: Option<Span>) -> Result {
self.pending_semi = true;
Ok(())
}
with_semi!(write_space());
with_semi!(write_comment(span: Span, s: &str));
with_semi!(write_keyword(span: Option<Span>, s: &'static str));
with_semi!(write_operator(span: Option<Span>, s: &str));
with_semi!(write_param(s: &str));
with_semi!(write_property(s: &str));
with_semi!(write_line());
with_semi!(write_lit(span: Span, s: &str));
with_semi!(write_str_lit(span: Span, s: &str));
with_semi!(write_str(s: &str));
with_semi!(write_symbol(span: Span, s: &str));
fn write_punct(&mut self, span: Option<Span>, s: &'static str) -> Result {
match s {
"\"" | "'" => {
self.commit_pending_semi()?;
}
"[" | "!" | "/" | "{" | "(" | "~" => {
self.commit_pending_semi()?;
}
_ => {
self.pending_semi = false;
}
}
Ok(self.inner.write_punct(span, s)?)
}
fn target(&self) -> swc_ecma_ast::EsVersion {
self.inner.target()
}
#[inline]
fn care_about_srcmap(&self) -> bool {
self.inner.care_about_srcmap()
}
}
impl<W: WriteJs> OmitTrailingSemi<W> {
fn commit_pending_semi(&mut self) -> Result {
if self.pending_semi {
self.inner.write_punct(None, ";")?;
self.pending_semi = false;
}
Ok(())
}
}