Skip to content

Commit

Permalink
refactor(transformer/typescript): remove unnecessary code (#4321)
Browse files Browse the repository at this point in the history
close: #3827
  • Loading branch information
Dunqing committed Jul 17, 2024
1 parent 95e15b6 commit a197e01
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 59 deletions.
17 changes: 0 additions & 17 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,6 @@ impl<'a> Traverse<'a> for Transformer<'a> {
fn enter_class_body(&mut self, body: &mut ClassBody<'a>, _ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.transform_class_body(body);
}

fn enter_import_declaration(
&mut self,
decl: &mut ImportDeclaration<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.x0_typescript.transform_import_declaration(decl);
}

fn enter_export_named_declaration(
&mut self,
decl: &mut ExportNamedDeclaration<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.x0_typescript.transform_export_named_declaration(decl);
}

fn enter_ts_module_declaration(
&mut self,
decl: &mut TSModuleDeclaration<'a>,
Expand Down
51 changes: 17 additions & 34 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,23 @@ impl<'a> TypeScriptAnnotations<'a> {
false
} else {
decl.specifiers.retain(|specifier| {
!specifier.export_kind.is_type()
&& !self.type_identifier_names.contains(&specifier.exported.name())
!(specifier.export_kind.is_type()
|| self.type_identifier_names.contains(&specifier.exported.name())
|| {
if let ModuleExportName::IdentifierReference(ident) =
&specifier.local
{
ident.reference_id.get().is_some_and(|id| {
ctx.symbols().references[id].symbol_id().is_some_and(
|symbol_id| {
ctx.symbols().get_flag(symbol_id).is_type()
},
)
})
} else {
false
}
})
});

!decl.specifiers.is_empty()
Expand Down Expand Up @@ -476,38 +491,6 @@ impl<'a> TypeScriptAnnotations<'a> {
self.has_jsx_fragment = true;
}

pub fn transform_import_declaration(&mut self, decl: &mut ImportDeclaration<'a>) {
let Some(specifiers) = &decl.specifiers else {
return;
};
let is_type = decl.import_kind.is_type();
for specifier in specifiers {
let mut specifier_is_type = is_type;
let id = match specifier {
ImportDeclarationSpecifier::ImportSpecifier(s) => {
if s.import_kind.is_type() {
specifier_is_type = true;
}
&s.local
}
ImportDeclarationSpecifier::ImportDefaultSpecifier(s) => &s.local,
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s) => &s.local,
};
if specifier_is_type {
self.type_identifier_names.insert(id.name.clone());
}
}
}

pub fn transform_export_named_declaration(&mut self, decl: &mut ExportNamedDeclaration<'a>) {
let is_type = decl.export_kind.is_type();
for specifier in &decl.specifiers {
if is_type || specifier.export_kind.is_type() {
self.type_identifier_names.insert(specifier.local.name().clone());
}
}
}

pub fn transform_ts_module_declaration(&mut self, decl: &mut TSModuleDeclaration<'a>) {
// NB: Namespace transform happens in `enter_program` visitor, and replaces retained
// namespaces with functions. This visitor is called after, by which time any remaining
Expand Down
8 changes: 0 additions & 8 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ impl<'a> TypeScript<'a> {
self.annotations.transform_class_body(body);
}

pub fn transform_import_declaration(&mut self, decl: &mut ImportDeclaration<'a>) {
self.annotations.transform_import_declaration(decl);
}

pub fn transform_export_named_declaration(&mut self, decl: &mut ExportNamedDeclaration<'a>) {
self.annotations.transform_export_named_declaration(decl);
}

pub fn transform_ts_module_declaration(&mut self, decl: &mut TSModuleDeclaration<'a>) {
self.annotations.transform_ts_module_declaration(decl);
}
Expand Down

0 comments on commit a197e01

Please sign in to comment.