Skip to content

Commit

Permalink
Add build options for imports info
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Dec 28, 2024
1 parent 86ba7f6 commit b68545c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 43 additions & 1 deletion src/imports_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,46 @@ pub struct ImportsInfo {
external_imports_metadata: HashMap<(PackageItemToken, String), ImportMetadata>,
}

#[derive(Debug, Clone)]
pub struct ImportsInfoBuildOptions {
include_typechecking_imports: bool,
include_external_imports: bool,
}

impl Default for ImportsInfoBuildOptions {
fn default() -> Self {
Self::new()
}
}

impl ImportsInfoBuildOptions {
pub fn new() -> Self {
ImportsInfoBuildOptions {
include_typechecking_imports: true,
include_external_imports: true,
}
}

pub fn exclude_typechecking_imports(mut self) -> Self {
self.include_typechecking_imports = false;
self
}

pub fn exclude_external_imports(mut self) -> Self {
self.include_external_imports = false;
self
}
}

impl ImportsInfo {
pub fn build(package_info: PackageInfo) -> Result<Self> {
ImportsInfo::build_with_options(package_info, ImportsInfoBuildOptions::new())
}

pub fn build_with_options(
package_info: PackageInfo,
options: ImportsInfoBuildOptions,
) -> Result<Self> {
let all_raw_imports = get_all_raw_imports(&package_info)?;

let mut imports_info = ImportsInfo {
Expand All @@ -64,6 +102,10 @@ impl ImportsInfo {

for (item, raw_imports) in all_raw_imports {
for raw_import in raw_imports {
if !options.include_typechecking_imports && raw_import.is_typechecking {
continue;
}

let metadata = ImportMetadata {
line_number: raw_import.line_number,
is_typechecking: raw_import.is_typechecking,
Expand Down Expand Up @@ -91,7 +133,7 @@ impl ImportsInfo {
};

imports_info.add_internal_import(item, internal_item, Some(metadata))?;
} else {
} else if options.include_external_imports {
imports_info.add_external_import(item, raw_import.pypath, Some(metadata))?;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ mod testutils;
pub use testutils::TestPackage;

pub use errors::Error;
pub use imports_info::{ImportMetadata, ImportsInfo, InternalImportsQueries, PackageItemTarget};
pub use imports_info::{
ImportMetadata, ImportsInfo, ImportsInfoBuildOptions, InternalImportsQueries, PackageItemTarget,
};
pub use package_info::{
Module, ModuleToken, Package, PackageInfo, PackageItem, PackageItemToken, PackageToken,
};

0 comments on commit b68545c

Please sign in to comment.