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

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

Merged
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
19 changes: 15 additions & 4 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub struct TypeScript<'a> {
ast: Rc<AstBuilder<'a>>,
ctx: TransformerCtx<'a>,
verbatim_module_syntax: bool,

/// type imports names
import_type_name_set: FxHashSet<Atom>,
export_name_set: FxHashSet<Atom>,
}

Expand All @@ -30,7 +31,13 @@ impl<'a> TypeScript<'a> {
ctx: TransformerCtx<'a>,
verbatim_module_syntax: bool,
) -> 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>) {
Expand Down Expand Up @@ -101,21 +108,25 @@ impl<'a> TypeScript<'a> {
/// * Remove the top level import / export statements that are types
/// * Adds `export {}` if all import / export statements are removed, this is used to tell
/// 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;

for stmt in program.body.iter_mut() {
if let Statement::ModuleDeclaration(module_decl) = stmt {
needs_explicit_esm = true;
match &mut **module_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() => {
if let Some(specifiers) = &mut decl.specifiers {
specifiers.retain(|specifier| match specifier {
ImportDeclarationSpecifier::ImportSpecifier(s) => {
if s.import_kind.is_type() {
self.import_type_name_set.insert(s.local.name.clone());
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Passed: 304/1179
Passed: 306/1179

# All Passed:
* babel-plugin-transform-numeric-separator
Expand Down Expand Up @@ -832,7 +832,7 @@ Passed: 304/1179
* general/function-duplicate-name/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-method/input.ts
* class/abstract-class-decorated-parameter/input.ts
Expand Down Expand Up @@ -872,9 +872,7 @@ Passed: 304/1179
* imports/import=-module-to-cjs/input.ts
* imports/only-remove-type-imports/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-import-specifier-3/input.ts
* imports/type-only-import-specifier-4/input.ts
* namespace/ambient-module-nested/input.ts
* namespace/ambient-module-nested-exported/input.ts
Expand Down