Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transformer): only run typescript plugin for typescript source #6889

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/oxc/src/napi/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ impl From<TransformOptions> 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()
Expand Down
159 changes: 119 additions & 40 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<TypeScript<'a, 'ctx>>,
x1_jsx: Jsx<'a, 'ctx>,
x2_es2022: ES2022,
x2_es2021: ES2021<'a, 'ctx>,
Expand All @@ -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);
}
Expand All @@ -147,32 +154,44 @@ 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(
&mut self,
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);
}

Expand All @@ -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);
Expand All @@ -214,37 +237,47 @@ 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(
&mut self,
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(
&mut self,
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>) {
self.x3_es2015.enter_function(func, 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>) {
Expand All @@ -260,15 +293,19 @@ 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(
&mut self,
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);
}

Expand All @@ -277,41 +314,53 @@ 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(
&mut self,
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(
&mut self,
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(
&mut self,
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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -355,43 +408,61 @@ 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(
&mut self,
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>) {
Expand All @@ -403,30 +474,38 @@ 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(
&mut self,
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(
&mut self,
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(
&mut self,
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);
}
}
}
Loading
Loading