Skip to content

Commit c3f5298

Browse files
committed
fix(core): workaround package.json files with no name
Closes #261
1 parent 0b4c7d5 commit c3f5298

File tree

7 files changed

+22
-24
lines changed

7 files changed

+22
-24
lines changed

src/dependency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Dependency {
177177
for instance in every_instance_in_the_project {
178178
if *instance.name_internal.borrow() == *self.name_internal {
179179
for snapped_to_package in snapped_to_packages {
180-
if instance.package.borrow().get_name_unsafe() == snapped_to_package.borrow().get_name_unsafe() {
180+
if instance.package.borrow().name == snapped_to_package.borrow().name {
181181
return Some(instance.actual_specifier.clone());
182182
}
183183
}
@@ -211,7 +211,7 @@ impl Dependency {
211211
}
212212
let specifier_order = b.actual_specifier.unwrap().cmp(&a.actual_specifier.unwrap());
213213
if matches!(specifier_order, Ordering::Equal) {
214-
a.package.borrow().get_name_unsafe().cmp(&b.package.borrow().get_name_unsafe())
214+
a.package.borrow().name.cmp(&b.package.borrow().name)
215215
} else {
216216
specifier_order
217217
}

src/effects/ui.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl Ui<'_> {
459459
if self.ctx.config.cli.show_packages {
460460
packages
461461
.iter()
462-
.sorted_by_key(|package| package.borrow().get_name_unsafe())
462+
.sorted_by_key(|package| package.borrow().name.clone())
463463
.for_each(|package| {
464464
self.print_formatted_package(&package.borrow());
465465
});
@@ -486,7 +486,7 @@ impl Ui<'_> {
486486
if self.ctx.config.cli.show_packages {
487487
mismatches
488488
.iter()
489-
.sorted_by_key(|mismatch| mismatch.package.borrow().get_name_unsafe())
489+
.sorted_by_key(|mismatch| mismatch.package.borrow().name.clone())
490490
.for_each(|mismatch| {
491491
let icon = "-".dimmed();
492492
let package = mismatch.package.borrow();
@@ -500,9 +500,8 @@ impl Ui<'_> {
500500

501501
/// Render a clickable link to a package.json file
502502
fn package_json_link(&self, package: &PackageJson) -> ColoredString {
503-
let name = package.get_name_unsafe();
504503
let file_path = package.file_path.to_str().unwrap();
505-
let plain_link = self.link(format!("file:{file_path}"), name);
504+
let plain_link = self.link(format!("file:{file_path}"), package.name.clone());
506505
format!("{plain_link}").normal()
507506
}
508507

src/group_selector.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ impl GroupSelector {
8686
}
8787

8888
pub fn matches_packages(&self, instance: &Instance) -> bool {
89-
matches_globs(
90-
&instance.package.borrow().get_name_unsafe(),
91-
&self.include_packages,
92-
&self.exclude_packages,
93-
)
89+
matches_globs(&instance.package.borrow().name, &self.include_packages, &self.exclude_packages)
9490
}
9591

9692
pub fn matches_dependencies(&self, instance: &Instance) -> bool {
@@ -158,13 +154,13 @@ fn with_resolved_keywords(dependency_names: &[String], packages: &Packages) -> V
158154
match dependency_name.as_str() {
159155
"$LOCAL" => {
160156
for package in packages.all.iter() {
161-
let package_name = package.borrow().get_name_unsafe();
157+
let package_name = package.borrow().name.clone();
162158
resolved_dependencies.push(package_name);
163159
}
164160
}
165161
"!$LOCAL" => {
166162
for package in packages.all.iter() {
167-
let package_name = package.borrow().get_name_unsafe();
163+
let package_name = package.borrow().name.clone();
168164
resolved_dependencies.push(format!("!{}", package_name));
169165
}
170166
}

src/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Instance {
6262
dependency_type: &DependencyType,
6363
package: Rc<RefCell<PackageJson>>,
6464
) -> Instance {
65-
let package_name = package.borrow().get_name_unsafe();
65+
let package_name = package.borrow().name.clone();
6666
let specifier = Specifier::new(&raw_specifier);
6767
Instance {
6868
actual_specifier: specifier.clone(),

src/package_json.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use {
88

99
#[derive(Debug)]
1010
pub struct PackageJson {
11+
/// The name property of the package.json
12+
pub name: String,
1113
/// The path to the package.json file
1214
pub file_path: PathBuf,
1315
/// Syncpack formatting mismatches found in the file
@@ -63,6 +65,11 @@ impl PackageJson {
6365
error!("Invalid JSON: {}", &file_path.to_str().unwrap());
6466
})
6567
.map(|contents: Value| Self {
68+
name: contents
69+
.pointer("/name")
70+
.and_then(|name| name.as_str())
71+
.unwrap_or("NAME_IS_MISSING")
72+
.to_string(),
6673
file_path: file_path.clone(),
6774
formatting_mismatches: RefCell::new(vec![]),
6875
json: RefCell::new(contents.to_string()),
@@ -86,11 +93,6 @@ impl PackageJson {
8693
}
8794
}
8895

89-
/// Convenience method to get the name of the package
90-
pub fn get_name_unsafe(&self) -> String {
91-
self.get_string("/name").expect("package.json file has no name property")
92-
}
93-
9496
/// Deeply get a property in the parsed package.json
9597
pub fn get_prop(&self, pointer: &str) -> Option<Value> {
9698
self.contents.borrow().pointer(pointer).cloned()

src/packages.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ impl Packages {
6161

6262
/// Get a package.json file by its name
6363
pub fn get_by_name(&self, name: &str) -> Option<Rc<RefCell<PackageJson>>> {
64-
self
65-
.all
66-
.iter()
67-
.find(|package| package.borrow().get_name_unsafe() == name)
68-
.map(Rc::clone)
64+
self.all.iter().find(|package| package.borrow().name == name).map(Rc::clone)
6965
}
7066

7167
/// Get every instance of a dependency from every package.json file

src/test/mock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ pub fn rcfile_from_mock(value: serde_json::Value) -> Rcfile {
6161
/// Parse a package.json string
6262
pub fn package_json_from_value(contents: Value) -> PackageJson {
6363
PackageJson {
64+
name: contents
65+
.pointer("/name")
66+
.and_then(|name| name.as_str())
67+
.unwrap_or("NAME_IS_MISSING")
68+
.to_string(),
6469
file_path: PathBuf::new(),
6570
formatting_mismatches: RefCell::new(vec![]),
6671
json: RefCell::new(contents.to_string()),

0 commit comments

Comments
 (0)