feat(transformer/typescript): report error for export = <value> (#3021)

This commit is contained in:
Dunqing 2024-04-18 18:56:29 +08:00 committed by GitHub
parent 7416de217b
commit 3831147b6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 32 additions and 38 deletions

View file

@ -210,7 +210,6 @@ impl<'a> VisitMut<'a> for Transformer<'a> {
} }
fn visit_statement(&mut self, stmt: &mut Statement<'a>) { fn visit_statement(&mut self, stmt: &mut Statement<'a>) {
self.x0_typescript.transform_statement(stmt);
walk_mut::walk_statement_mut(self, 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); self.x0_typescript.transform_if_statement(stmt);
walk_mut::walk_if_statement_mut(self, 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);
}
} }

View file

@ -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` // Creates `this.name = name`
fn create_this_property_assignment(&self, name: &Atom<'a>) -> Statement<'a> { fn create_this_property_assignment(&self, name: &Atom<'a>) -> Statement<'a> {
let ast = &self.ctx.ast; let ast = &self.ctx.ast;
@ -315,7 +295,6 @@ impl<'a> TypeScriptAnnotations<'a> {
// Remove TS specific statements // Remove TS specific statements
stmts.retain(|stmt| match stmt { stmts.retain(|stmt| match stmt {
Statement::ExpressionStatement(s) => !s.expression.is_typescript_syntax(), Statement::ExpressionStatement(s) => !s.expression.is_typescript_syntax(),
Statement::Declaration(s) => !s.is_typescript_syntax(),
// Ignore ModuleDeclaration as it's handled in the program // Ignore ModuleDeclaration as it's handled in the program
_ => true, _ => true,
}); });
@ -373,12 +352,4 @@ impl<'a> TypeScriptAnnotations<'a> {
) { ) {
expr.type_parameters = None; 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);
}
}
}
} }

View file

@ -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.")] #[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))] #[diagnostic(severity(warning))]
pub struct ImportEqualsRequireUnsupported(#[label] pub Span); pub struct ImportEqualsRequireUnsupported(#[label] pub Span);
#[derive(Debug, Error, Diagnostic)]
#[error("`export = <value>;` is only supported when compiling modules to CommonJS.\nPlease consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.")]
#[diagnostic(severity(warning))]
pub struct ExportAssignmentUnsupported(#[label] pub Span);

View file

@ -156,10 +156,6 @@ impl<'a> TypeScript<'a> {
self.reference_collector.visit_identifier_reference(ident); 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>) { pub fn transform_declaration(&mut self, decl: &mut Declaration<'a>) {
match decl { match decl {
Declaration::TSImportEqualsDeclaration(ts_import_equals) 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);
}
}
} }

View file

@ -2,7 +2,10 @@ use oxc_allocator::Box;
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_span::SPAN; use oxc_span::SPAN;
use super::{diagnostics::ImportEqualsRequireUnsupported, TypeScript}; use super::{
diagnostics::{ExportAssignmentUnsupported, ImportEqualsRequireUnsupported},
TypeScript,
};
impl<'a> TypeScript<'a> { impl<'a> TypeScript<'a> {
fn transform_ts_type_name(&self, type_name: &mut TSTypeName<'a>) -> Expression<'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) 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));
}
}
} }

View file

@ -1,4 +1,4 @@
Passed: 152/209 Passed: 153/209
# All Passed: # All Passed:
* babel-plugin-transform-react-jsx-source * babel-plugin-transform-react-jsx-source
@ -17,13 +17,12 @@ Passed: 152/209
* opts/optimizeConstEnums/input.ts * opts/optimizeConstEnums/input.ts
* opts/rewriteImportExtensions/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-false/input.ts
* class/accessor-allowDeclareFields-true/input.ts * class/accessor-allowDeclareFields-true/input.ts
* exports/declared-types/input.ts * exports/declared-types/input.ts
* exports/export-const-enums/input.ts * exports/export-const-enums/input.ts
* exports/export-type-star-from/input.ts * exports/export-type-star-from/input.ts
* exports/export=/input.ts
* imports/enum-id/input.ts * imports/enum-id/input.ts
* imports/enum-value/input.ts * imports/enum-value/input.ts
* imports/type-only-export-specifier-2/input.ts * imports/type-only-export-specifier-2/input.ts