Skip to content

Commit

Permalink
[ts] Strip type-only namespaces (#15799)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 20, 2023
1 parent 12dfea7 commit 36acc87
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 114 deletions.
65 changes: 41 additions & 24 deletions packages/babel-plugin-transform-typescript/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export default function transpileNamespace(

const name = path.node.id.name;
const value = handleNested(path, t.cloneNode(path.node, true));
if (path.scope.hasOwnBinding(name)) {
if (value === null) {
path.remove();
} else if (path.scope.hasOwnBinding(name)) {
path.replaceWith(value);
} else {
path.scope.registerDeclaration(
Expand Down Expand Up @@ -110,7 +112,7 @@ function handleNested(
path: NodePath,
node: t.TSModuleDeclaration,
parentExport?: t.Expression,
) {
): t.Statement | null {
const names = new Set();
const realName = node.id;
t.assertIdentifier(realName);
Expand All @@ -125,6 +127,8 @@ function handleNested(
// }
[t.exportNamedDeclaration(node.body)];

let isEmpty = true;

for (let i = 0; i < namespaceTopLevel.length; i++) {
const subNode = namespaceTopLevel[i];

Expand All @@ -137,26 +141,31 @@ function handleNested(
}

const transformed = handleNested(path, subNode);
const moduleName = subNode.id.name;
if (names.has(moduleName)) {
namespaceTopLevel[i] = transformed;
} else {
names.add(moduleName);
namespaceTopLevel.splice(
i++,
1,
getDeclaration(moduleName),
transformed,
);
if (transformed !== null) {
isEmpty = false;
const moduleName = subNode.id.name;
if (names.has(moduleName)) {
namespaceTopLevel[i] = transformed;
} else {
names.add(moduleName);
namespaceTopLevel.splice(
i++,
1,
getDeclaration(moduleName),
transformed,
);
}
}
continue;
}
case "TSEnumDeclaration":
case "FunctionDeclaration":
case "ClassDeclaration":
isEmpty = false;
names.add(subNode.id.name);
continue;
case "VariableDeclaration": {
isEmpty = false;
// getBindingIdentifiers returns an object without prototype.
// eslint-disable-next-line guard-for-in
for (const name in t.getBindingIdentifiers(subNode)) {
Expand All @@ -165,6 +174,7 @@ function handleNested(
continue;
}
default:
isEmpty &&= t.isTypeScript(subNode);
// Neither named declaration nor export, continue to next item.
continue;
case "ExportNamedDeclaration":
Expand All @@ -180,6 +190,7 @@ function handleNested(
case "TSEnumDeclaration":
case "FunctionDeclaration":
case "ClassDeclaration": {
isEmpty = false;
const itemName = subNode.declaration.id.name;
names.add(itemName);
namespaceTopLevel.splice(
Expand All @@ -197,6 +208,7 @@ function handleNested(
break;
}
case "VariableDeclaration": {
isEmpty = false;
const nodes = handleVariableDeclaration(
subNode.declaration,
name,
Expand All @@ -216,22 +228,27 @@ function handleNested(
subNode.declaration,
t.identifier(name),
);
const moduleName = subNode.declaration.id.name;
if (names.has(moduleName)) {
namespaceTopLevel[i] = transformed;
} else {
names.add(moduleName);
namespaceTopLevel.splice(
i++,
1,
getDeclaration(moduleName),
transformed,
);
if (transformed !== null) {
isEmpty = false;
const moduleName = subNode.declaration.id.name;
if (names.has(moduleName)) {
namespaceTopLevel[i] = transformed;
} else {
names.add(moduleName);
namespaceTopLevel.splice(
i++,
1,
getDeclaration(moduleName),
transformed,
);
}
}
}
}
}

if (isEmpty) return null;

// {}
let fallthroughValue: t.Expression = t.objectExpression([]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export class N {}
export namespace N {}
export namespace N { var x; }
export default N;
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export class N {}
(function (_N) {})(N || (N = {}));
(function (_N) {
var x;
})(N || (N = {}));
export default N;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import N from 'n';

namespace N {}
namespace N { var x }
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import N from 'n';
(function (_N) {})(N || (N = {}));
(function (_N) {
var x;
})(N || (N = {}));
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
namespace N {
namespace N {}
namespace constructor {}
namespace length {}
namespace concat {}
namespace copyWithin {}
namespace fill {}
namespace find {}
namespace findIndex {}
namespace lastIndexOf {}
namespace pop {}
namespace push {}
namespace reverse {}
namespace shift {}
namespace unshift {}
namespace slice {}
namespace sort {}
namespace splice {}
namespace includes {}
namespace indexOf {}
namespace join {}
namespace keys {}
namespace entries {}
namespace values {}
namespace forEach {}
namespace filter {}
namespace map {}
namespace every {}
namespace some {}
namespace reduce {}
namespace reduceRight {}
namespace toLocaleString {}
namespace toString {}
namespace flat {}
namespace flatMap {}
namespace N { var x }
namespace constructor { var x }
namespace length { var x }
namespace concat { var x }
namespace copyWithin { var x }
namespace fill { var x }
namespace find { var x }
namespace findIndex { var x }
namespace lastIndexOf { var x }
namespace pop { var x }
namespace push { var x }
namespace reverse { var x }
namespace shift { var x }
namespace unshift { var x }
namespace slice { var x }
namespace sort { var x }
namespace splice { var x }
namespace includes { var x }
namespace indexOf { var x }
namespace join { var x }
namespace keys { var x }
namespace entries { var x }
namespace values { var x }
namespace forEach { var x }
namespace filter { var x }
namespace map { var x }
namespace every { var x }
namespace some { var x }
namespace reduce { var x }
namespace reduceRight { var x }
namespace toLocaleString { var x }
namespace toString { var x }
namespace flat { var x }
namespace flatMap { var x }
}

0 comments on commit 36acc87

Please sign in to comment.