Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts] Fix transform for nested namespaces shorthand syntax #13664

Merged
merged 3 commits into from Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = {}));