fix: remove imported types from export (#13800)

* fix: remove imported types from export

* add onlyRemoveTypeImports testcase
This commit is contained in:
Huáng Jùnliàng 2021-09-28 15:13:37 -04:00 committed by GitHub
parent 767b72f9c2
commit 89cab4311c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 6 deletions

View File

@ -47,8 +47,8 @@ function isGlobalType(path, name) {
return false; return false;
} }
function registerGlobalType(programScope, name) { function registerGlobalType(programNode, name) {
GLOBAL_TYPES.get(programScope.path.node).add(name); GLOBAL_TYPES.get(programNode).add(name);
} }
export default declare((api, opts) => { export default declare((api, opts) => {
@ -188,9 +188,10 @@ export default declare((api, opts) => {
const { file } = state; const { file } = state;
let fileJsxPragma = null; let fileJsxPragma = null;
let fileJsxPragmaFrag = null; let fileJsxPragmaFrag = null;
const programNode = path.node;
if (!GLOBAL_TYPES.has(path.node)) { if (!GLOBAL_TYPES.has(programNode)) {
GLOBAL_TYPES.set(path.node, new Set()); GLOBAL_TYPES.set(programNode, new Set());
} }
if (file.ast.comments) { if (file.ast.comments) {
@ -225,6 +226,9 @@ export default declare((api, opts) => {
} }
if (stmt.node.importKind === "type") { if (stmt.node.importKind === "type") {
for (const specifier of stmt.node.specifiers) {
registerGlobalType(programNode, specifier.local.name);
}
stmt.remove(); stmt.remove();
continue; continue;
} }
@ -287,7 +291,7 @@ export default declare((api, opts) => {
if (stmt.isVariableDeclaration({ declare: true })) { if (stmt.isVariableDeclaration({ declare: true })) {
for (const name of Object.keys(stmt.getBindingIdentifiers())) { for (const name of Object.keys(stmt.getBindingIdentifiers())) {
registerGlobalType(path.scope, name); registerGlobalType(programNode, name);
} }
} else if ( } else if (
stmt.isTSTypeAliasDeclaration() || stmt.isTSTypeAliasDeclaration() ||
@ -298,7 +302,7 @@ export default declare((api, opts) => {
(stmt.isTSModuleDeclaration({ declare: true }) && (stmt.isTSModuleDeclaration({ declare: true }) &&
stmt.get("id").isIdentifier()) stmt.get("id").isIdentifier())
) { ) {
registerGlobalType(path.scope, stmt.node.id.name); registerGlobalType(programNode, stmt.node.id.name);
} }
} }
}, },

View File

@ -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

View File

@ -0,0 +1,10 @@
{
"plugins": [
[
"transform-typescript",
{
"onlyRemoveTypeImports": true
}
]
]
}

View File

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

View File

@ -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

View File

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