Skip to content

Commit

Permalink
Extract error logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 10, 2021
1 parent 9a1a74d commit 059df9a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/babel-plugin-transform-typescript/src/namespace.ts
Expand Up @@ -103,6 +103,14 @@ function handleVariableDeclaration(
return [node, t.expressionStatement(t.sequenceExpression(assignments))];
}

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"),
Expand Down Expand Up @@ -131,11 +139,7 @@ function handleNested(
switch (subNode.type) {
case "TSModuleDeclaration": {
if (!t.isIdentifier(subNode.id)) {
throw path.hub.buildError(
subNode,
"Ambient modules cannot be nested in other modules or namespaces.",
Error,
);
throw buildNestedAmbiendModuleError(path, subNode);
}

const transformed = handleNested(path, t, subNode);
Expand Down Expand Up @@ -206,11 +210,7 @@ function handleNested(
}
case "TSModuleDeclaration": {
if (!t.isIdentifier(subNode.declaration.id)) {
throw path.hub.buildError(
subNode,
"Ambient modules cannot be nested in other modules or namespaces.",
Error,
);
throw buildNestedAmbiendModuleError(path, subNode.declaration);
}

const transformed = handleNested(
Expand Down

0 comments on commit 059df9a

Please sign in to comment.