Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "unused_finder: mark files as typeonly if their contents are typeonly",
"packageName": "@good-fences/api",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "unused_finder: expand star exports during source file resolution",
"packageName": "@good-fences/api",
"email": "[email protected]",
"dependentChangeType": "patch"
}
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
19 changes: 18 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,23 @@ impl Graph {
file.tag_symbol(symbol, tag);
}

pub fn mark_file(&mut self, path: &Path, tag: UsedTag) {
let file_id = match self.path_to_id.get(path) {
Some(id) => *id,
None => {
return;
}
};

let file = &mut self.files[file_id];
file.file_tags.insert(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
14 changes: 7 additions & 7 deletions crates/unused_finder/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
graph::{Graph, GraphFile},
parse::ExportedSymbol,
tag::UsedTag,
UnusedFinderResult, UsedTagEnum,
UnusedFinderConfig, UnusedFinderResult, UsedTagEnum,
};

// Report of a single exported item in a file
Expand Down Expand Up @@ -127,11 +127,11 @@ fn extract_symbols<T: Send + Sync>(
.collect::<AHashMap<String, Vec<T>>>()
}

fn is_used(tags: &UsedTag) -> bool {
fn is_used(tags: &UsedTag, config: &UnusedFinderConfig) -> bool {
tags.contains(UsedTag::FROM_ENTRY)
|| tags.contains(UsedTag::FROM_IGNORED)
|| tags.contains(UsedTag::FROM_TEST)
|| tags.contains(UsedTag::TYPE_ONLY)
|| (config.allow_unused_types && tags.contains(UsedTag::TYPE_ONLY))
}
fn include_extra(tags: &UsedTag) -> bool {
!tags.is_empty() && *tags != UsedTag::FROM_ENTRY
Expand All @@ -144,7 +144,7 @@ impl From<&UnusedFinderResult> for UnusedFinderReport {
.files
.par_iter()
.filter_map(|file| {
if is_used(&file.file_tags) {
if is_used(&file.file_tags, &value.config) {
return None;
}
Some(file.file_path.to_string_lossy().to_string())
Expand Down Expand Up @@ -173,12 +173,12 @@ impl From<&UnusedFinderResult> for UnusedFinderReport {
let symbol_bitflags: &UsedTag =
file.symbol_tags.get(symbol_name).unwrap_or(&default);

if is_used(symbol_bitflags) {
if is_used(symbol_bitflags, &value.config) {
// don't return used symbols
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