@@ -92,7 +92,10 @@ impl<'a> Transformer<'a> {
9292 jsx:: update_options_with_comments ( & program. comments , & mut self . options , & self . ctx ) ;
9393
9494 let mut transformer = TransformerImpl {
95- x0_typescript : TypeScript :: new ( & self . options . typescript , & self . ctx ) ,
95+ x0_typescript : program
96+ . source_type
97+ . is_typescript ( )
98+ . then ( || TypeScript :: new ( & self . options . typescript , & self . ctx ) ) ,
9699 x1_jsx : Jsx :: new ( self . options . jsx , ast_builder, & self . ctx ) ,
97100 x2_es2022 : ES2022 :: new ( self . options . es2022 ) ,
98101 x2_es2021 : ES2021 :: new ( self . options . es2021 , & self . ctx ) ,
@@ -113,7 +116,7 @@ impl<'a> Transformer<'a> {
113116
114117struct TransformerImpl < ' a , ' ctx > {
115118 // NOTE: all callbacks must run in order.
116- x0_typescript : TypeScript < ' a , ' ctx > ,
119+ x0_typescript : Option < TypeScript < ' a , ' ctx > > ,
117120 x1_jsx : Jsx < ' a , ' ctx > ,
118121 x2_es2022 : ES2022 ,
119122 x2_es2021 : ES2021 < ' a , ' ctx > ,
@@ -129,13 +132,17 @@ struct TransformerImpl<'a, 'ctx> {
129132
130133impl < ' a , ' ctx > Traverse < ' a > for TransformerImpl < ' a , ' ctx > {
131134 fn enter_program ( & mut self , program : & mut Program < ' a > , ctx : & mut TraverseCtx < ' a > ) {
132- self . x0_typescript . enter_program ( program, ctx) ;
135+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
136+ typescript. enter_program ( program, ctx) ;
137+ }
133138 self . x1_jsx . enter_program ( program, ctx) ;
134139 }
135140
136141 fn exit_program ( & mut self , program : & mut Program < ' a > , ctx : & mut TraverseCtx < ' a > ) {
137142 self . x1_jsx . exit_program ( program, ctx) ;
138- self . x0_typescript . exit_program ( program, ctx) ;
143+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
144+ typescript. exit_program ( program, ctx) ;
145+ }
139146 self . x3_es2015 . exit_program ( program, ctx) ;
140147 self . common . exit_program ( program, ctx) ;
141148 }
@@ -147,32 +154,44 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
147154 arrow : & mut ArrowFunctionExpression < ' a > ,
148155 ctx : & mut TraverseCtx < ' a > ,
149156 ) {
150- self . x0_typescript . enter_arrow_function_expression ( arrow, ctx) ;
157+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
158+ typescript. enter_arrow_function_expression ( arrow, ctx) ;
159+ }
151160 }
152161
153162 fn enter_variable_declarator (
154163 & mut self ,
155164 decl : & mut VariableDeclarator < ' a > ,
156165 ctx : & mut TraverseCtx < ' a > ,
157166 ) {
158- self . x0_typescript . enter_variable_declarator ( decl, ctx) ;
167+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
168+ typescript. enter_variable_declarator ( decl, ctx) ;
169+ }
159170 }
160171
161172 fn enter_binding_pattern ( & mut self , pat : & mut BindingPattern < ' a > , ctx : & mut TraverseCtx < ' a > ) {
162- self . x0_typescript . enter_binding_pattern ( pat, ctx) ;
173+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
174+ typescript. enter_binding_pattern ( pat, ctx) ;
175+ }
163176 }
164177
165178 fn enter_call_expression ( & mut self , expr : & mut CallExpression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
166- self . x0_typescript . enter_call_expression ( expr, ctx) ;
179+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
180+ typescript. enter_call_expression ( expr, ctx) ;
181+ }
167182 self . x1_jsx . enter_call_expression ( expr, ctx) ;
168183 }
169184
170185 fn enter_class ( & mut self , class : & mut Class < ' a > , ctx : & mut TraverseCtx < ' a > ) {
171- self . x0_typescript . enter_class ( class, ctx) ;
186+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
187+ typescript. enter_class ( class, ctx) ;
188+ }
172189 }
173190
174191 fn enter_class_body ( & mut self , body : & mut ClassBody < ' a > , ctx : & mut TraverseCtx < ' a > ) {
175- self . x0_typescript . enter_class_body ( body, ctx) ;
192+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
193+ typescript. enter_class_body ( body, ctx) ;
194+ }
176195 self . x2_es2022 . enter_class_body ( body, ctx) ;
177196 }
178197
@@ -189,12 +208,16 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
189208 decl : & mut TSModuleDeclaration < ' a > ,
190209 ctx : & mut TraverseCtx < ' a > ,
191210 ) {
192- self . x0_typescript . enter_ts_module_declaration ( decl, ctx) ;
211+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
212+ typescript. enter_ts_module_declaration ( decl, ctx) ;
213+ }
193214 }
194215
195216 #[ inline]
196217 fn enter_expression ( & mut self , expr : & mut Expression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
197- self . x0_typescript . enter_expression ( expr, ctx) ;
218+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
219+ typescript. enter_expression ( expr, ctx) ;
220+ }
198221 self . x2_es2021 . enter_expression ( expr, ctx) ;
199222 self . x2_es2020 . enter_expression ( expr, ctx) ;
200223 self . x2_es2018 . enter_expression ( expr, ctx) ;
@@ -214,37 +237,47 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
214237 node : & mut SimpleAssignmentTarget < ' a > ,
215238 ctx : & mut TraverseCtx < ' a > ,
216239 ) {
217- self . x0_typescript . enter_simple_assignment_target ( node, ctx) ;
240+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
241+ typescript. enter_simple_assignment_target ( node, ctx) ;
242+ }
218243 }
219244
220245 fn enter_assignment_target (
221246 & mut self ,
222247 node : & mut AssignmentTarget < ' a > ,
223248 ctx : & mut TraverseCtx < ' a > ,
224249 ) {
225- self . x0_typescript . enter_assignment_target ( node, ctx) ;
250+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
251+ typescript. enter_assignment_target ( node, ctx) ;
252+ }
226253 }
227254
228255 fn enter_formal_parameter (
229256 & mut self ,
230257 param : & mut FormalParameter < ' a > ,
231258 ctx : & mut TraverseCtx < ' a > ,
232259 ) {
233- self . x0_typescript . enter_formal_parameter ( param, ctx) ;
260+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
261+ typescript. enter_formal_parameter ( param, ctx) ;
262+ }
234263 }
235264
236265 fn enter_function ( & mut self , func : & mut Function < ' a > , ctx : & mut TraverseCtx < ' a > ) {
237266 self . x3_es2015 . enter_function ( func, ctx) ;
238267 }
239268
240269 fn exit_function ( & mut self , func : & mut Function < ' a > , ctx : & mut TraverseCtx < ' a > ) {
241- self . x0_typescript . exit_function ( func, ctx) ;
270+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
271+ typescript. exit_function ( func, ctx) ;
272+ }
242273 self . x1_jsx . exit_function ( func, ctx) ;
243274 self . x3_es2015 . exit_function ( func, ctx) ;
244275 }
245276
246277 fn enter_jsx_element ( & mut self , node : & mut JSXElement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
247- self . x0_typescript . enter_jsx_element ( node, ctx) ;
278+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
279+ typescript. enter_jsx_element ( node, ctx) ;
280+ }
248281 }
249282
250283 fn enter_jsx_element_name ( & mut self , node : & mut JSXElementName < ' a > , ctx : & mut TraverseCtx < ' a > ) {
@@ -260,15 +293,19 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
260293 }
261294
262295 fn enter_jsx_fragment ( & mut self , node : & mut JSXFragment < ' a > , ctx : & mut TraverseCtx < ' a > ) {
263- self . x0_typescript . enter_jsx_fragment ( node, ctx) ;
296+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
297+ typescript. enter_jsx_fragment ( node, ctx) ;
298+ }
264299 }
265300
266301 fn enter_jsx_opening_element (
267302 & mut self ,
268303 elem : & mut JSXOpeningElement < ' a > ,
269304 ctx : & mut TraverseCtx < ' a > ,
270305 ) {
271- self . x0_typescript . enter_jsx_opening_element ( elem, ctx) ;
306+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
307+ typescript. enter_jsx_opening_element ( elem, ctx) ;
308+ }
272309 self . x1_jsx . enter_jsx_opening_element ( elem, ctx) ;
273310 }
274311
@@ -277,41 +314,53 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
277314 def : & mut MethodDefinition < ' a > ,
278315 ctx : & mut TraverseCtx < ' a > ,
279316 ) {
280- self . x0_typescript . enter_method_definition ( def, ctx) ;
317+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
318+ typescript. enter_method_definition ( def, ctx) ;
319+ }
281320 }
282321
283322 fn exit_method_definition (
284323 & mut self ,
285324 def : & mut MethodDefinition < ' a > ,
286325 ctx : & mut TraverseCtx < ' a > ,
287326 ) {
288- self . x0_typescript . exit_method_definition ( def, ctx) ;
327+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
328+ typescript. exit_method_definition ( def, ctx) ;
329+ }
289330 self . x2_es2017 . exit_method_definition ( def, ctx) ;
290331 }
291332
292333 fn enter_new_expression ( & mut self , expr : & mut NewExpression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
293- self . x0_typescript . enter_new_expression ( expr, ctx) ;
334+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
335+ typescript. enter_new_expression ( expr, ctx) ;
336+ }
294337 }
295338
296339 fn enter_property_definition (
297340 & mut self ,
298341 def : & mut PropertyDefinition < ' a > ,
299342 ctx : & mut TraverseCtx < ' a > ,
300343 ) {
301- self . x0_typescript . enter_property_definition ( def, ctx) ;
344+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
345+ typescript. enter_property_definition ( def, ctx) ;
346+ }
302347 }
303348
304349 fn enter_accessor_property (
305350 & mut self ,
306351 node : & mut AccessorProperty < ' a > ,
307352 ctx : & mut TraverseCtx < ' a > ,
308353 ) {
309- self . x0_typescript . enter_accessor_property ( node, ctx) ;
354+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
355+ typescript. enter_accessor_property ( node, ctx) ;
356+ }
310357 }
311358
312359 fn enter_statements ( & mut self , stmts : & mut Vec < ' a , Statement < ' a > > , ctx : & mut TraverseCtx < ' a > ) {
313360 self . common . enter_statements ( stmts, ctx) ;
314- self . x0_typescript . enter_statements ( stmts, ctx) ;
361+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
362+ typescript. enter_statements ( stmts, ctx) ;
363+ }
315364 self . x1_jsx . enter_statements ( stmts, ctx) ;
316365 }
317366
@@ -340,13 +389,17 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
340389 }
341390
342391 fn exit_statements ( & mut self , stmts : & mut Vec < ' a , Statement < ' a > > , ctx : & mut TraverseCtx < ' a > ) {
343- self . x0_typescript . exit_statements ( stmts, ctx) ;
392+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
393+ typescript. exit_statements ( stmts, ctx) ;
394+ }
344395 self . x1_jsx . exit_statements ( stmts, ctx) ;
345396 self . common . exit_statements ( stmts, ctx) ;
346397 }
347398
348399 fn exit_statement ( & mut self , stmt : & mut Statement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
349- self . x0_typescript . exit_statement ( stmt, ctx) ;
400+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
401+ typescript. exit_statement ( stmt, ctx) ;
402+ }
350403 self . x2_es2017 . exit_statement ( stmt, ctx) ;
351404 }
352405
@@ -355,43 +408,61 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
355408 expr : & mut TaggedTemplateExpression < ' a > ,
356409 ctx : & mut TraverseCtx < ' a > ,
357410 ) {
358- self . x0_typescript . enter_tagged_template_expression ( expr, ctx) ;
411+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
412+ typescript. enter_tagged_template_expression ( expr, ctx) ;
413+ }
359414 }
360415
361416 fn enter_statement ( & mut self , stmt : & mut Statement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
362- self . x0_typescript . enter_statement ( stmt, ctx) ;
417+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
418+ typescript. enter_statement ( stmt, ctx) ;
419+ }
363420 }
364421
365422 fn enter_declaration ( & mut self , decl : & mut Declaration < ' a > , ctx : & mut TraverseCtx < ' a > ) {
366- self . x0_typescript . enter_declaration ( decl, ctx) ;
423+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
424+ typescript. enter_declaration ( decl, ctx) ;
425+ }
367426 }
368427
369428 fn enter_if_statement ( & mut self , stmt : & mut IfStatement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
370- self . x0_typescript . enter_if_statement ( stmt, ctx) ;
429+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
430+ typescript. enter_if_statement ( stmt, ctx) ;
431+ }
371432 }
372433
373434 fn enter_while_statement ( & mut self , stmt : & mut WhileStatement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
374- self . x0_typescript . enter_while_statement ( stmt, ctx) ;
435+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
436+ typescript. enter_while_statement ( stmt, ctx) ;
437+ }
375438 }
376439
377440 fn enter_do_while_statement (
378441 & mut self ,
379442 stmt : & mut DoWhileStatement < ' a > ,
380443 ctx : & mut TraverseCtx < ' a > ,
381444 ) {
382- self . x0_typescript . enter_do_while_statement ( stmt, ctx) ;
445+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
446+ typescript. enter_do_while_statement ( stmt, ctx) ;
447+ }
383448 }
384449
385450 fn enter_for_statement ( & mut self , stmt : & mut ForStatement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
386- self . x0_typescript . enter_for_statement ( stmt, ctx) ;
451+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
452+ typescript. enter_for_statement ( stmt, ctx) ;
453+ }
387454 }
388455
389456 fn enter_for_of_statement ( & mut self , stmt : & mut ForOfStatement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
390- self . x0_typescript . enter_for_of_statement ( stmt, ctx) ;
457+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
458+ typescript. enter_for_of_statement ( stmt, ctx) ;
459+ }
391460 }
392461
393462 fn enter_for_in_statement ( & mut self , stmt : & mut ForInStatement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
394- self . x0_typescript . enter_for_in_statement ( stmt, ctx) ;
463+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
464+ typescript. enter_for_in_statement ( stmt, ctx) ;
465+ }
395466 }
396467
397468 fn enter_catch_clause ( & mut self , clause : & mut CatchClause < ' a > , ctx : & mut TraverseCtx < ' a > ) {
@@ -403,30 +474,38 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
403474 node : & mut ImportDeclaration < ' a > ,
404475 ctx : & mut TraverseCtx < ' a > ,
405476 ) {
406- self . x0_typescript . enter_import_declaration ( node, ctx) ;
477+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
478+ typescript. enter_import_declaration ( node, ctx) ;
479+ }
407480 }
408481
409482 fn enter_export_all_declaration (
410483 & mut self ,
411484 node : & mut ExportAllDeclaration < ' a > ,
412485 ctx : & mut TraverseCtx < ' a > ,
413486 ) {
414- self . x0_typescript . enter_export_all_declaration ( node, ctx) ;
487+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
488+ typescript. enter_export_all_declaration ( node, ctx) ;
489+ }
415490 }
416491
417492 fn enter_export_named_declaration (
418493 & mut self ,
419494 node : & mut ExportNamedDeclaration < ' a > ,
420495 ctx : & mut TraverseCtx < ' a > ,
421496 ) {
422- self . x0_typescript . enter_export_named_declaration ( node, ctx) ;
497+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
498+ typescript. enter_export_named_declaration ( node, ctx) ;
499+ }
423500 }
424501
425502 fn enter_ts_export_assignment (
426503 & mut self ,
427504 export_assignment : & mut TSExportAssignment < ' a > ,
428505 ctx : & mut TraverseCtx < ' a > ,
429506 ) {
430- self . x0_typescript . enter_ts_export_assignment ( export_assignment, ctx) ;
507+ if let Some ( typescript) = self . x0_typescript . as_mut ( ) {
508+ typescript. enter_ts_export_assignment ( export_assignment, ctx) ;
509+ }
431510 }
432511}
0 commit comments