Skip to content

Commit

Permalink
fix: remove imported types from export (#13800)
Browse files Browse the repository at this point in the history
* fix: remove imported types from export

* add onlyRemoveTypeImports testcase
  • Loading branch information
JLHwung committed Sep 28, 2021
1 parent 767b72f commit 89cab43
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/babel-plugin-transform-typescript/src/index.ts
Expand Up @@ -47,8 +47,8 @@ function isGlobalType(path, name) {
return false;
}

function registerGlobalType(programScope, name) {
GLOBAL_TYPES.get(programScope.path.node).add(name);
function registerGlobalType(programNode, name) {
GLOBAL_TYPES.get(programNode).add(name);
}

export default declare((api, opts) => {
Expand Down Expand Up @@ -188,9 +188,10 @@ export default declare((api, opts) => {
const { file } = state;
let fileJsxPragma = null;
let fileJsxPragmaFrag = null;
const programNode = path.node;

if (!GLOBAL_TYPES.has(path.node)) {
GLOBAL_TYPES.set(path.node, new Set());
if (!GLOBAL_TYPES.has(programNode)) {
GLOBAL_TYPES.set(programNode, new Set());
}

if (file.ast.comments) {
Expand Down Expand Up @@ -225,6 +226,9 @@ export default declare((api, opts) => {
}

if (stmt.node.importKind === "type") {
for (const specifier of stmt.node.specifiers) {
registerGlobalType(programNode, specifier.local.name);
}
stmt.remove();
continue;
}
Expand Down Expand Up @@ -287,7 +291,7 @@ export default declare((api, opts) => {

if (stmt.isVariableDeclaration({ declare: true })) {
for (const name of Object.keys(stmt.getBindingIdentifiers())) {
registerGlobalType(path.scope, name);
registerGlobalType(programNode, name);
}
} else if (
stmt.isTSTypeAliasDeclaration() ||
Expand All @@ -298,7 +302,7 @@ export default declare((api, opts) => {
(stmt.isTSModuleDeclaration({ declare: true }) &&
stmt.get("id").isIdentifier())
) {
registerGlobalType(path.scope, stmt.node.id.name);
registerGlobalType(programNode, stmt.node.id.name);
}
}
},
Expand Down
@@ -0,0 +1,5 @@
import type A from "A";
import type { B } from "B";
import C from "C";

export { A, B, C } // <-- A and B will be removed
@@ -0,0 +1,10 @@
{
"plugins": [
[
"transform-typescript",
{
"onlyRemoveTypeImports": true
}
]
]
}
@@ -0,0 +1,2 @@
import C from "C";
export { C }; // <-- A and B will be removed
@@ -0,0 +1,5 @@
import type A from "A";
import type { B } from "B";
import C from "C";

export { A, B, C } // <-- A and B will be removed
@@ -0,0 +1,2 @@
import C from "C";
export { C }; // <-- A and B will be removed

0 comments on commit 89cab43

Please sign in to comment.