Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TS declare class in namespace should be removed #14774

Merged
merged 3 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = {}));