diff --git a/crates/oxc/src/napi/transform.rs b/crates/oxc/src/napi/transform.rs index aa2a46588a85c0..ecf5a49894d804 100644 --- a/crates/oxc/src/napi/transform.rs +++ b/crates/oxc/src/napi/transform.rs @@ -96,7 +96,10 @@ impl From for oxc_transformer::TransformOptions { fn from(options: TransformOptions) -> Self { Self { cwd: options.cwd.map(PathBuf::from).unwrap_or_default(), - typescript: options.typescript.map(Into::into).unwrap_or_default(), + typescript: options + .typescript + .map(oxc_transformer::TypeScriptOptions::from) + .unwrap_or_default(), jsx: options.jsx.map(Into::into).unwrap_or_default(), es2015: options.es2015.map(Into::into).unwrap_or_default(), ..Self::default() diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index dc70c8ecc1c9b7..46af910352e63d 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -92,7 +92,10 @@ impl<'a> Transformer<'a> { jsx::update_options_with_comments(&program.comments, &mut self.options, &self.ctx); let mut transformer = TransformerImpl { - x0_typescript: TypeScript::new(&self.options.typescript, &self.ctx), + x0_typescript: program + .source_type + .is_typescript() + .then(|| TypeScript::new(&self.options.typescript, &self.ctx)), x1_jsx: Jsx::new(self.options.jsx, ast_builder, &self.ctx), x2_es2022: ES2022::new(self.options.es2022), x2_es2021: ES2021::new(self.options.es2021, &self.ctx), @@ -113,7 +116,7 @@ impl<'a> Transformer<'a> { struct TransformerImpl<'a, 'ctx> { // NOTE: all callbacks must run in order. - x0_typescript: TypeScript<'a, 'ctx>, + x0_typescript: Option>, x1_jsx: Jsx<'a, 'ctx>, x2_es2022: ES2022, x2_es2021: ES2021<'a, 'ctx>, @@ -129,13 +132,17 @@ struct TransformerImpl<'a, 'ctx> { impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_program(program, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_program(program, ctx); + } self.x1_jsx.enter_program(program, ctx); } fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { self.x1_jsx.exit_program(program, ctx); - self.x0_typescript.exit_program(program, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.exit_program(program, ctx); + } self.x3_es2015.exit_program(program, ctx); self.common.exit_program(program, ctx); } @@ -147,7 +154,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { arrow: &mut ArrowFunctionExpression<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_arrow_function_expression(arrow, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_arrow_function_expression(arrow, ctx); + } } fn enter_variable_declarator( @@ -155,24 +164,34 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { decl: &mut VariableDeclarator<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_variable_declarator(decl, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_variable_declarator(decl, ctx); + } } fn enter_binding_pattern(&mut self, pat: &mut BindingPattern<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_binding_pattern(pat, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_binding_pattern(pat, ctx); + } } fn enter_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_call_expression(expr, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_call_expression(expr, ctx); + } self.x1_jsx.enter_call_expression(expr, ctx); } fn enter_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_class(class, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_class(class, ctx); + } } fn enter_class_body(&mut self, body: &mut ClassBody<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_class_body(body, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_class_body(body, ctx); + } self.x2_es2022.enter_class_body(body, ctx); } @@ -189,12 +208,16 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { decl: &mut TSModuleDeclaration<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_ts_module_declaration(decl, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_ts_module_declaration(decl, ctx); + } } #[inline] fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_expression(expr, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_expression(expr, ctx); + } self.x2_es2021.enter_expression(expr, ctx); self.x2_es2020.enter_expression(expr, ctx); self.x2_es2018.enter_expression(expr, ctx); @@ -214,7 +237,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { node: &mut SimpleAssignmentTarget<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_simple_assignment_target(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_simple_assignment_target(node, ctx); + } } fn enter_assignment_target( @@ -222,7 +247,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { node: &mut AssignmentTarget<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_assignment_target(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_assignment_target(node, ctx); + } } fn enter_formal_parameter( @@ -230,7 +257,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { param: &mut FormalParameter<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_formal_parameter(param, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_formal_parameter(param, ctx); + } } fn enter_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) { @@ -238,13 +267,17 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { } fn exit_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.exit_function(func, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.exit_function(func, ctx); + } self.x1_jsx.exit_function(func, ctx); self.x3_es2015.exit_function(func, ctx); } fn enter_jsx_element(&mut self, node: &mut JSXElement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_jsx_element(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_jsx_element(node, ctx); + } } fn enter_jsx_element_name(&mut self, node: &mut JSXElementName<'a>, ctx: &mut TraverseCtx<'a>) { @@ -260,7 +293,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { } fn enter_jsx_fragment(&mut self, node: &mut JSXFragment<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_jsx_fragment(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_jsx_fragment(node, ctx); + } } fn enter_jsx_opening_element( @@ -268,7 +303,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { elem: &mut JSXOpeningElement<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_jsx_opening_element(elem, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_jsx_opening_element(elem, ctx); + } self.x1_jsx.enter_jsx_opening_element(elem, ctx); } @@ -277,7 +314,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { def: &mut MethodDefinition<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_method_definition(def, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_method_definition(def, ctx); + } } fn exit_method_definition( @@ -285,12 +324,16 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { def: &mut MethodDefinition<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.exit_method_definition(def, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.exit_method_definition(def, ctx); + } self.x2_es2017.exit_method_definition(def, ctx); } fn enter_new_expression(&mut self, expr: &mut NewExpression<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_new_expression(expr, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_new_expression(expr, ctx); + } } fn enter_property_definition( @@ -298,7 +341,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { def: &mut PropertyDefinition<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_property_definition(def, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_property_definition(def, ctx); + } } fn enter_accessor_property( @@ -306,12 +351,16 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { node: &mut AccessorProperty<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_accessor_property(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_accessor_property(node, ctx); + } } fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { self.common.enter_statements(stmts, ctx); - self.x0_typescript.enter_statements(stmts, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_statements(stmts, ctx); + } self.x1_jsx.enter_statements(stmts, ctx); } @@ -340,13 +389,17 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { } fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.exit_statements(stmts, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.exit_statements(stmts, ctx); + } self.x1_jsx.exit_statements(stmts, ctx); self.common.exit_statements(stmts, ctx); } fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.exit_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.exit_statement(stmt, ctx); + } self.x2_es2017.exit_statement(stmt, ctx); } @@ -355,23 +408,33 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { expr: &mut TaggedTemplateExpression<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_tagged_template_expression(expr, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_tagged_template_expression(expr, ctx); + } } fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_statement(stmt, ctx); + } } fn enter_declaration(&mut self, decl: &mut Declaration<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_declaration(decl, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_declaration(decl, ctx); + } } fn enter_if_statement(&mut self, stmt: &mut IfStatement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_if_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_if_statement(stmt, ctx); + } } fn enter_while_statement(&mut self, stmt: &mut WhileStatement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_while_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_while_statement(stmt, ctx); + } } fn enter_do_while_statement( @@ -379,19 +442,27 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { stmt: &mut DoWhileStatement<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_do_while_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_do_while_statement(stmt, ctx); + } } fn enter_for_statement(&mut self, stmt: &mut ForStatement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_for_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_for_statement(stmt, ctx); + } } fn enter_for_of_statement(&mut self, stmt: &mut ForOfStatement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_for_of_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_for_of_statement(stmt, ctx); + } } fn enter_for_in_statement(&mut self, stmt: &mut ForInStatement<'a>, ctx: &mut TraverseCtx<'a>) { - self.x0_typescript.enter_for_in_statement(stmt, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_for_in_statement(stmt, ctx); + } } fn enter_catch_clause(&mut self, clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) { @@ -403,7 +474,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { node: &mut ImportDeclaration<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_import_declaration(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_import_declaration(node, ctx); + } } fn enter_export_all_declaration( @@ -411,7 +484,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { node: &mut ExportAllDeclaration<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_export_all_declaration(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_export_all_declaration(node, ctx); + } } fn enter_export_named_declaration( @@ -419,7 +494,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { node: &mut ExportNamedDeclaration<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_export_named_declaration(node, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_export_named_declaration(node, ctx); + } } fn enter_ts_export_assignment( @@ -427,6 +504,8 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { export_assignment: &mut TSExportAssignment<'a>, ctx: &mut TraverseCtx<'a>, ) { - self.x0_typescript.enter_ts_export_assignment(export_assignment, ctx); + if let Some(typescript) = self.x0_typescript.as_mut() { + typescript.enter_ts_export_assignment(export_assignment, ctx); + } } }