diff --git a/crates/codescythe/analyze.rs b/crates/codescythe/analyze.rs index 7be5f9b..c792dfb 100644 --- a/crates/codescythe/analyze.rs +++ b/crates/codescythe/analyze.rs @@ -381,51 +381,44 @@ pub fn analyze_path( let file = files.get(index)?.clone(); let public_entry = entry_indexes.contains(&index) && !config.include_entry_exports; + let mut static_imports_by_source = BTreeMap::<&str, BTreeSet>>::new(); for import in &file.imports { - match module_resolver.resolve(&file, &import.source)? { - ImportResolution::Project(target) => { - if let Some(name) = &import.imported { - mark_used_export( - target, - name.clone(), - &mut used_files, - &mut used_exports, - &mut queue, - &mut queued_files, - &test_file_indexes, - ); - } else { - mark_used_file( - target, - &test_file_indexes, - &mut used_files, - &mut queue, - &mut queued_files, - ); - } - } - ImportResolution::Unresolved => { - unresolved_policy.record( - &mut unresolved, - &mut ignored_unresolved_imports_by_pattern, - &file.relative, - &import.source, - )?; - } - ImportResolution::External => {} - } + static_imports_by_source + .entry(import.source.as_str()) + .or_default() + .insert(import.imported.as_deref()); } - for source in &file.side_effect_imports { + static_imports_by_source + .entry(source.as_str()) + .or_default() + .insert(None); + } + + for (source, imported_names) in static_imports_by_source { match module_resolver.resolve(&file, source)? { ImportResolution::Project(target) => { - mark_used_file( - target, - &test_file_indexes, - &mut used_files, - &mut queue, - &mut queued_files, - ); + for imported in imported_names { + if let Some(name) = imported { + mark_used_export( + target, + name.to_string(), + &mut used_files, + &mut used_exports, + &mut queue, + &mut queued_files, + &test_file_indexes, + ); + } else { + mark_used_file( + target, + &test_file_indexes, + &mut used_files, + &mut queue, + &mut queued_files, + ); + } + } } ImportResolution::Unresolved => { unresolved_policy.record( diff --git a/crates/codescythe/analyze/parse.rs b/crates/codescythe/analyze/parse.rs index 1c89923..49efb5b 100644 --- a/crates/codescythe/analyze/parse.rs +++ b/crates/codescythe/analyze/parse.rs @@ -196,7 +196,7 @@ pub(super) struct ExportInfo { pub(super) namespace_source: Option, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)] pub(super) struct ImportRecord { pub(super) source: String, pub(super) imported: Option, @@ -241,7 +241,14 @@ impl FileVisitor { } } - fn finish(self) -> FileData { + fn finish(mut self) -> FileData { + dedupe_preserving_order(&mut self.imports); + dedupe_preserving_order(&mut self.side_effect_imports); + dedupe_preserving_order(&mut self.dynamic_imports); + dedupe_preserving_order(&mut self.glob_imports); + dedupe_preserving_order(&mut self.member_uses); + dedupe_preserving_order(&mut self.reexport_all); + FileData { path: self.path, relative: self.relative, @@ -486,6 +493,14 @@ impl<'a> Visit<'a> for FileVisitor { } } +fn dedupe_preserving_order(items: &mut Vec) +where + T: Clone + Ord, +{ + let mut seen = BTreeSet::new(); + items.retain(|item| seen.insert(item.clone())); +} + impl FileVisitor { fn add_declaration_exports( &mut self,