feat(transformer): report ambient module cannot be nested error (#3253)

This commit is contained in:
Dunqing 2024-05-13 07:51:06 +00:00
parent 7ed673d2a5
commit 34dd53cc98
4 changed files with 21 additions and 5 deletions

View file

@ -861,6 +861,10 @@ pub enum TSModuleDeclarationName<'a> {
} }
impl<'a> TSModuleDeclarationName<'a> { impl<'a> TSModuleDeclarationName<'a> {
pub fn is_string_literal(&self) -> bool {
matches!(self, Self::StringLiteral(_))
}
pub fn name(&self) -> &Atom<'a> { pub fn name(&self) -> &Atom<'a> {
match self { match self {
Self::Identifier(ident) => &ident.name, Self::Identifier(ident) => &ident.name,

View file

@ -10,3 +10,8 @@ pub fn export_assignment_unsupported(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warning("`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.") OxcDiagnostic::warning("`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.")
.with_labels([span0.into()]) .with_labels([span0.into()])
} }
pub fn ambient_module_nested(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warning("Ambient modules cannot be nested in other modules or namespaces.")
.with_label(span0)
}

View file

@ -1,6 +1,6 @@
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use super::TypeScript; use super::{diagnostics::ambient_module_nested, TypeScript};
use oxc_allocator::{Box, Vec}; use oxc_allocator::{Box, Vec};
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames}; use oxc_ast::{ast::*, syntax_directed_operations::BoundNames};
@ -121,6 +121,7 @@ impl<'a> TypeScript<'a> {
parent_export: Option<Expression<'a>>, parent_export: Option<Expression<'a>>,
) -> Option<Statement<'a>> { ) -> Option<Statement<'a>> {
let mut names: FxHashSet<Atom<'a>> = FxHashSet::default(); let mut names: FxHashSet<Atom<'a>> = FxHashSet::default();
let real_name = decl.id.name(); let real_name = decl.id.name();
let name = self.ctx.ast.new_atom(&format!("_{}", real_name.clone())); // path.scope.generateUid(realName.name); let name = self.ctx.ast.new_atom(&format!("_{}", real_name.clone())); // path.scope.generateUid(realName.name);
@ -155,6 +156,10 @@ impl<'a> TypeScript<'a> {
for stmt in namespace_top_level { for stmt in namespace_top_level {
match stmt { match stmt {
Statement::TSModuleDeclaration(decl) => { Statement::TSModuleDeclaration(decl) => {
if decl.id.is_string_literal() {
self.ctx.error(ambient_module_nested(decl.span));
}
let module_name = decl.id.name().clone(); let module_name = decl.id.name().clone();
if let Some(transformed) = self.handle_nested(decl.unbox(), None) { if let Some(transformed) = self.handle_nested(decl.unbox(), None) {
is_empty = false; is_empty = false;
@ -218,6 +223,10 @@ impl<'a> TypeScript<'a> {
new_stmts.extend(stmts); new_stmts.extend(stmts);
} }
Declaration::TSModuleDeclaration(module_decl) => { Declaration::TSModuleDeclaration(module_decl) => {
if module_decl.id.is_string_literal() {
self.ctx.error(ambient_module_nested(module_decl.span));
}
let module_name = module_decl.id.name().clone(); let module_name = module_decl.id.name().clone();
if let Some(transformed) = self.handle_nested( if let Some(transformed) = self.handle_nested(
module_decl.unbox(), module_decl.unbox(),

View file

@ -1,4 +1,4 @@
Passed: 304/362 Passed: 306/362
# All Passed: # All Passed:
* babel-preset-react * babel-preset-react
@ -24,7 +24,7 @@ Passed: 304/362
* opts/optimizeConstEnums/input.ts * opts/optimizeConstEnums/input.ts
* opts/rewriteImportExtensions/input.ts * opts/rewriteImportExtensions/input.ts
# babel-plugin-transform-typescript (119/156) # babel-plugin-transform-typescript (121/156)
* class/accessor-allowDeclareFields-false/input.ts * class/accessor-allowDeclareFields-false/input.ts
* class/accessor-allowDeclareFields-true/input.ts * class/accessor-allowDeclareFields-true/input.ts
* enum/mix-references/input.ts * enum/mix-references/input.ts
@ -34,8 +34,6 @@ Passed: 304/362
* exports/export-type-star-from/input.ts * exports/export-type-star-from/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
* namespace/ambient-module-nested/input.ts
* namespace/ambient-module-nested-exported/input.ts
* namespace/canonical/input.ts * namespace/canonical/input.ts
* namespace/contentious-names/input.ts * namespace/contentious-names/input.ts
* namespace/empty-removed/input.ts * namespace/empty-removed/input.ts