From 059df9ae24fda7915af2564918083aa1f8e6607e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 10 Aug 2021 21:56:43 +0200 Subject: [PATCH] Extract error logic --- .../src/namespace.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/babel-plugin-transform-typescript/src/namespace.ts b/packages/babel-plugin-transform-typescript/src/namespace.ts index c0851f473c60..186da7c3c77c 100644 --- a/packages/babel-plugin-transform-typescript/src/namespace.ts +++ b/packages/babel-plugin-transform-typescript/src/namespace.ts @@ -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"), @@ -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); @@ -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(