From 9e67898cb91bc6cffd820dccf0268f2ac8d3fb0f Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 14 Mar 2023 21:28:29 +0800 Subject: [PATCH] fix: Handle `export { type foo }` as inType (#15494) --- packages/babel-plugin-transform-typescript/src/index.ts | 5 ++++- .../imports/type-only-export-specifier-1/input.ts | 4 ++++ .../imports/type-only-export-specifier-1/output.mjs | 1 + .../imports/type-only-export-specifier-2/input.ts | 4 ++++ .../imports/type-only-export-specifier-2/options.json | 8 ++++++++ .../imports/type-only-export-specifier-2/output.mjs | 2 ++ 6 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/input.ts create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/output.mjs create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/input.ts create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/output.mjs diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index 1aa42d08e00f..1ef9ebcbcf24 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -24,9 +24,12 @@ function isInType(path: NodePath) { ); case "ExportSpecifier": return ( + // export { type foo }; + path.parent.exportKind === "type" || + // export type { foo }; // @ts-expect-error: DeclareExportDeclaration does not have `exportKind` (path.parentPath as NodePath).parent.exportKind === - "type" + "type" ); default: return false; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/input.ts new file mode 100644 index 000000000000..56dfd0ba142f --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/input.ts @@ -0,0 +1,4 @@ +import { foo, bar, type baz } from "Foo"; +export { type foo }; +export type { bar }; +export { baz }; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/output.mjs new file mode 100644 index 000000000000..cb0ff5c3b541 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-1/output.mjs @@ -0,0 +1 @@ +export {}; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/input.ts new file mode 100644 index 000000000000..56dfd0ba142f --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/input.ts @@ -0,0 +1,4 @@ +import { foo, bar, type baz } from "Foo"; +export { type foo }; +export type { bar }; +export { baz }; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/options.json new file mode 100644 index 000000000000..ce280969f6e9 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [[ + "transform-typescript", + { + "onlyRemoveTypeImports": true + } + ]] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/output.mjs new file mode 100644 index 000000000000..f112d980d494 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/type-only-export-specifier-2/output.mjs @@ -0,0 +1,2 @@ +import { foo, bar } from "Foo"; +export {};