From 6e8409a020f944eee089121becdbfafac47879c9 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:22:07 +0000 Subject: [PATCH] fix(isolated-declarations): bindings referenced in `TSModuleDeclaration` are removed incorrectly (#5680) close: #5667 --- crates/oxc_isolated_declarations/src/lib.rs | 10 +++---- crates/oxc_isolated_declarations/src/scope.rs | 1 + .../tests/fixtures/module-declaration.ts | 30 ++++++++++++------- .../tests/snapshots/module-declaration.snap | 9 ++++++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/crates/oxc_isolated_declarations/src/lib.rs b/crates/oxc_isolated_declarations/src/lib.rs index 087c8f0af..079cdb6f5 100644 --- a/crates/oxc_isolated_declarations/src/lib.rs +++ b/crates/oxc_isolated_declarations/src/lib.rs @@ -146,7 +146,10 @@ impl<'a> IsolatedDeclarations<'a> { variable_transformed_indexes.push_back(FxHashSet::default()); } Declaration::TSModuleDeclaration(decl) => { - if decl.kind.is_global() { + // declare global { ... } or declare module "foo" { ... } + // We need to emit it anyway + if decl.kind.is_global() || decl.id.is_string_literal() { + // We need to visit the module declaration to collect all references self.scope.visit_ts_module_declaration(decl); transformed_indexes.insert(new_stmts.len()); } @@ -293,11 +296,6 @@ impl<'a> IsolatedDeclarations<'a> { new_ast_stmts.push(Statement::ImportDeclaration(decl)); } } - Statement::TSModuleDeclaration(decl) => { - if decl.kind.is_global() || decl.id.is_string_literal() { - new_ast_stmts.push(Statement::TSModuleDeclaration(decl)); - } - } _ => {} } } diff --git a/crates/oxc_isolated_declarations/src/scope.rs b/crates/oxc_isolated_declarations/src/scope.rs index 5a5326e1f..921c48f6a 100644 --- a/crates/oxc_isolated_declarations/src/scope.rs +++ b/crates/oxc_isolated_declarations/src/scope.rs @@ -33,6 +33,7 @@ impl<'a> Scope<'a> { } /// Linear tree of declaration scopes. +#[derive(Debug)] pub struct ScopeTree<'a> { levels: Vec<'a, Scope<'a>>, } diff --git a/crates/oxc_isolated_declarations/tests/fixtures/module-declaration.ts b/crates/oxc_isolated_declarations/tests/fixtures/module-declaration.ts index f88da9d55..e3445ee25 100644 --- a/crates/oxc_isolated_declarations/tests/fixtures/module-declaration.ts +++ b/crates/oxc_isolated_declarations/tests/fixtures/module-declaration.ts @@ -1,21 +1,31 @@ -import 'foo'; -declare module 'foo' { - interface Foo {} - const foo = 42; +import "foo"; +declare module "foo" { + interface Foo {} + const foo = 42; } declare global { - interface Bar {} - const bar = 42 ; + interface Bar {} + const bar = 42; +} + +import { type X } from "./x"; +type Y = 1; + +declare module "foo" { + interface Foo { + x: X; + y: Y; + } } // should not be emitted module baz { - interface Baz {} - const baz = 42; + interface Baz {} + const baz = 42; } declare module x { - interface Qux {} - const qux = 42; + interface Qux {} + const qux = 42; } diff --git a/crates/oxc_isolated_declarations/tests/snapshots/module-declaration.snap b/crates/oxc_isolated_declarations/tests/snapshots/module-declaration.snap index 2c3ddd956..1096eb034 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/module-declaration.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/module-declaration.snap @@ -13,3 +13,12 @@ declare global { interface Bar {} const bar = 42; } +import { type X } from "./x"; +type Y = 1; +declare module "foo" { + interface Foo { + x: X; + y: Y; + } +} +export {};