Skip to content

Commit

Permalink
feat(transformer): report ambient module cannot be nested error (#3253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed May 13, 2024
1 parent 7ed673d commit 34dd53c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ pub enum TSModuleDeclarationName<'a> {
}

impl<'a> TSModuleDeclarationName<'a> {
pub fn is_string_literal(&self) -> bool {
matches!(self, Self::StringLiteral(_))
}

pub fn name(&self) -> &Atom<'a> {
match self {
Self::Identifier(ident) => &ident.name,
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_transformer/src/typescript/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
.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)
}
11 changes: 10 additions & 1 deletion crates/oxc_transformer/src/typescript/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashSet;

use super::TypeScript;
use super::{diagnostics::ambient_module_nested, TypeScript};

use oxc_allocator::{Box, Vec};
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames};
Expand Down Expand Up @@ -121,6 +121,7 @@ impl<'a> TypeScript<'a> {
parent_export: Option<Expression<'a>>,
) -> Option<Statement<'a>> {
let mut names: FxHashSet<Atom<'a>> = FxHashSet::default();

let real_name = decl.id.name();

let name = self.ctx.ast.new_atom(&format!("_{}", real_name.clone())); // path.scope.generateUid(realName.name);
Expand Down Expand Up @@ -155,6 +156,10 @@ impl<'a> TypeScript<'a> {
for stmt in namespace_top_level {
match stmt {
Statement::TSModuleDeclaration(decl) => {
if decl.id.is_string_literal() {
self.ctx.error(ambient_module_nested(decl.span));
}

let module_name = decl.id.name().clone();
if let Some(transformed) = self.handle_nested(decl.unbox(), None) {
is_empty = false;
Expand Down Expand Up @@ -218,6 +223,10 @@ impl<'a> TypeScript<'a> {
new_stmts.extend(stmts);
}
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();
if let Some(transformed) = self.handle_nested(
module_decl.unbox(),
Expand Down
6 changes: 2 additions & 4 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Passed: 304/362
Passed: 306/362

# All Passed:
* babel-preset-react
Expand All @@ -24,7 +24,7 @@ Passed: 304/362
* opts/optimizeConstEnums/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-true/input.ts
* enum/mix-references/input.ts
Expand All @@ -34,8 +34,6 @@ Passed: 304/362
* exports/export-type-star-from/input.ts
* imports/enum-value/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/contentious-names/input.ts
* namespace/empty-removed/input.ts
Expand Down

0 comments on commit 34dd53c

Please sign in to comment.