Skip to content

Commit

Permalink
[ts] Fix compiling extended exported nested namespace (#15798)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 20, 2023
1 parent 3caeeb1 commit 12dfea7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
23 changes: 23 additions & 0 deletions packages/babel-plugin-transform-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,29 @@ export default declare((api, opts: Options) => {
return;
}

// Convert `export namespace X {}` into `export let X; namespace X {}`,
// so that when visiting TSModuleDeclaration we do not have to possibly
// replace its parent path.
if (t.isTSModuleDeclaration(path.node.declaration)) {
const namespace = path.node.declaration;
const { id } = namespace;
if (t.isIdentifier(id)) {
if (path.scope.hasOwnBinding(id.name)) {
path.replaceWith(namespace);
} else {
const [newExport] = path.replaceWithMultiple([
t.exportNamedDeclaration(
t.variableDeclaration("let", [
t.variableDeclarator(t.cloneNode(id)),
]),
),
namespace,
]);
path.scope.registerDeclaration(newExport);
}
}
}

NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false);
},

Expand Down
11 changes: 1 addition & 10 deletions packages/babel-plugin-transform-typescript/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ export default function transpileNamespace(

const name = path.node.id.name;
const value = handleNested(path, t.cloneNode(path.node, true));
const bound = path.scope.hasOwnBinding(name);
if (path.parent.type === "ExportNamedDeclaration") {
if (!bound) {
path.parentPath.insertAfter(value);
path.replaceWith(getDeclaration(name));
path.scope.registerDeclaration(path.parentPath);
} else {
path.parentPath.replaceWith(value);
}
} else if (bound) {
if (path.scope.hasOwnBinding(name)) {
path.replaceWith(value);
} else {
path.scope.registerDeclaration(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export namespace ui.a {
export const x = 1;
}
export namespace ui.b {
export const y = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export let ui;
(function (_ui2) {
let a;
(function (_a) {
const x = _a.x = 1;
})(a || (a = _ui2.a || (_ui2.a = {})));
})(ui || (ui = {}));
(function (_ui) {
let b;
(function (_b) {
const y = _b.y = 2;
})(b || (b = _ui.b || (_ui.b = {})));
})(ui || (ui = {}));

0 comments on commit 12dfea7

Please sign in to comment.