Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed May 9, 2023
1 parent 676e937 commit 2a39bd8
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions crates/swc_ecma_transforms_typescript/src/strip_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,7 @@ impl VisitMut for StripType {
n.visit_mut_children_with(self);

let retain = match n {
Stmt::Decl(decl) => match decl {
// https://github.com/tc39/proposal-type-annotations#ambient-declarations
Decl::Class(class) => !class.declare,
// https://github.com/tc39/proposal-type-annotations#function-overloads
Decl::Fn(r#fn) => !r#fn.declare && r#fn.function.body.is_some(),
Decl::Var(var) => !var.declare,
Decl::TsInterface(..) => false,
Decl::TsTypeAlias(..) => false,
Decl::TsEnum(ts_enum) => !ts_enum.declare,
Decl::TsModule(ts_module) => !ts_module.declare,
},
Stmt::Decl(decl) => should_retain_decl(decl),
// Note: we expect that type/interface/declare are `decl`.
_ => true,
};
Expand Down Expand Up @@ -184,15 +174,9 @@ impl VisitMut for StripType {
fn should_retain_module_item(module_item: &ModuleItem) -> bool {
match module_item {
ModuleItem::ModuleDecl(ModuleDecl::Import(import_decl)) => !import_decl.type_only,
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export_decl)) => match &export_decl.decl {
Decl::Class(class) => !class.declare,
Decl::Fn(r#fn) => !r#fn.declare && r#fn.function.body.is_some(),
Decl::Var(var) => !var.declare,
Decl::TsInterface(..) => false,
Decl::TsTypeAlias(..) => false,
Decl::TsEnum(ts_enum) => !ts_enum.declare,
Decl::TsModule(ts_module) => !ts_module.declare,
},
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export_decl)) => {
should_retain_decl(&export_decl.decl)
}
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(named_export)) => !named_export.type_only,
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(export_default_decl)) => {
!export_default_decl.decl.is_ts_interface_decl()
Expand All @@ -211,3 +195,15 @@ fn should_retain_module_item(module_item: &ModuleItem) -> bool {
ModuleItem::Stmt(stmt) => !stmt.is_empty(),
}
}

fn should_retain_decl(decl: &Decl) -> bool {
match decl {
Decl::Class(class) => !class.declare,
Decl::Fn(r#fn) => !r#fn.declare && r#fn.function.body.is_some(),
Decl::Var(var) => !var.declare,
Decl::TsInterface(..) => false,
Decl::TsTypeAlias(..) => false,
Decl::TsEnum(ts_enum) => !ts_enum.declare,
Decl::TsModule(ts_module) => !ts_module.declare,
}
}

0 comments on commit 2a39bd8

Please sign in to comment.