feat(transformer/typescript): remove export specifier that import_kind is type (#2015)

This commit is contained in:
Dunqing 2024-01-13 21:25:59 +08:00 committed by GitHub
parent 869643b03b
commit 6c7f983de5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View file

@ -20,7 +20,8 @@ pub struct TypeScript<'a> {
ast: Rc<AstBuilder<'a>>, ast: Rc<AstBuilder<'a>>,
ctx: TransformerCtx<'a>, ctx: TransformerCtx<'a>,
verbatim_module_syntax: bool, verbatim_module_syntax: bool,
/// type imports names
import_type_name_set: FxHashSet<Atom>,
export_name_set: FxHashSet<Atom>, export_name_set: FxHashSet<Atom>,
} }
@ -30,7 +31,13 @@ impl<'a> TypeScript<'a> {
ctx: TransformerCtx<'a>, ctx: TransformerCtx<'a>,
verbatim_module_syntax: bool, verbatim_module_syntax: bool,
) -> Self { ) -> Self {
Self { ast, ctx, verbatim_module_syntax, export_name_set: FxHashSet::default() } Self {
ast,
ctx,
verbatim_module_syntax,
import_type_name_set: FxHashSet::default(),
export_name_set: FxHashSet::default(),
}
} }
pub fn transform_declaration(&mut self, decl: &mut Declaration<'a>) { pub fn transform_declaration(&mut self, decl: &mut Declaration<'a>) {
@ -101,7 +108,7 @@ impl<'a> TypeScript<'a> {
/// * Remove the top level import / export statements that are types /// * Remove the top level import / export statements that are types
/// * Adds `export {}` if all import / export statements are removed, this is used to tell /// * Adds `export {}` if all import / export statements are removed, this is used to tell
/// downstream tools that this file is in ESM. /// downstream tools that this file is in ESM.
pub fn transform_program(&self, program: &mut Program<'a>) { pub fn transform_program(&mut self, program: &mut Program<'a>) {
let mut needs_explicit_esm = false; let mut needs_explicit_esm = false;
for stmt in program.body.iter_mut() { for stmt in program.body.iter_mut() {
@ -109,13 +116,17 @@ impl<'a> TypeScript<'a> {
needs_explicit_esm = true; needs_explicit_esm = true;
match &mut **module_decl { match &mut **module_decl {
ModuleDeclaration::ExportNamedDeclaration(decl) => { ModuleDeclaration::ExportNamedDeclaration(decl) => {
decl.specifiers.retain(|specifier| specifier.export_kind.is_value()); decl.specifiers.retain(|specifier| {
!(specifier.export_kind.is_type()
|| self.import_type_name_set.contains(specifier.exported.name()))
});
} }
ModuleDeclaration::ImportDeclaration(decl) if decl.import_kind.is_value() => { ModuleDeclaration::ImportDeclaration(decl) if decl.import_kind.is_value() => {
if let Some(specifiers) = &mut decl.specifiers { if let Some(specifiers) = &mut decl.specifiers {
specifiers.retain(|specifier| match specifier { specifiers.retain(|specifier| match specifier {
ImportDeclarationSpecifier::ImportSpecifier(s) => { ImportDeclarationSpecifier::ImportSpecifier(s) => {
if s.import_kind.is_type() { if s.import_kind.is_type() {
self.import_type_name_set.insert(s.local.name.clone());
return false; return false;
} }

View file

@ -1,4 +1,4 @@
Passed: 304/1179 Passed: 306/1179
# All Passed: # All Passed:
* babel-plugin-transform-numeric-separator * babel-plugin-transform-numeric-separator
@ -832,7 +832,7 @@ Passed: 304/1179
* general/function-duplicate-name/input.js * general/function-duplicate-name/input.js
* general/object/input.js * general/object/input.js
# babel-plugin-transform-typescript (73/158) # babel-plugin-transform-typescript (75/158)
* class/abstract-class-decorated/input.ts * class/abstract-class-decorated/input.ts
* class/abstract-class-decorated-method/input.ts * class/abstract-class-decorated-method/input.ts
* class/abstract-class-decorated-parameter/input.ts * class/abstract-class-decorated-parameter/input.ts
@ -872,9 +872,7 @@ Passed: 304/1179
* imports/import=-module-to-cjs/input.ts * imports/import=-module-to-cjs/input.ts
* imports/only-remove-type-imports/input.ts * imports/only-remove-type-imports/input.ts
* imports/parameter-decorators/input.ts * imports/parameter-decorators/input.ts
* imports/type-only-export-specifier-1/input.ts
* imports/type-only-export-specifier-2/input.ts * imports/type-only-export-specifier-2/input.ts
* imports/type-only-import-specifier-3/input.ts
* imports/type-only-import-specifier-4/input.ts * imports/type-only-import-specifier-4/input.ts
* namespace/ambient-module-nested/input.ts * namespace/ambient-module-nested/input.ts
* namespace/ambient-module-nested-exported/input.ts * namespace/ambient-module-nested-exported/input.ts