Skip to content

Commit

Permalink
remove usage analysis for script
Browse files Browse the repository at this point in the history
  • Loading branch information
CPunisher committed Nov 23, 2024
1 parent bf181bb commit bf3c9b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
24 changes: 13 additions & 11 deletions crates/swc_typescript/src/fast_dts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ impl FastDts {

impl FastDts {
pub fn transform(&mut self, program: &mut Program) -> Vec<DtsIssue> {
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),
Expand All @@ -94,11 +90,17 @@ impl FastDts {
items: &mut Vec<ModuleItem>,
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() {
Expand All @@ -115,18 +117,18 @@ 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()
.map(|stmt| stmt.is_decl() && !self.has_internal_annotation(stmt.span_lo()))
.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;
Expand Down Expand Up @@ -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) => {
Expand Down
22 changes: 3 additions & 19 deletions crates/swc_typescript/src/fast_dts/visitors/type_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -23,7 +23,7 @@ pub struct TypeUsageAnalyzer<'a> {

impl TypeUsageAnalyzer<'_> {
pub fn analyze(
program: &Program,
module_items: &Vec<ModuleItem>,
internal_annotations: Option<&FxHashSet<BytePos>>,
) -> FxHashSet<Id> {
// Create a fake entry
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit bf3c9b6

Please sign in to comment.