Skip to content

Commit

Permalink
add report_exported_items flag to gate reporting exported items (#77)
Browse files Browse the repository at this point in the history
Disable per-item reporting with report_exported_items flag

This is for cleaner test reporting while debugging unused files in
`unused_bin`

---------

Co-authored-by: Max Huang-Hobbs <[email protected]>
  • Loading branch information
Adjective-Object and Max Huang-Hobbs authored Aug 19, 2024
1 parent 351fe0c commit 6afa7ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "add report_exported_items flag to gate reporting exported items",
"packageName": "@good-fences/api",
"email": "[email protected]",
"dependentChangeType": "patch"
}
59 changes: 35 additions & 24 deletions crates/unused_finder/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ use tsconfig_paths::TsconfigPathsJson;
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FindUnusedItemsConfig {
// Trace exported symbols that are not imported anywhere in the project
pub report_exported_items: bool,
// Paths to read as source files
pub paths_to_read: Vec<String>,
// Path to the root tsconfig.paths.json file used to resolve ts imports between projects.
// Note: this should be removed and replaced with normal node resolution.
pub ts_config_path: String,
// Files under matching dirs won't be scanned.
pub skipped_dirs: Vec<String>,
Expand Down Expand Up @@ -120,6 +125,7 @@ pub fn find_unused_items(
config: FindUnusedItemsConfig,
) -> Result<UnusedFinderReport, js_err::JsErr> {
let FindUnusedItemsConfig {
report_exported_items,
paths_to_read,
ts_config_path,
skipped_dirs,
Expand Down Expand Up @@ -221,33 +227,38 @@ pub fn find_unused_items(
.iter()
.filter(|f| !f.1.is_used && !allow_list.iter().any(|p| p.matches(f.0))),
);
let unused_files_items: HashMap<String, Vec<ExportedItemReport>> = graph
.files
.iter()
.filter_map(|(file_path, info)| {
if info.is_used {
match file_path_exported_items_map.remove(file_path) {
Some(mut exported_items) => {
let unused_exports = &info.unused_exports;
if unused_exports.is_empty() {
return None;

let unused_files_items: HashMap<String, Vec<ExportedItemReport>> = if report_exported_items {
graph
.files
.iter()
.filter_map(|(file_path, info)| {
if info.is_used {
match file_path_exported_items_map.remove(file_path) {
Some(mut exported_items) => {
let unused_exports = &info.unused_exports;
if unused_exports.is_empty() {
return None;
}
let unused_exports = exported_items
.drain(0..)
.filter(|exported| {
unused_exports
.iter()
.any(|unused| unused.to_string() == exported.id.to_string())
})
.collect();
return Some((file_path.to_string(), unused_exports));
}
let unused_exports = exported_items
.drain(0..)
.filter(|exported| {
unused_exports
.iter()
.any(|unused| unused.to_string() == exported.id.to_string())
})
.collect();
return Some((file_path.to_string(), unused_exports));
None => return None,
}
None => return None,
}
}
None
})
.collect();
None
})
.collect()
} else {
HashMap::new()
};

Ok(UnusedFinderReport {
unused_files: reported_unused_files
Expand Down
1 change: 1 addition & 0 deletions crates/unused_finder/src/unused_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct UnusedFinder {
impl UnusedFinder {
pub fn new(config: FindUnusedItemsConfig) -> anyhow::Result<Self, JsErr> {
let FindUnusedItemsConfig {
report_exported_items: _,
paths_to_read,
ts_config_path,
skipped_dirs,
Expand Down

0 comments on commit 6afa7ac

Please sign in to comment.