Skip to content

Commit 883713a

Browse files
fix(transform): preserve export keywords on nested namespaces (#360)
Fixes #359 Removes two problematic code sections from PR #352: 1. Lines 26-28: Unnecessary duplicateExports() call that caused superfluous export name duplication in namespace bodies 2. Lines 596-605: Incorrect removal of export keywords from nested namespaces, which changed their accessibility from public to private The plugin should bundle declarations, not transform their semantics. Nested namespaces with export keywords must remain exported to: - Maintain public API surface for downstream consumers - Preserve correct scoping in API documentation - Keep original type semantics intact
1 parent 963a4fc commit 883713a

File tree

4 files changed

+4
-16
lines changed

4 files changed

+4
-16
lines changed

src/transform/preprocess.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ function preProcessNamespaceBody(body: ts.ModuleBlock, code: MagicString, source
2323
// Safely call the new context-aware function on all children
2424
fixModifiers(code, stmt);
2525

26-
if (ts.isModuleDeclaration(stmt)) {
27-
duplicateExports(code, stmt);
28-
}
29-
3026
// Recurse for nested namespaces
3127
if (ts.isModuleDeclaration(stmt) && stmt.body && ts.isModuleBlock(stmt.body)) {
3228
preProcessNamespaceBody(stmt.body, code, sourceFile);
@@ -593,16 +589,8 @@ function fixModifiers(code: MagicString, node: ts.Node) {
593589
if (needsDeclare && !hasDeclare) {
594590
code.appendRight(node.getStart(), "declare ");
595591
}
596-
} else {
597-
// For statements inside a namespace/module, only remove `export` from a nested namespace
598-
// Leave all other members (vars, types, etc.) untouched
599-
if (ts.isModuleDeclaration(node)) {
600-
const exportKeyword = node.modifiers?.find((mod) => mod.kind === ts.SyntaxKind.ExportKeyword);
601-
if (exportKeyword) {
602-
code.remove(exportKeyword.getStart(), exportKeyword.getEnd() + 1);
603-
}
604-
}
605592
}
593+
// For statements inside namespaces, preserve all modifiers (including export)
606594
}
607595

608596
function duplicateExports(code: MagicString, module: ts.ModuleDeclaration) {

tests/testcases/namespace-nested-export/expected.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ interface External {
22
id: string;
33
}
44
declare namespace Container {
5-
namespace Nested {
5+
export namespace Nested {
66
// Import inside nested namespace with export keyword preserved
77
import Local = External;
88
export type FinalType = Local;

tests/testcases/namespace-nested-shadowing/expected.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ interface TargetType {
22
version: 'v2';
33
}
44
declare namespace Outer {
5-
namespace Inner {
5+
export namespace Inner {
66
// Import shadows outer interface, verifies scoping and tree-shaking
77
import ShadowedType = TargetType;
88
export type Result = ShadowedType;

tests/testcases/namespace-reexport-interface/expected.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare namespace ArrayObjectStore {
44
id: string;
55
value: number;
66
}
7-
namespace Util {
7+
export namespace Util {
88
export function helper(): void;
99
}
1010
}

0 commit comments

Comments
 (0)