Skip to content

Commit

Permalink
fix: declare class in namespace should be removed (#14774)
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe committed Jul 20, 2022
1 parent ea5ff29 commit b58e35b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/babel-plugin-transform-typescript/src/namespace.ts
Expand Up @@ -81,11 +81,14 @@ 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) {
declarator.init = t.assignmentExpression(
"=",
getMemberExpression(name, declarator.id.name),
declarator.init,
);
// `export declare const a`,the declarator.init would be null,
if (declarator.init) {
declarator.init = t.assignmentExpression(
"=",
getMemberExpression(name, declarator.id.name),
declarator.init,
);
}
}
return [node];
}
Expand Down Expand Up @@ -185,6 +188,10 @@ function handleNested(
case "TSEnumDeclaration":
case "FunctionDeclaration":
case "ClassDeclaration": {
if (subNode.declaration.declare) {
continue;
}

const itemName = subNode.declaration.id.name;
names.add(itemName);
namespaceTopLevel.splice(
Expand Down
@@ -0,0 +1,7 @@
export namespace N {
export declare class C {
}
export declare enum e { };
export declare function f(): void;
export declare const v: unknown;
}
@@ -0,0 +1,5 @@
export let N;

(function (_N) {
;
})(N || (N = {}));

0 comments on commit b58e35b

Please sign in to comment.