From bf3c9b65a8fe8e17338c3d21d695909d9eeb175e Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Sat, 23 Nov 2024 13:51:28 +0800 Subject: [PATCH] remove usage analysis for script --- crates/swc_typescript/src/fast_dts/mod.rs | 24 ++++++++++--------- .../src/fast_dts/visitors/type_usage.rs | 22 +++-------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/crates/swc_typescript/src/fast_dts/mod.rs b/crates/swc_typescript/src/fast_dts/mod.rs index 984fe2040b3e..c2049ade8aab 100644 --- a/crates/swc_typescript/src/fast_dts/mod.rs +++ b/crates/swc_typescript/src/fast_dts/mod.rs @@ -78,10 +78,6 @@ impl FastDts { impl FastDts { pub fn transform(&mut self, program: &mut Program) -> Vec { - self.used_ids.extend(TypeUsageAnalyzer::analyze( - program, - self.internal_annotations.as_ref(), - )); match program { Program::Module(module) => self.transform_module_body(&mut module.body, false), Program::Script(script) => self.transform_script(script), @@ -94,11 +90,17 @@ impl FastDts { items: &mut Vec, in_global_or_lit_module: bool, ) { - // 1. Transform. + // 1. Analyze usage + self.used_ids.extend(TypeUsageAnalyzer::analyze( + items, + self.internal_annotations.as_ref(), + )); + + // 2. Transform. Self::remove_function_overloads_in_module(items); self.transform_module_items(items); - // 2. Strip export keywords in ts module blocks + // 3. Strip export keywords in ts module blocks for item in items.iter_mut() { if let Some(Stmt::Decl(Decl::TsModule(ts_module))) = item.as_mut_stmt() { if ts_module.global || !ts_module.id.is_str() { @@ -115,7 +117,7 @@ impl FastDts { } } - // 3. Report error for expando function and remove statements. + // 4. Report error for expando function and remove statements. self.report_error_for_expando_function_in_module(items); items.retain(|item| { item.as_stmt() @@ -123,10 +125,10 @@ impl FastDts { .unwrap_or(true) }); - // 4. Remove unused imports and decls + // 5. Remove unused imports and decls self.remove_ununsed(items, in_global_or_lit_module); - // 5. Add empty export mark if there's any declaration that is used but not + // 6. Add empty export mark if there's any declaration that is used but not // exported to keep its privacy. let mut has_non_exported_stmt = false; let mut has_export = false; @@ -416,8 +418,8 @@ impl FastDts { for stmt in stmts { match stmt { Stmt::Decl(decl) => match decl { - Decl::Fn(fn_decl) => collector.add_fn_decl(fn_decl, true), - Decl::Var(var_decl) => collector.add_var_decl(var_decl, true), + Decl::Fn(fn_decl) => collector.add_fn_decl(fn_decl, false), + Decl::Var(var_decl) => collector.add_var_decl(var_decl, false), _ => (), }, Stmt::Expr(expr_stmt) => { diff --git a/crates/swc_typescript/src/fast_dts/visitors/type_usage.rs b/crates/swc_typescript/src/fast_dts/visitors/type_usage.rs index 6bc4a30f012c..96b562bf8639 100644 --- a/crates/swc_typescript/src/fast_dts/visitors/type_usage.rs +++ b/crates/swc_typescript/src/fast_dts/visitors/type_usage.rs @@ -7,7 +7,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use swc_common::{BytePos, Spanned, SyntaxContext}; use swc_ecma_ast::{ Class, Decl, ExportDecl, ExportDefaultDecl, ExportDefaultExpr, Function, Id, Ident, - ModuleExportName, ModuleItem, NamedExport, Program, Script, TsEntityName, TsExportAssignment, + ModuleExportName, ModuleItem, NamedExport, TsEntityName, TsExportAssignment, TsExprWithTypeArgs, }; use swc_ecma_visit::{Visit, VisitWith}; @@ -23,7 +23,7 @@ pub struct TypeUsageAnalyzer<'a> { impl TypeUsageAnalyzer<'_> { pub fn analyze( - program: &Program, + module_items: &Vec, internal_annotations: Option<&FxHashSet>, ) -> FxHashSet { // Create a fake entry @@ -37,7 +37,7 @@ impl TypeUsageAnalyzer<'_> { source: None, internal_annotations, }; - program.visit_with(&mut analyzer); + module_items.visit_with(&mut analyzer); // Reachability let mut used_ids = FxHashSet::default(); @@ -238,20 +238,4 @@ impl Visit for TypeUsageAnalyzer<'_> { item.visit_children_with(self); } } - - fn visit_script(&mut self, node: &Script) { - for stmt in &node.body { - // Skip statements - if !stmt.is_decl() - || self - .internal_annotations - .map_or(false, |internal_annotations| { - internal_annotations.contains(&stmt.span_lo()) - }) - { - continue; - } - stmt.visit_children_with(self); - } - } }