diff --git a/crates/oxc_isolated_declarations/src/declaration.rs b/crates/oxc_isolated_declarations/src/declaration.rs index fe049d3c5038b..6943a2083b901 100644 --- a/crates/oxc_isolated_declarations/src/declaration.rs +++ b/crates/oxc_isolated_declarations/src/declaration.rs @@ -203,7 +203,7 @@ impl<'a> IsolatedDeclarations<'a> { || matches!( &decl.id, TSModuleDeclarationName::Identifier(ident) - if self.scope.has_value_reference(&ident.name) + if self.scope.has_reference(&ident.name) ) { Some(Declaration::TSModuleDeclaration( diff --git a/crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference-complex.ts b/crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference-complex.ts new file mode 100644 index 0000000000000..f83a175e10303 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference-complex.ts @@ -0,0 +1,47 @@ +// Test various namespace reference scenarios + +// Simple namespace reference +export type SimpleRef = NS1.Type1; + +namespace NS1 { + export type Type1 = string; +} + +// Nested namespace reference +export type NestedRef = Outer.Inner.Type2; + +namespace Outer { + export namespace Inner { + export type Type2 = number; + } +} + +// Multiple references to same namespace +export type Ref1 = NS2.TypeA; +export type Ref2 = NS2.TypeB; + +namespace NS2 { + export type TypeA = boolean; + export type TypeB = symbol; +} + +// Namespace used in union type +export type UnionRef = NS3.Type3 | string; + +namespace NS3 { + export type Type3 = "literal"; +} + +// Namespace used in interface +export interface InterfaceWithNS { + field: NS4.Type4; +} + +namespace NS4 { + export type Type4 = bigint; +} + +// Unreferenced namespace should not be included +namespace UnusedNS { + export type UnusedType = never; +} diff --git a/crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference.ts b/crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference.ts new file mode 100644 index 0000000000000..5b8359fffac7f --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference.ts @@ -0,0 +1,6 @@ +// This ends up being `any` +export type A = Foo.Bar; + +namespace Foo { + export type Bar = "bar" +} diff --git a/crates/oxc_isolated_declarations/tests/snapshots/namespace-type-reference-complex.snap b/crates/oxc_isolated_declarations/tests/snapshots/namespace-type-reference-complex.snap new file mode 100644 index 0000000000000..53196efdf4e88 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/namespace-type-reference-complex.snap @@ -0,0 +1,40 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference-complex.ts +--- +``` +==================== .D.TS ==================== + +// Test various namespace reference scenarios +// Simple namespace reference +export type SimpleRef = NS1.Type1; +declare namespace NS1 { + type Type1 = string; +} +// Nested namespace reference +export type NestedRef = Outer.Inner.Type2; +declare namespace Outer { + namespace Inner { + type Type2 = number; + } +} +// Multiple references to same namespace +export type Ref1 = NS2.TypeA; +export type Ref2 = NS2.TypeB; +declare namespace NS2 { + type TypeA = boolean; + type TypeB = symbol; +} +// Namespace used in union type +export type UnionRef = NS3.Type3 | string; +declare namespace NS3 { + type Type3 = "literal"; +} +// Namespace used in interface +export interface InterfaceWithNS { + field: NS4.Type4; +} +declare namespace NS4 { + type Type4 = bigint; +} +export {}; diff --git a/crates/oxc_isolated_declarations/tests/snapshots/namespace-type-reference.snap b/crates/oxc_isolated_declarations/tests/snapshots/namespace-type-reference.snap new file mode 100644 index 0000000000000..8225aa1dc182d --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/namespace-type-reference.snap @@ -0,0 +1,13 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/namespace-type-reference.ts +--- +``` +==================== .D.TS ==================== + +// This ends up being `any` +export type A = Foo.Bar; +declare namespace Foo { + type Bar = "bar"; +} +export {}; diff --git a/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap b/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap index 0e7c4df574cff..e59a8b2e408d0 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap @@ -13,5 +13,8 @@ export type Bar = { type Func = () => void; export type FuncType = Func; type Module = () => void; +declare namespace Module { + const x = 1; +} export type ModuleType = Module; export {};