diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index bc6fc856c..527d6fbe3 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -210,7 +210,6 @@ impl<'a> VisitMut<'a> for Transformer<'a> { } fn visit_statement(&mut self, stmt: &mut Statement<'a>) { - self.x0_typescript.transform_statement(stmt); walk_mut::walk_statement_mut(self, stmt); } @@ -223,4 +222,9 @@ impl<'a> VisitMut<'a> for Transformer<'a> { self.x0_typescript.transform_if_statement(stmt); walk_mut::walk_if_statement_mut(self, stmt); } + + fn visit_module_declaration(&mut self, decl: &mut ModuleDeclaration<'a>) { + self.x0_typescript.transform_module_declaration(decl); + walk_mut::walk_module_declaration_mut(self, decl); + } } diff --git a/crates/oxc_transformer/src/typescript/annotations.rs b/crates/oxc_transformer/src/typescript/annotations.rs index 55c81f12e..705dabb59 100644 --- a/crates/oxc_transformer/src/typescript/annotations.rs +++ b/crates/oxc_transformer/src/typescript/annotations.rs @@ -32,26 +32,6 @@ impl<'a> TypeScriptAnnotations<'a> { } } - // Convert `export = expr` into `module.exports = expr` - fn create_module_exports(&self, exp: &TSExportAssignment<'a>) -> Statement<'a> { - let ast = &self.ctx.ast; - - ast.expression_statement( - SPAN, - ast.assignment_expression( - SPAN, - AssignmentOperator::Assign, - ast.simple_assignment_target_member_expression(ast.static_member( - SPAN, - ast.identifier_reference_expression(ast.identifier_reference(SPAN, "module")), - ast.identifier_name(SPAN, "exports"), - false, - )), - ast.copy(&exp.expression), - ), - ) - } - // Creates `this.name = name` fn create_this_property_assignment(&self, name: &Atom<'a>) -> Statement<'a> { let ast = &self.ctx.ast; @@ -315,7 +295,6 @@ impl<'a> TypeScriptAnnotations<'a> { // Remove TS specific statements stmts.retain(|stmt| match stmt { Statement::ExpressionStatement(s) => !s.expression.is_typescript_syntax(), - Statement::Declaration(s) => !s.is_typescript_syntax(), // Ignore ModuleDeclaration as it's handled in the program _ => true, }); @@ -373,12 +352,4 @@ impl<'a> TypeScriptAnnotations<'a> { ) { expr.type_parameters = None; } - - pub fn transform_statement(&mut self, decl: &mut Statement<'a>) { - if let Statement::ModuleDeclaration(module_decl) = decl { - if let ModuleDeclaration::TSExportAssignment(exp) = &mut **module_decl { - *decl = self.create_module_exports(exp); - } - } - } } diff --git a/crates/oxc_transformer/src/typescript/diagnostics.rs b/crates/oxc_transformer/src/typescript/diagnostics.rs index a7d043c45..32aff2770 100644 --- a/crates/oxc_transformer/src/typescript/diagnostics.rs +++ b/crates/oxc_transformer/src/typescript/diagnostics.rs @@ -8,3 +8,8 @@ use oxc_span::Span; #[error("`import lib = require(...);` is only supported when compiling modules to CommonJS.\nPlease consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config.")] #[diagnostic(severity(warning))] pub struct ImportEqualsRequireUnsupported(#[label] pub Span); + +#[derive(Debug, Error, Diagnostic)] +#[error("`export = ;` is only supported when compiling modules to CommonJS.\nPlease consider using `export default ;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.")] +#[diagnostic(severity(warning))] +pub struct ExportAssignmentUnsupported(#[label] pub Span); diff --git a/crates/oxc_transformer/src/typescript/mod.rs b/crates/oxc_transformer/src/typescript/mod.rs index 3910db10d..f2d33fcb0 100644 --- a/crates/oxc_transformer/src/typescript/mod.rs +++ b/crates/oxc_transformer/src/typescript/mod.rs @@ -156,10 +156,6 @@ impl<'a> TypeScript<'a> { self.reference_collector.visit_identifier_reference(ident); } - pub fn transform_statement(&mut self, stmt: &mut Statement<'a>) { - self.annotations.transform_statement(stmt); - } - pub fn transform_declaration(&mut self, decl: &mut Declaration<'a>) { match decl { Declaration::TSImportEqualsDeclaration(ts_import_equals) @@ -175,4 +171,10 @@ impl<'a> TypeScript<'a> { _ => {} } } + + pub fn transform_module_declaration(&mut self, module_decl: &mut ModuleDeclaration<'a>) { + if let ModuleDeclaration::TSExportAssignment(ts_export_assignment) = &mut *module_decl { + self.transform_ts_export_assignment(ts_export_assignment); + } + } } diff --git a/crates/oxc_transformer/src/typescript/module.rs b/crates/oxc_transformer/src/typescript/module.rs index 5de68f696..f5ac95d91 100644 --- a/crates/oxc_transformer/src/typescript/module.rs +++ b/crates/oxc_transformer/src/typescript/module.rs @@ -2,7 +2,10 @@ use oxc_allocator::Box; use oxc_ast::ast::*; use oxc_span::SPAN; -use super::{diagnostics::ImportEqualsRequireUnsupported, TypeScript}; +use super::{ + diagnostics::{ExportAssignmentUnsupported, ImportEqualsRequireUnsupported}, + TypeScript, +}; impl<'a> TypeScript<'a> { fn transform_ts_type_name(&self, type_name: &mut TSTypeName<'a>) -> Expression<'a> { @@ -70,4 +73,14 @@ impl<'a> TypeScript<'a> { Declaration::VariableDeclaration(variable_declaration) } + + pub fn transform_ts_export_assignment( + &mut self, + export_assignment: &mut TSExportAssignment<'a>, + ) { + println!("{:?}", self.ctx.source_type()); + if self.ctx.source_type().is_module() { + self.ctx.error(ExportAssignmentUnsupported(export_assignment.span)); + } + } } diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index d70c53de0..443f57d3a 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,4 +1,4 @@ -Passed: 152/209 +Passed: 153/209 # All Passed: * babel-plugin-transform-react-jsx-source @@ -17,13 +17,12 @@ Passed: 152/209 * opts/optimizeConstEnums/input.ts * opts/rewriteImportExtensions/input.ts -# babel-plugin-transform-typescript (96/139) +# babel-plugin-transform-typescript (97/139) * class/accessor-allowDeclareFields-false/input.ts * class/accessor-allowDeclareFields-true/input.ts * exports/declared-types/input.ts * exports/export-const-enums/input.ts * exports/export-type-star-from/input.ts -* exports/export=/input.ts * imports/enum-id/input.ts * imports/enum-value/input.ts * imports/type-only-export-specifier-2/input.ts