Skip to content

Commit

Permalink
fix(transformer/typescript): should strip import specifiers type with…
Browse files Browse the repository at this point in the history
… `only_remove_type_imports` (#8141)

close: #8140
close: rolldown/rolldown#3244

---------

Co-authored-by: Dunqing <[email protected]>
  • Loading branch information
underfin and Dunqing authored Jan 6, 2025
1 parent f87da16 commit 2e7207f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 21 additions & 4 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptAnnotations<'a, 'ctx> {
Statement::ImportDeclaration(decl) => {
if decl.import_kind.is_type() {
false
} else if self.only_remove_type_imports {
true
} else if let Some(specifiers) = &mut decl.specifiers {
if specifiers.is_empty() {
// import {} from 'mod' -> import 'mod'
Expand All @@ -118,9 +116,28 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptAnnotations<'a, 'ctx> {
&s.local
}
};
self.has_value_reference(&id.name, ctx)
// If `only_remove_type_imports` is true, then we can return `true` to keep it because
// it is not a type import, otherwise we need to check if the identifier is referenced
if self.only_remove_type_imports {
true
} else {
self.has_value_reference(&id.name, ctx)
}
});
!specifiers.is_empty()

if specifiers.is_empty() {
// `import { type A } from 'mod'`
if self.only_remove_type_imports {
// -> `import 'mod'`
decl.specifiers = None;
true
} else {
// Remove the import declaration if all specifiers are removed
false
}
} else {
true
}
}
} else {
true
Expand Down
8 changes: 6 additions & 2 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -2173,7 +2173,9 @@ after transform: []
rebuilt : ["require"]

* imports/only-remove-type-imports/input.ts
x Output mismatch
Bindings mismatch:
after transform: ScopeId(0): ["H", "I", "I2", "J", "K1", "K2", "L1", "L2", "L3", "a", "b", "c2", "d", "d2", "e", "e4"]
rebuilt : ScopeId(0): ["L2", "a", "b", "c2", "d", "d2", "e", "e4"]

* imports/property-signature/input.ts
Bindings mismatch:
Expand Down Expand Up @@ -2204,7 +2206,9 @@ after transform: ScopeId(0): ["Foo1", "Foo2"]
rebuilt : ScopeId(0): []

* imports/type-only-import-specifier-4/input.ts
x Output mismatch
Bindings mismatch:
after transform: ScopeId(0): ["A"]
rebuilt : ScopeId(0): []

* lvalues/TSTypeParameterInstantiation/input.ts
Symbol reference IDs mismatch for "AbstractClass":
Expand Down

0 comments on commit 2e7207f

Please sign in to comment.