Skip to content

Commit

Permalink
[ts] Fix transform for nested namespaces shorthand syntax (#13664)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 29, 2021
1 parent 64d116b commit 1355d00
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
38 changes: 35 additions & 3 deletions packages/babel-plugin-transform-typescript/src/namespace.ts
@@ -1,4 +1,5 @@
import { template, types as t } from "@babel/core";
import type { NodePath } from "@babel/traverse";

export default function transpileNamespace(path, t, allowNamespaces) {
if (path.node.declare || path.node.id.type === "StringLiteral") {
Expand Down Expand Up @@ -102,18 +103,45 @@ function handleVariableDeclaration(
return [node, t.expressionStatement(t.sequenceExpression(assignments))];
}

function handleNested(path, t, node, parentExport?) {
function buildNestedAmbiendModuleError(path: NodePath, node: t.Node) {
throw path.hub.buildError(
node,
"Ambient modules cannot be nested in other modules or namespaces.",
Error,
);
}

function handleNested(
path: NodePath,
t: typeof import("@babel/types"),
node: t.TSModuleDeclaration,
parentExport?,
) {
const names = new Set();
const realName = node.id;
t.assertIdentifier(realName);

const name = path.scope.generateUid(realName.name);
const namespaceTopLevel = node.body.body;

const namespaceTopLevel: t.Statement[] = t.isTSModuleBlock(node.body)
? node.body.body
: // We handle `namespace X.Y {}` as if it was
// namespace X {
// export namespace Y {}
// }
[t.exportNamedDeclaration(node.body)];

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

// The first switch is mainly to detect name usage. Only export
// declarations require further transformation.
switch (subNode.type) {
case "TSModuleDeclaration": {
if (!t.isIdentifier(subNode.id)) {
throw buildNestedAmbiendModuleError(path, subNode);
}

const transformed = handleNested(path, t, subNode);
const moduleName = subNode.id.name;
if (names.has(moduleName)) {
Expand Down Expand Up @@ -181,6 +209,10 @@ function handleNested(path, t, node, parentExport?) {
break;
}
case "TSModuleDeclaration": {
if (!t.isIdentifier(subNode.declaration.id)) {
throw buildNestedAmbiendModuleError(path, subNode.declaration);
}

const transformed = handleNested(
path,
t,
Expand All @@ -204,7 +236,7 @@ function handleNested(path, t, node, parentExport?) {
}

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

if (parentExport) {
const memberExpr = t.memberExpression(parentExport, realName);
Expand Down
@@ -0,0 +1,3 @@
namespace X {
export module "m" {}
}
@@ -0,0 +1,3 @@
{
"throws": "Ambient modules cannot be nested in other modules or namespaces."
}
@@ -0,0 +1,3 @@
namespace X {
module "m" {}
}
@@ -0,0 +1,3 @@
{
"throws": "Ambient modules cannot be nested in other modules or namespaces."
}
@@ -0,0 +1,7 @@
namespace X.Y {
export const Z = 1;
}

namespace proj.data.util.api {
export const X = 1;
}
@@ -0,0 +1,27 @@
let X;

(function (_X) {
let Y;

(function (_Y) {
const Z = _Y.Z = 1;
})(Y || (Y = _X.Y || (_X.Y = {})));
})(X || (X = {}));

let proj;

(function (_proj) {
let data;

(function (_data) {
let util;

(function (_util) {
let api;

(function (_api) {
const X = _api.X = 1;
})(api || (api = _util.api || (_util.api = {})));
})(util || (util = _data.util || (_data.util = {})));
})(data || (data = _proj.data || (_proj.data = {})));
})(proj || (proj = {}));

0 comments on commit 1355d00

Please sign in to comment.