From d60f2ecd1528b279b97a40b37c6c6a5e114bf9a3 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Mon, 19 Sep 2022 18:44:26 +0800 Subject: [PATCH] fix --- .../src/namespace.ts | 21 ++++++++----------- .../test/fixtures/namespace/declare/input.ts | 5 ++++- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/babel-plugin-transform-typescript/src/namespace.ts b/packages/babel-plugin-transform-typescript/src/namespace.ts index a181cdca7448..8c2980452839 100644 --- a/packages/babel-plugin-transform-typescript/src/namespace.ts +++ b/packages/babel-plugin-transform-typescript/src/namespace.ts @@ -81,14 +81,11 @@ function handleVariableDeclaration( // `export const a = 1` transforms to `const a = N.a = 1`, the output // is smaller than `const a = 1; N.a = a`; for (const declarator of declarations) { - // `export declare const a`,the declarator.init would be null, - if (declarator.init) { - declarator.init = t.assignmentExpression( - "=", - getMemberExpression(name, declarator.id.name), - declarator.init, - ); - } + declarator.init = t.assignmentExpression( + "=", + getMemberExpression(name, declarator.id.name), + declarator.init, + ); } return [node]; } @@ -183,15 +180,15 @@ function handleNested( // Export declarations get parsed using the next switch. } + if ("declare" in subNode.declaration && subNode.declaration.declare) { + continue; + } + // Transform the export declarations that occur inside of a namespace. switch (subNode.declaration.type) { case "TSEnumDeclaration": case "FunctionDeclaration": case "ClassDeclaration": { - if (subNode.declaration.declare) { - continue; - } - const itemName = subNode.declaration.id.name; names.add(itemName); namespaceTopLevel.splice( diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts index 8850f9ac5929..e82800b1ef83 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts @@ -4,4 +4,7 @@ export namespace N { export declare enum e { }; export declare function f(): void; export declare const v: unknown; -} \ No newline at end of file + export declare namespace B { + export interface C { } + } +}