swc_ecma_codegen/
text_writer.rs

1pub use self::{basic_impl::JsWriter, semicolon::omit_trailing_semi};
2use super::*;
3
4mod basic_impl;
5mod semicolon;
6
7/// TODO
8pub type Symbol = Str;
9
10/// Ecmascript writer.
11///
12/// Ported from `EmitWriteJs`.
13pub trait WriteJs {
14    fn increase_indent(&mut self) -> Result;
15    fn decrease_indent(&mut self) -> Result;
16
17    /// This *may* write semicolon.
18    fn write_semi(&mut self, span: Option<Span>) -> Result;
19
20    fn write_space(&mut self) -> Result;
21    fn write_keyword(&mut self, span: Option<Span>, s: &'static str) -> Result;
22    fn write_operator(&mut self, span: Option<Span>, s: &str) -> Result;
23    fn write_param(&mut self, s: &str) -> Result;
24    fn write_property(&mut self, s: &str) -> Result;
25
26    fn write_line(&mut self) -> Result;
27
28    fn write_lit(&mut self, span: Span, s: &str) -> Result;
29    fn write_comment(&mut self, s: &str) -> Result;
30
31    fn write_str_lit(&mut self, span: Span, s: &str) -> Result;
32    fn write_str(&mut self, s: &str) -> Result;
33
34    fn write_symbol(&mut self, span: Span, s: &str) -> Result;
35
36    fn write_punct(&mut self, span: Option<Span>, s: &'static str) -> Result;
37
38    fn care_about_srcmap(&self) -> bool;
39
40    fn add_srcmap(&mut self, pos: BytePos) -> Result;
41
42    fn commit_pending_semi(&mut self) -> Result;
43
44    /// If true, the code generator will skip **modification** of invalid
45    /// unicode characters.
46    ///
47    /// Defaults to `false``
48    fn can_ignore_invalid_unicodes(&mut self) -> bool {
49        false
50    }
51}
52
53impl<W> WriteJs for Box<W>
54where
55    W: ?Sized + WriteJs,
56{
57    #[inline]
58    fn increase_indent(&mut self) -> Result {
59        (**self).increase_indent()
60    }
61
62    #[inline]
63    fn decrease_indent(&mut self) -> Result {
64        (**self).decrease_indent()
65    }
66
67    #[inline]
68    fn write_semi(&mut self, span: Option<Span>) -> Result {
69        (**self).write_semi(span)
70    }
71
72    #[inline]
73    fn write_space(&mut self) -> Result {
74        (**self).write_space()
75    }
76
77    #[inline]
78    fn write_keyword(&mut self, span: Option<Span>, s: &'static str) -> Result {
79        (**self).write_keyword(span, s)
80    }
81
82    #[inline]
83    fn write_operator(&mut self, span: Option<Span>, s: &str) -> Result {
84        (**self).write_operator(span, s)
85    }
86
87    #[inline]
88    fn write_param(&mut self, s: &str) -> Result {
89        (**self).write_param(s)
90    }
91
92    #[inline]
93    fn write_property(&mut self, s: &str) -> Result {
94        (**self).write_property(s)
95    }
96
97    #[inline]
98    fn write_line(&mut self) -> Result {
99        (**self).write_line()
100    }
101
102    #[inline]
103    fn write_lit(&mut self, span: Span, s: &str) -> Result {
104        (**self).write_lit(span, s)
105    }
106
107    #[inline]
108    fn write_comment(&mut self, s: &str) -> Result {
109        (**self).write_comment(s)
110    }
111
112    #[inline]
113    fn write_str_lit(&mut self, span: Span, s: &str) -> Result {
114        (**self).write_str_lit(span, s)
115    }
116
117    #[inline]
118    fn write_str(&mut self, s: &str) -> Result {
119        (**self).write_str(s)
120    }
121
122    #[inline]
123    fn write_symbol(&mut self, span: Span, s: &str) -> Result {
124        (**self).write_symbol(span, s)
125    }
126
127    #[inline]
128    fn write_punct(&mut self, span: Option<Span>, s: &'static str) -> Result {
129        (**self).write_punct(span, s)
130    }
131
132    #[inline]
133    fn care_about_srcmap(&self) -> bool {
134        (**self).care_about_srcmap()
135    }
136
137    #[inline]
138    fn add_srcmap(&mut self, pos: BytePos) -> Result {
139        (**self).add_srcmap(pos)
140    }
141
142    fn commit_pending_semi(&mut self) -> Result {
143        (**self).commit_pending_semi()
144    }
145
146    #[inline(always)]
147    fn can_ignore_invalid_unicodes(&mut self) -> bool {
148        (**self).can_ignore_invalid_unicodes()
149    }
150}
151
152impl<W> WriteJs for &'_ mut W
153where
154    W: ?Sized + WriteJs,
155{
156    #[inline]
157    fn increase_indent(&mut self) -> Result {
158        (**self).increase_indent()
159    }
160
161    #[inline]
162    fn decrease_indent(&mut self) -> Result {
163        (**self).decrease_indent()
164    }
165
166    #[inline]
167    fn write_semi(&mut self, span: Option<Span>) -> Result {
168        (**self).write_semi(span)
169    }
170
171    #[inline]
172    fn write_space(&mut self) -> Result {
173        (**self).write_space()
174    }
175
176    #[inline]
177    fn write_keyword(&mut self, span: Option<Span>, s: &'static str) -> Result {
178        (**self).write_keyword(span, s)
179    }
180
181    #[inline]
182    fn write_operator(&mut self, span: Option<Span>, s: &str) -> Result {
183        (**self).write_operator(span, s)
184    }
185
186    #[inline]
187    fn write_param(&mut self, s: &str) -> Result {
188        (**self).write_param(s)
189    }
190
191    #[inline]
192    fn write_property(&mut self, s: &str) -> Result {
193        (**self).write_property(s)
194    }
195
196    #[inline]
197    fn write_line(&mut self) -> Result {
198        (**self).write_line()
199    }
200
201    #[inline]
202    fn write_lit(&mut self, span: Span, s: &str) -> Result {
203        (**self).write_lit(span, s)
204    }
205
206    #[inline]
207    fn write_comment(&mut self, s: &str) -> Result {
208        (**self).write_comment(s)
209    }
210
211    #[inline]
212    fn write_str_lit(&mut self, span: Span, s: &str) -> Result {
213        (**self).write_str_lit(span, s)
214    }
215
216    #[inline]
217    fn write_str(&mut self, s: &str) -> Result {
218        (**self).write_str(s)
219    }
220
221    #[inline]
222    fn write_symbol(&mut self, span: Span, s: &str) -> Result {
223        (**self).write_symbol(span, s)
224    }
225
226    #[inline]
227    fn write_punct(&mut self, span: Option<Span>, s: &'static str) -> Result {
228        (**self).write_punct(span, s)
229    }
230
231    #[inline]
232    fn care_about_srcmap(&self) -> bool {
233        (**self).care_about_srcmap()
234    }
235
236    #[inline]
237    fn add_srcmap(&mut self, pos: BytePos) -> Result {
238        (**self).add_srcmap(pos)
239    }
240
241    #[inline(always)]
242    fn commit_pending_semi(&mut self) -> Result {
243        (**self).commit_pending_semi()
244    }
245
246    #[inline(always)]
247    fn can_ignore_invalid_unicodes(&mut self) -> bool {
248        (**self).can_ignore_invalid_unicodes()
249    }
250}