swc_ecma_codegen/
pat.rs

1use swc_common::Spanned;
2use swc_ecma_ast::*;
3use swc_ecma_codegen_macros::node_impl;
4
5#[cfg(swc_ast_unknown)]
6use crate::unknown_error;
7
8#[node_impl]
9impl MacroNode for Param {
10    fn emit(&mut self, emitter: &mut Macro) -> Result {
11        emitter.emit_leading_comments_of_span(self.span(), false)?;
12
13        srcmap!(emitter, self, true);
14
15        emitter.emit_list(self.span, Some(&self.decorators), ListFormat::Decorators)?;
16
17        emit!(self.pat);
18
19        srcmap!(emitter, self, false);
20
21        Ok(())
22    }
23}
24
25#[node_impl]
26impl MacroNode for Pat {
27    fn emit(&mut self, emitter: &mut Macro) -> Result {
28        match self {
29            Pat::Array(ref n) => emit!(n),
30            Pat::Assign(ref n) => emit!(n),
31            Pat::Expr(ref n) => emit!(n),
32            Pat::Ident(ref n) => emit!(n),
33            Pat::Object(ref n) => emit!(n),
34            Pat::Rest(ref n) => emit!(n),
35            Pat::Invalid(n) => emit!(n),
36            #[cfg(swc_ast_unknown)]
37            _ => return Err(unknown_error()),
38        }
39
40        if emitter.comments.is_some() {
41            emitter.emit_trailing_comments_of_pos(self.span().hi, true, true)?;
42        }
43
44        Ok(())
45    }
46}
47
48#[node_impl]
49impl MacroNode for RestPat {
50    fn emit(&mut self, emitter: &mut Macro) -> Result {
51        emitter.emit_leading_comments_of_span(self.span(), false)?;
52
53        punct!(emitter, self.dot3_token, "...");
54        emit!(self.arg);
55
56        if let Some(type_ann) = &self.type_ann {
57            punct!(emitter, ":");
58            formatting_space!(emitter);
59            emit!(type_ann);
60        }
61
62        Ok(())
63    }
64}
65
66#[node_impl]
67impl MacroNode for PropOrSpread {
68    fn emit(&mut self, emitter: &mut Macro) -> Result {
69        match self {
70            PropOrSpread::Prop(ref n) => emit!(n),
71            PropOrSpread::Spread(ref n) => emit!(n),
72            #[cfg(swc_ast_unknown)]
73            _ => return Err(unknown_error()),
74        }
75
76        Ok(())
77    }
78}
79
80#[node_impl]
81impl MacroNode for SpreadElement {
82    fn emit(&mut self, emitter: &mut Macro) -> Result {
83        if emitter.comments.is_some() {
84            emitter.emit_leading_comments_of_span(self.span(), false)?;
85        }
86
87        srcmap!(emitter, self, true);
88
89        punct!(emitter, "...");
90        emit!(self.expr);
91
92        srcmap!(emitter, self, false);
93
94        Ok(())
95    }
96}
97
98#[node_impl]
99impl MacroNode for AssignTarget {
100    fn emit(&mut self, emitter: &mut Macro) -> Result {
101        match self {
102            AssignTarget::Simple(ref n) => emit!(n),
103            AssignTarget::Pat(ref n) => emit!(n),
104            #[cfg(swc_ast_unknown)]
105            _ => return Err(unknown_error()),
106        }
107
108        Ok(())
109    }
110}
111
112#[node_impl]
113impl MacroNode for SimpleAssignTarget {
114    fn emit(&mut self, emitter: &mut Macro) -> Result {
115        match self {
116            SimpleAssignTarget::Ident(n) => emit!(n),
117            SimpleAssignTarget::Member(n) => emit!(n),
118            SimpleAssignTarget::Invalid(n) => emit!(n),
119            SimpleAssignTarget::SuperProp(n) => emit!(n),
120            SimpleAssignTarget::Paren(n) => emit!(n),
121            SimpleAssignTarget::OptChain(n) => emit!(n),
122            SimpleAssignTarget::TsAs(n) => emit!(n),
123            SimpleAssignTarget::TsNonNull(n) => emit!(n),
124            SimpleAssignTarget::TsSatisfies(n) => emit!(n),
125            SimpleAssignTarget::TsTypeAssertion(n) => emit!(n),
126            SimpleAssignTarget::TsInstantiation(n) => emit!(n),
127            #[cfg(swc_ast_unknown)]
128            _ => return Err(unknown_error()),
129        }
130
131        Ok(())
132    }
133}
134
135#[node_impl]
136impl MacroNode for AssignTargetPat {
137    fn emit(&mut self, emitter: &mut Macro) -> Result {
138        match self {
139            AssignTargetPat::Array(n) => emit!(n),
140            AssignTargetPat::Object(n) => emit!(n),
141            AssignTargetPat::Invalid(n) => emit!(n),
142            #[cfg(swc_ast_unknown)]
143            _ => return Err(unknown_error()),
144        }
145
146        Ok(())
147    }
148}
149
150#[node_impl]
151impl MacroNode for ArrayPat {
152    fn emit(&mut self, emitter: &mut Macro) -> Result {
153        emitter.emit_leading_comments_of_span(self.span(), false)?;
154
155        srcmap!(emitter, self, true);
156
157        punct!(emitter, "[");
158
159        let mut format = ListFormat::ArrayBindingPatternElements;
160
161        if let Some(None) = self.elems.last() {
162            format |= ListFormat::ForceTrailingComma;
163        }
164
165        emitter.emit_list(self.span(), Some(&self.elems), format)?;
166        punct!(emitter, "]");
167        if self.optional {
168            punct!(emitter, "?");
169        }
170
171        if let Some(type_ann) = &self.type_ann {
172            punct!(emitter, ":");
173            space!(emitter);
174            emit!(type_ann);
175        }
176
177        srcmap!(emitter, self, false);
178
179        Ok(())
180    }
181}
182
183#[node_impl]
184impl MacroNode for AssignPat {
185    fn emit(&mut self, emitter: &mut Macro) -> Result {
186        emitter.emit_leading_comments_of_span(self.span(), false)?;
187
188        srcmap!(emitter, self, true);
189
190        emit!(self.left);
191        formatting_space!(emitter);
192        punct!(emitter, "=");
193        formatting_space!(emitter);
194        emit!(self.right);
195
196        srcmap!(emitter, self, false);
197
198        Ok(())
199    }
200}
201
202#[node_impl]
203impl MacroNode for ObjectPat {
204    fn emit(&mut self, emitter: &mut Macro) -> Result {
205        emitter.emit_leading_comments_of_span(self.span(), false)?;
206
207        srcmap!(emitter, self, true);
208        punct!(emitter, "{");
209
210        emitter.emit_list(
211            self.span(),
212            Some(&self.props),
213            ListFormat::ObjectBindingPatternElements | ListFormat::CanSkipTrailingComma,
214        )?;
215
216        punct!(emitter, "}");
217
218        if self.optional {
219            punct!(emitter, "?");
220        }
221
222        if let Some(type_ann) = &self.type_ann {
223            punct!(emitter, ":");
224            space!(emitter);
225            emit!(type_ann);
226        }
227
228        srcmap!(emitter, self, false);
229
230        Ok(())
231    }
232}
233
234#[node_impl]
235impl MacroNode for ObjectPatProp {
236    fn emit(&mut self, emitter: &mut Macro) -> Result {
237        match self {
238            ObjectPatProp::KeyValue(ref node) => emit!(node),
239            ObjectPatProp::Assign(ref node) => emit!(node),
240            ObjectPatProp::Rest(ref node) => emit!(node),
241            #[cfg(swc_ast_unknown)]
242            _ => return Err(unknown_error()),
243        }
244
245        Ok(())
246    }
247}
248
249#[node_impl]
250impl MacroNode for KeyValuePatProp {
251    fn emit(&mut self, emitter: &mut Macro) -> Result {
252        emitter.emit_leading_comments_of_span(self.span(), false)?;
253
254        srcmap!(emitter, self, true);
255
256        emit!(self.key);
257        punct!(emitter, ":");
258        formatting_space!(emitter);
259        emit!(self.value);
260
261        srcmap!(emitter, self, false);
262
263        Ok(())
264    }
265}
266
267#[node_impl]
268impl MacroNode for AssignPatProp {
269    fn emit(&mut self, emitter: &mut Macro) -> Result {
270        emitter.emit_leading_comments_of_span(self.span(), false)?;
271
272        srcmap!(emitter, self, true);
273
274        emit!(self.key);
275        if let Some(value) = &self.value {
276            formatting_space!(emitter);
277            punct!(emitter, "=");
278            formatting_space!(emitter);
279            emit!(value);
280        }
281
282        srcmap!(emitter, self, false);
283
284        Ok(())
285    }
286}
287
288#[node_impl]
289impl MacroNode for ForHead {
290    fn emit(&mut self, emitter: &mut Macro) -> Result {
291        match self {
292            ForHead::Pat(n) => emit!(n),
293            ForHead::VarDecl(n) => emit!(n),
294            ForHead::UsingDecl(n) => emit!(n),
295            #[cfg(swc_ast_unknown)]
296            _ => return Err(unknown_error()),
297        }
298
299        Ok(())
300    }
301}