Skip to content

Commit 7d1ebad

Browse files
committed
fix(isolated-declarations): incorrect nested namespace output in isolated declarations (#15800)
Dotted namespace syntax `export namespace X.Y.Z {}` was incorrectly transformed to `export declare namespace Y.Z.Z {}` instead of `export declare namespace X.Y.Z {}`. **Root cause:** Variable shadowing in `transform_ts_module_declaration`. When processing nested `TSModuleDeclaration` bodies, the pattern match variable `decl` shadowed the function parameter, causing the inner namespace name to be used instead of the outer one. **Changes:** - Renamed pattern match variable from `decl` to `inner_decl` to preserve access to outer declaration's identifier - Added test coverage for dotted namespace syntax with varying nesting depths **Example:** ```ts // Input export namespace X.Y.Z {} // Before: Y.Z.Z (incorrect) // After: X.Y.Z (correct) export declare namespace X.Y.Z { export {}; } ``` <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Isolated declarations: Incorrect output for nested `namespace`</issue_title> > <issue_description>Input: > > ```ts > // _____ > export namespace X.Y.Z {} > ``` > > Isolated declarations output: > > ```ts > // _____ > export declare namespace Y.Z.Z { > export {}; > } > ``` > > [Playground](https://playground.oxc.rs/#eNptU01v2zAM/SuBzlnRFRhQ5Da0CzCgW7J1WLdhF1pmXKWy6FF00sLIfx/lWEoK9GT5Ufx4j0+DsWZh8LkjllmAFmMHFme/Ln5f/JkNh7/BzA2ZxWC4D+njXRCz2ICPODcb4hZEkAsiDCEmuCAukgfB+hatBwZxFKJZCPca2z86OTYs11sIjT/9Wmo7xqgZExAtdRo+5seXtiKf/+ymma4d5qYDjmmuQbkJhqhtladEpQPe0/47Ss9h1Ut0NS77YNNguVJqibzDNbCmZnTM+3n9OQg7LWhLIKJOLc5+YiaeUB0hSTWOoOczpQbTR/wB1YmTQPXgank0i6u5wVCvNncuKEnjNzpup91kil9fajeXFPrWk5xk2sbn+zfgf+lvzdRpLwPxXUCssdaauian4zU31LaQYt4rqjxcIatc9pm/xvfwkrSrGOwTyr3uTNPz5YyqfY6TT/2p2qKVB4ZOa2RRU6Nx1o+iQla9Toj8Kk3diOxaDAJ+pUcQ4jVFd1xRUsiopMVqR1EFuEG1psF4dfn+g3ZRmW9xo4WXxDceYlw69HXZ2nkXNSeNbU7R1klBv6BADaJKTat929SDicqoU4MgB1BfZjNaqrHB8f2EZIRi2W2sSd/fJHkIJGOtjHhs8t2xSn4LyVH5oShz6u5wh6XoE2L3Nb3k8wHUtOSXauCUsUOuKGbBE5+QFpVC5XRQuB7lS3A5HRKeZM6cDv8BbLGFnw==) > > Maybe this code is the cause? > > https://github.com/oxc-project/oxc/blob/4608549fc7715ee76c4a6d7a78069ff339f52267/crates/oxc_isolated_declarations/src/lib.rs#L521-L523 > > Looks fishy! Shouldn't it walk down to find the nested `TSModuleBlock` when `decl.body` is a `TSModuleDeclarationBody::TSModuleDeclaration`?</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes #15729 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.
1 parent 82471c8 commit 7d1ebad

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

crates/oxc_isolated_declarations/src/declaration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ impl<'a> IsolatedDeclarations<'a> {
129129
// Follows https://github.com/microsoft/TypeScript/pull/54134
130130
let kind = TSModuleDeclarationKind::Namespace;
131131
match body {
132-
TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
133-
let inner = self.transform_ts_module_declaration(decl);
132+
TSModuleDeclarationBody::TSModuleDeclaration(inner_decl) => {
133+
let inner = self.transform_ts_module_declaration(inner_decl);
134134
self.ast.alloc_ts_module_declaration(
135135
decl.span,
136136
decl.id.clone_in(self.ast.allocator),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Dotted namespace syntax should preserve all names correctly
2+
export namespace X.Y.Z {}
3+
4+
// Nested dotted namespaces with content
5+
export namespace A.B.C {
6+
export const value = 1;
7+
export function foo(): void {}
8+
}
9+
10+
// Deeply nested
11+
export namespace Deep.Nested.Namespace.Structure {
12+
export interface Config {
13+
value: string;
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/oxc_isolated_declarations/tests/mod.rs
3+
input_file: crates/oxc_isolated_declarations/tests/fixtures/nested-namespace-dotted.ts
4+
---
5+
```
6+
==================== .D.TS ====================
7+
8+
// Dotted namespace syntax should preserve all names correctly
9+
export declare namespace X.Y.Z {
10+
export {};
11+
}
12+
// Nested dotted namespaces with content
13+
export declare namespace A.B.C {
14+
const value = 1;
15+
function foo(): void;
16+
}
17+
// Deeply nested
18+
export declare namespace Deep.Nested.Namespace.Structure {
19+
interface Config {
20+
value: string;
21+
}
22+
}

0 commit comments

Comments
 (0)