fix: remove imported types from export (#13800)
* fix: remove imported types from export * add onlyRemoveTypeImports testcase
This commit is contained in:
parent
767b72f9c2
commit
89cab4311c
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@ -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
|
||||||
5
packages/babel-plugin-transform-typescript/test/fixtures/exports/imported-types/input.ts
vendored
Normal file
5
packages/babel-plugin-transform-typescript/test/fixtures/exports/imported-types/input.ts
vendored
Normal 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
|
||||||
2
packages/babel-plugin-transform-typescript/test/fixtures/exports/imported-types/output.mjs
vendored
Normal file
2
packages/babel-plugin-transform-typescript/test/fixtures/exports/imported-types/output.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
import C from "C";
|
||||||
|
export { C }; // <-- A and B will be removed
|
||||||
Loading…
x
Reference in New Issue
Block a user