Skip to content

Commit

Permalink
unused_finder: ensure tagging passes don't overwrite each other (#114)
Browse files Browse the repository at this point in the history
Ensures that multiple tagging passes over a graph will not overwrite
previous tagged values
  • Loading branch information
Adjective-Object authored Dec 11, 2024
1 parent cf7bcb7 commit 5c363c5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "unused_finder: ensure tagging passes don't overwrite each other",
"packageName": "@good-fences/api",
"email": "[email protected]",
"dependentChangeType": "patch"
}
49 changes: 45 additions & 4 deletions crates/unused_finder/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ impl GraphFile {
}
}

// tags an individual named or default tag on this symbol's symbol map
fn tag_named_or_default_symbol(
symbol_tags: &mut AHashMap<ExportedSymbol, UsedTag>,
symbol: &ExportedSymbol,
tag: UsedTag,
) {
let tags = symbol_tags.get(symbol).copied().unwrap_or_default();
if !tags.contains(tag) {
symbol_tags.insert(symbol.clone(), tag);
}
let old_tag = symbol_tags.entry(symbol.clone()).or_default();
*old_tag = old_tag.union(tag);
}

// A 1-way representation of an edge in the import graph
Expand Down Expand Up @@ -140,6 +139,12 @@ impl Graph {
file.tag_symbol(symbol, tag);
}

pub fn get_symbol_tags(&self, path: &Path, symbol: &ExportedSymbol) -> Option<&UsedTag> {
let file_id = self.path_to_id.get(path)?;
let file = &self.files[*file_id];
file.symbol_tags.get(symbol).clone()
}

pub fn traverse_bfs(
&mut self,
logger: impl Logger,
Expand Down Expand Up @@ -327,3 +332,39 @@ impl Graph {
next_frontier_symbols.into_iter().collect()
}
}

#[cfg(test)]
mod test {
use test_tmpdir::amap2;

use super::*;
#[test]
fn test_tag_union() {
let path = PathBuf::from("/test/a.ts");
let a_sym = ExportedSymbol::Named("a".to_string());

let src_files = vec![ResolvedSourceFile {
owning_package: None,
source_file_path: path.clone(),
import_export_info: ResolvedImportExportInfo {
exported_ids: amap2![
a_sym.clone() => Default::default()
],
..Default::default()
},
}];

let mut graph = Graph::from_source_files(src_files.iter());
graph.mark_symbol(&path, &a_sym, UsedTag::FROM_ENTRY);
graph.mark_symbol(&path, &a_sym, UsedTag::FROM_IGNORED);

// check the value is the union
let tags = graph.get_symbol_tags(&path, &a_sym);
assert_eq!(
tags.unwrap().clone(),
UsedTag::empty()
.union(UsedTag::FROM_ENTRY)
.union(UsedTag::FROM_IGNORED)
)
}
}

0 comments on commit 5c363c5

Please sign in to comment.