[ts] Fix transform for nested namespaces shorthand syntax (#13664)
This commit is contained in:
parent
64d116bd6a
commit
1355d0078c
@ -1,4 +1,5 @@
|
|||||||
import { template, types as t } from "@babel/core";
|
import { template, types as t } from "@babel/core";
|
||||||
|
import type { NodePath } from "@babel/traverse";
|
||||||
|
|
||||||
export default function transpileNamespace(path, t, allowNamespaces) {
|
export default function transpileNamespace(path, t, allowNamespaces) {
|
||||||
if (path.node.declare || path.node.id.type === "StringLiteral") {
|
if (path.node.declare || path.node.id.type === "StringLiteral") {
|
||||||
@ -102,11 +103,34 @@ function handleVariableDeclaration(
|
|||||||
return [node, t.expressionStatement(t.sequenceExpression(assignments))];
|
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 names = new Set();
|
||||||
const realName = node.id;
|
const realName = node.id;
|
||||||
|
t.assertIdentifier(realName);
|
||||||
|
|
||||||
const name = path.scope.generateUid(realName.name);
|
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++) {
|
for (let i = 0; i < namespaceTopLevel.length; i++) {
|
||||||
const subNode = namespaceTopLevel[i];
|
const subNode = namespaceTopLevel[i];
|
||||||
|
|
||||||
@ -114,6 +138,10 @@ function handleNested(path, t, node, parentExport?) {
|
|||||||
// declarations require further transformation.
|
// declarations require further transformation.
|
||||||
switch (subNode.type) {
|
switch (subNode.type) {
|
||||||
case "TSModuleDeclaration": {
|
case "TSModuleDeclaration": {
|
||||||
|
if (!t.isIdentifier(subNode.id)) {
|
||||||
|
throw buildNestedAmbiendModuleError(path, subNode);
|
||||||
|
}
|
||||||
|
|
||||||
const transformed = handleNested(path, t, subNode);
|
const transformed = handleNested(path, t, subNode);
|
||||||
const moduleName = subNode.id.name;
|
const moduleName = subNode.id.name;
|
||||||
if (names.has(moduleName)) {
|
if (names.has(moduleName)) {
|
||||||
@ -181,6 +209,10 @@ function handleNested(path, t, node, parentExport?) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "TSModuleDeclaration": {
|
case "TSModuleDeclaration": {
|
||||||
|
if (!t.isIdentifier(subNode.declaration.id)) {
|
||||||
|
throw buildNestedAmbiendModuleError(path, subNode.declaration);
|
||||||
|
}
|
||||||
|
|
||||||
const transformed = handleNested(
|
const transformed = handleNested(
|
||||||
path,
|
path,
|
||||||
t,
|
t,
|
||||||
@ -204,7 +236,7 @@ function handleNested(path, t, node, parentExport?) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// {}
|
// {}
|
||||||
let fallthroughValue = t.objectExpression([]);
|
let fallthroughValue: t.Expression = t.objectExpression([]);
|
||||||
|
|
||||||
if (parentExport) {
|
if (parentExport) {
|
||||||
const memberExpr = t.memberExpression(parentExport, realName);
|
const memberExpr = t.memberExpression(parentExport, realName);
|
||||||
|
|||||||
@ -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."
|
||||||
|
}
|
||||||
7
packages/babel-plugin-transform-typescript/test/fixtures/namespace/nested-shorthand/input.ts
vendored
Normal file
7
packages/babel-plugin-transform-typescript/test/fixtures/namespace/nested-shorthand/input.ts
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace X.Y {
|
||||||
|
export const Z = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace proj.data.util.api {
|
||||||
|
export const X = 1;
|
||||||
|
}
|
||||||
27
packages/babel-plugin-transform-typescript/test/fixtures/namespace/nested-shorthand/output.mjs
vendored
Normal file
27
packages/babel-plugin-transform-typescript/test/fixtures/namespace/nested-shorthand/output.mjs
vendored
Normal file
@ -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 = {}));
|
||||||
Loading…
x
Reference in New Issue
Block a user