Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transformer-typescript): should strip import specifiers type with only_remove_type_imports #8141

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 21 additions & 3 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashSet;

use oxc_allocator::Vec as ArenaVec;
use oxc_allocator::{CloneIn, Vec as ArenaVec};
use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::SymbolFlags;
Expand Down Expand Up @@ -95,31 +95,49 @@ 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'
decl.specifiers = None;
true
} else {
let mut all_specifiers_is_type = true;
specifiers.retain(|specifier| {
let id = match specifier {
ImportDeclarationSpecifier::ImportSpecifier(s) => {
if s.import_kind.is_type() {
return false;
}
all_specifiers_is_type = false;
&s.local
}
ImportDeclarationSpecifier::ImportDefaultSpecifier(s) => {
all_specifiers_is_type = false;
&s.local
}
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s) => {
all_specifiers_is_type = false;
&s.local
}
};
// Should preserve `import x from 'x'`(x is unused) if only_remove_type_imports enable
if self.only_remove_type_imports {
return true;
}
self.has_value_reference(&id.name, ctx)
});
// Should transform `import { type x } from 'x'`` to `import 'xx'` if only_remove_type_imports enable
if all_specifiers_is_type && self.only_remove_type_imports {
*decl = ctx.ast.alloc_import_declaration(
decl.span,
None,
decl.source.clone(),
None,
decl.with_clause.clone_in(ctx.ast.allocator),
decl.import_kind,
);
return true;
}
!specifiers.is_empty()
}
} else {
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 @@ -1691,7 +1691,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 @@ -1722,7 +1724,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
Loading