mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
refactor(transformer/typescript): improve implementation of remove import/export (#2530)
This commit is contained in:
parent
d181209759
commit
2c2256a82f
1 changed files with 11 additions and 45 deletions
|
|
@ -108,47 +108,19 @@ impl<'a> TypeScript<'a> {
|
||||||
/// downstream tools that this file is in ESM.
|
/// downstream tools that this file is in ESM.
|
||||||
pub fn transform_program(&mut self, program: &mut Program<'a>) {
|
pub fn transform_program(&mut self, program: &mut Program<'a>) {
|
||||||
let mut export_type_names = FxHashSet::default();
|
let mut export_type_names = FxHashSet::default();
|
||||||
let mut export_names = FxHashSet::default();
|
|
||||||
|
|
||||||
// Collect export names
|
// // Collect export names
|
||||||
program.body.iter().for_each(|stmt| {
|
program.body.iter().for_each(|stmt| {
|
||||||
if let Statement::ModuleDeclaration(module_decl) = stmt {
|
if let Statement::ModuleDeclaration(module_decl) = stmt {
|
||||||
match &**module_decl {
|
if let ModuleDeclaration::ExportNamedDeclaration(decl) = &**module_decl {
|
||||||
ModuleDeclaration::ExportNamedDeclaration(decl) => {
|
decl.specifiers.iter().for_each(|specifier| {
|
||||||
decl.specifiers.iter().for_each(|specifier| {
|
let name = specifier.exported.name();
|
||||||
let name = specifier.exported.name();
|
if self.is_import_binding_only(name)
|
||||||
if self.is_import_binding_only(name) {
|
&& (decl.export_kind.is_type() || specifier.export_kind.is_type())
|
||||||
let is_value =
|
{
|
||||||
decl.export_kind.is_value() && specifier.export_kind.is_value();
|
export_type_names.insert(name.clone());
|
||||||
if is_value {
|
|
||||||
export_names.insert(name.clone());
|
|
||||||
} else {
|
|
||||||
export_type_names.insert(name.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
|
|
||||||
let name = decl.exported.name();
|
|
||||||
if self.is_import_binding_only(name) {
|
|
||||||
export_names.insert(decl.exported.name().clone());
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
ModuleDeclaration::ExportAllDeclaration(decl) => {
|
|
||||||
if let Some(exported) = &decl.exported {
|
|
||||||
let name = exported.name();
|
|
||||||
if self.is_import_binding_only(name) {
|
|
||||||
let is_value =
|
|
||||||
decl.export_kind.is_value() && decl.export_kind.is_value();
|
|
||||||
if is_value {
|
|
||||||
export_names.insert(name.clone());
|
|
||||||
} else {
|
|
||||||
export_type_names.insert(name.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -209,7 +181,6 @@ impl<'a> TypeScript<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.has_value_references(&s.local.name)
|
self.has_value_references(&s.local.name)
|
||||||
|| export_names.contains(&s.local.name)
|
|
||||||
}
|
}
|
||||||
ImportDeclarationSpecifier::ImportDefaultSpecifier(s)
|
ImportDeclarationSpecifier::ImportDefaultSpecifier(s)
|
||||||
if !self.verbatim_module_syntax =>
|
if !self.verbatim_module_syntax =>
|
||||||
|
|
@ -222,9 +193,7 @@ impl<'a> TypeScript<'a> {
|
||||||
if self.options.only_remove_type_imports {
|
if self.options.only_remove_type_imports {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.has_value_references(&s.local.name)
|
self.has_value_references(&s.local.name)
|
||||||
|| export_names.contains(&s.local.name)
|
|
||||||
}
|
}
|
||||||
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s)
|
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s)
|
||||||
if !self.verbatim_module_syntax =>
|
if !self.verbatim_module_syntax =>
|
||||||
|
|
@ -237,10 +206,6 @@ impl<'a> TypeScript<'a> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if export_names.contains(&s.local.name) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.has_value_references(&s.local.name)
|
self.has_value_references(&s.local.name)
|
||||||
}
|
}
|
||||||
_ => true,
|
_ => true,
|
||||||
|
|
@ -308,7 +273,8 @@ impl<'a> TypeScript<'a> {
|
||||||
.scopes()
|
.scopes()
|
||||||
.get_binding(root_scope_id, name)
|
.get_binding(root_scope_id, name)
|
||||||
.map(|symbol_id| {
|
.map(|symbol_id| {
|
||||||
self.ctx.symbols().get_resolved_references(symbol_id).any(|x| !x.is_type())
|
self.ctx.symbols().get_flag(symbol_id).is_export()
|
||||||
|
|| self.ctx.symbols().get_resolved_references(symbol_id).any(|x| !x.is_type())
|
||||||
})
|
})
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue