From d0e1904758a936a864854d21b79f1f8356278b25 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Thu, 5 Mar 2020 10:49:11 -0500 Subject: [PATCH] feat(package): ignore private JS packages (#975) `private: true` in `package.json` files means that the package is not supposed to be published to npm, which makes the `version` field meaningless, so we should probably ignore it to not clutter the prompt too much. --- src/modules/package.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/modules/package.rs b/src/modules/package.rs index 6e1a3071..9cd17abb 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -41,6 +41,11 @@ fn extract_cargo_version(file_contents: &str) -> Option { fn extract_package_version(file_contents: &str) -> Option { let package_json: json::Value = json::from_str(file_contents).ok()?; + + if package_json.get("private").and_then(json::Value::as_bool) == Some(true) { + return None; + } + let raw_version = package_json.get("version")?.as_str()?; if raw_version == "null" { return None; @@ -163,7 +168,10 @@ mod tests { extract_package_version(&package_with_version), expected_version ); + } + #[test] + fn test_extract_package_version_without_version() { let package_without_version = json::json!({ "name": "spacefish" }) @@ -176,6 +184,49 @@ mod tests { ); } + #[test] + fn test_extract_package_version_with_null_version() { + let package_with_null_version = json::json!({ + "name": "spacefish", + "version": null + }) + .to_string(); + + let expected_version = None; + assert_eq!( + extract_package_version(&package_with_null_version), + expected_version + ); + } + + #[test] + fn test_extract_package_version_with_null_string_version() { + let package_with_null_string_version = json::json!({ + "name": "spacefish", + "version": "null" + }) + .to_string(); + + let expected_version = None; + assert_eq!( + extract_package_version(&package_with_null_string_version), + expected_version + ); + } + + #[test] + fn test_extract_private_package_version() { + let private_package = json::json!({ + "name": "spacefish", + "version": "0.1.0", + "private": true + }) + .to_string(); + + let expected_version = None; + assert_eq!(extract_package_version(&private_package), expected_version); + } + #[test] fn test_extract_poetry_version() { let poetry_with_version = toml::toml! {