Skip to content

Commit

Permalink
unused_finder: show re-exported symbols in debug visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Huang-Hobbs authored and Adjective-Object committed Feb 10, 2025
1 parent 3f2bd4e commit f44af1f
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "unused_finder: show re-exported symbols in debug visualization",
"packageName": "@good-fences/api",
"email": "[email protected]",
"dependentChangeType": "patch"
}
18 changes: 16 additions & 2 deletions crates/unused_bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ enum Commands {
Graph {
#[arg(short = 'f', alias = "filter")]
filter: Option<String>,
#[arg(short = 'u', alias = "up_limit")]
up_limit: Option<usize>,
#[arg(short = 'd', alias = "down_limit")]
down_limit: Option<usize>,
},
}

Expand Down Expand Up @@ -166,11 +170,21 @@ fn main() -> Result<()> {
logger.log(format!("result:\n{report}"));

match &args.command {
Some(Commands::Graph { filter }) => {
Some(Commands::Graph {
filter,
up_limit,
down_limit,
}) => {
println!("Generating graph.dot file...");
let file = std::fs::File::create("graph.dot").expect("Failed to create graph.dot");
let mut stream = std::io::BufWriter::new(file);
result.write_dot_graph(logger, filter.as_ref().map(|x| x.as_str()), &mut stream)?;
result.write_dot_graph(
logger,
filter.as_ref().map(|x| x.as_str()),
*up_limit,
*down_limit,
&mut stream,
)?;
stream.flush().expect("Failed to flush graph.dot");
println!("Done!");
}
Expand Down
7 changes: 6 additions & 1 deletion crates/unused_finder/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl GraphFile {
Self {
file_tags: UsedTag::default(),
symbol_tags: AHashMap::with_capacity_and_hasher(
file.import_export_info.exported_ids.len(),
file.import_export_info.num_exported_symbols(),
Default::default(),
),
file_path: file.source_file_path.clone(),
Expand Down Expand Up @@ -134,6 +134,11 @@ impl Graph {
file.tag_symbol(symbol, tag);
}

pub fn get_file_tags(&self, path: &Path) -> Option<UsedTag> {
let file_id = self.path_to_id.get(path)?;
Some(self.files[*file_id].file_tags)
}

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];
Expand Down
17 changes: 17 additions & 0 deletions crates/unused_finder/src/parse/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ pub struct ResolvedImportExportInfo {
}

impl ResolvedImportExportInfo {
pub fn get_exported_symbol(&self, symbol: &ExportedSymbol) -> Option<&ExportedSymbolMetadata> {
self.exported_ids.get(symbol).or_else(|| {
self.export_from_symbols
.values()
.find_map(|reexported_symbols| {
reexported_symbols
.keys()
.find(|reexported_symbol| {
reexported_symbol.renamed_to.as_ref() == Some(symbol)
|| (reexported_symbol.renamed_to.is_none()
&& reexported_symbol.imported == *symbol)
})
.map(|reexported_symbol| &reexported_symbols[reexported_symbol])
})
})
}

pub fn num_exported_symbols(&self) -> usize {
self.exported_ids.len() + self.export_from_symbols.len()
}
Expand Down
38 changes: 19 additions & 19 deletions crates/unused_finder/src/parse/exports_visitor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod test {
pub is_typeonly: bool,
}

fn exported_ids(
fn own_exported_ids(
visitor: &ExportsVisitor<impl SrcFileLogger>,
) -> AHashMap<ExportedSymbol, TestMeta> {
visitor
Expand Down Expand Up @@ -112,7 +112,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
);
}

Expand All @@ -133,7 +133,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
);
}
#[test]
Expand All @@ -152,7 +152,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -174,7 +174,7 @@ mod test {
is_typeonly: true
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
);
}

Expand All @@ -194,7 +194,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
);
}

Expand All @@ -213,7 +213,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -232,7 +232,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand Down Expand Up @@ -323,7 +323,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand Down Expand Up @@ -351,7 +351,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand Down Expand Up @@ -379,7 +379,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand Down Expand Up @@ -407,7 +407,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -426,7 +426,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -445,7 +445,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -466,7 +466,7 @@ mod test {
is_typeonly: true
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -485,7 +485,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -503,7 +503,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -521,7 +521,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand All @@ -544,7 +544,7 @@ mod test {
is_typeonly: false
}
),
exported_ids(&visitor)
own_exported_ids(&visitor)
)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/unused_finder/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl From<&UnusedFinderResult> for UnusedFinderReport {
return None;
}

let ast_symbol = file.import_export_info.exported_ids.get(symbol_name)?;
let ast_symbol = file.import_export_info.get_exported_symbol(symbol_name)?;

Some(SymbolReport {
id: symbol_name.to_string(),
Expand All @@ -198,7 +198,7 @@ impl From<&UnusedFinderResult> for UnusedFinderReport {
return None;
}

let ast_symbol = file.import_export_info.exported_ids.get(symbol_name)?;
let ast_symbol = file.import_export_info.get_exported_symbol(symbol_name)?;

Some(SymbolReportWithTags {
symbol: SymbolReport {
Expand Down
Loading

0 comments on commit f44af1f

Please sign in to comment.