Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Aug 24, 2023
1 parent 86b6577 commit d921571
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libs/wingc/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ impl<'s> Parser<'s> {
// we need to inspect the npm dependency to figure out if it's a JSII library or a Wing library
// first, find where the package.json is located
let module_name = module_symbol.name[1..module_symbol.name.len() - 1].to_string();
let source_dir = Path::new(&self.source_name).parent().unwrap().to_str().unwrap();
let source_dir = Path::new(&self.source_name).parent().unwrap();
let module_dir =
wingii::util::package_json::find_dependency_directory(&module_name, &source_dir).ok_or_else(|| {
self
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4009,7 +4009,7 @@ impl<'a> TypeChecker<'a> {

assembly_name
} else {
let source_dir = self.source_path.parent().unwrap().to_str().unwrap();
let source_dir = self.source_path.parent().unwrap();
let assembly_name = match self.jsii_types.load_dep(library_name.as_str(), source_dir) {
Ok(name) => name,
Err(type_error) => {
Expand Down
8 changes: 5 additions & 3 deletions libs/wingii/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub mod type_system {
use crate::util::package_json;
use crate::Result;
use std::collections::HashMap;
use std::path::Path;

pub struct TypeSystem {
assemblies: HashMap<String, Assembly>,
Expand Down Expand Up @@ -240,10 +241,11 @@ pub mod type_system {
Ok(name)
}

pub fn load_dep(&mut self, dep: &str, search_start: &str) -> Result<AssemblyName> {
pub fn load_dep(&mut self, dep: &str, search_start: &Path) -> Result<AssemblyName> {
let module_dir = package_json::find_dependency_directory(dep, search_start).ok_or(format!(
"Unable to load \"{}\": Module not found in \"{}\"",
dep, search_start
dep,
search_start.display()
))?;
self.load_module(&module_dir)
}
Expand Down Expand Up @@ -279,7 +281,7 @@ pub mod type_system {
let deps = package_json::dependencies_of(&package);
for dep in deps {
if !bundled.contains(&dep) {
let dep_dir = package_json::find_dependency_directory(&dep, &module_directory).ok_or(format!(
let dep_dir = package_json::find_dependency_directory(&dep, &Path::new(&module_directory)).ok_or(format!(
"Unable to load \"{}\": Module not found from \"{}\"",
dep, module_directory
))?;
Expand Down
4 changes: 2 additions & 2 deletions libs/wingii/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub mod package_json {
})
}

pub fn find_dependency_directory(dependency_name: &str, search_start: &str) -> Option<String> {
let entrypoint = resolve_from(dependency_name, Path::new(search_start));
pub fn find_dependency_directory(dependency_name: &str, search_start: &Path) -> Option<String> {
let entrypoint = resolve_from(dependency_name, search_start);
let entrypoint = entrypoint.ok()?;
let dep_pkg_json_path = find_package_json_up(dependency_name, entrypoint);
let dep_pkg_json_path = dep_pkg_json_path?;
Expand Down

0 comments on commit d921571

Please sign in to comment.