Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'a> Traverse<'a> for TypeScriptAnnotations<'a, '_> {
if self.only_remove_type_imports {
true
} else {
self.has_value_reference(&id.name, ctx)
self.has_value_reference(id, ctx)
}
});

Expand Down Expand Up @@ -597,27 +597,22 @@ impl<'a> TypeScriptAnnotations<'a, '_> {
}
}

pub fn has_value_reference(&self, name: &str, ctx: &TraverseCtx<'a>) -> bool {
if let Some(symbol_id) = ctx.scoping().get_root_binding(name) {
// `import T from 'mod'; const T = 1;` The T has a value redeclaration
// `import T from 'mod'; type T = number;` The T has a type redeclaration
// If the symbol is still a value symbol after SymbolFlags::Import is removed, then it's a value redeclaration.
// That means the import is shadowed, and we can safely remove the import.
let has_value_redeclaration =
(ctx.scoping().symbol_flags(symbol_id) - SymbolFlags::Import).is_value();
if has_value_redeclaration {
return false;
}
if ctx
.scoping()
.get_resolved_references(symbol_id)
.any(|reference| !reference.is_type())
{
return true;
}
fn has_value_reference(&self, id: &BindingIdentifier<'a>, ctx: &TraverseCtx<'a>) -> bool {
let symbol_id = id.symbol_id();

// `import T from 'mod'; const T = 1;` The T has a value redeclaration
// `import T from 'mod'; type T = number;` The T has a type redeclaration
// If the symbol is still a value symbol after `SymbolFlags::Import` is removed, then it's a value redeclaration.
// That means the import is shadowed, and we can safely remove the import.
if (ctx.scoping().symbol_flags(symbol_id) - SymbolFlags::Import).is_value() {
return false;
}

if ctx.scoping().get_resolved_references(symbol_id).any(|reference| !reference.is_type()) {
return true;
}

self.is_jsx_imports(name)
self.is_jsx_imports(&id.name)
}
}

Expand Down
Loading