Skip to content

Commit

Permalink
fix: error for specifier with empty version constraint (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Sep 30, 2024
1 parent fa7bcc9 commit 2091f59
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ use super::VersionReq;
use super::XRange;

pub fn is_valid_npm_tag(value: &str) -> bool {
if value.trim().is_empty() {
return false;
}

// a valid tag is anything that doesn't get url encoded
// https://github.com/npm/npm-package-arg/blob/103c0fda8ed8185733919c7c6c73937cfb2baf3a/lib/npa.js#L399-L401
value
Expand Down Expand Up @@ -1273,6 +1277,18 @@ mod tests {
_ => unreachable!(),
}

// missing version req
let err = NpmPackageReqReference::from_str("npm:package@").unwrap_err();
match err {
PackageReqReferenceParseError::Invalid(err) => match err.source {
crate::package::PackageReqPartsParseError::SpecifierVersionReq(err) => {
assert_eq!(err.source.message, "Empty version constraint.");
}
_ => unreachable!(),
},
_ => unreachable!(),
}

// should parse leading slash
assert_eq!(
NpmPackageReqReference::from_str("npm:/@package/test/sub_path").unwrap(),
Expand Down Expand Up @@ -1380,4 +1396,11 @@ mod tests {
assert!(!req.matches(&Version::parse_from_npm("0.0.0-pre").unwrap()));
}
}

#[test]
fn test_is_valid_npm_tag() {
assert_eq!(is_valid_npm_tag("latest"), true);
assert_eq!(is_valid_npm_tag(""), false);
assert_eq!(is_valid_npm_tag("SD&*($#&%*(#*$%"), false);
}
}
7 changes: 4 additions & 3 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ impl PackageReq {

fn parse_with_path(
input: &str,
parse_version: impl FnOnce(
parse_version_req: impl FnOnce(
&str,
) -> Result<VersionReq, PackageReqPartsParseError>,
)
-> Result<VersionReq, PackageReqPartsParseError>,
) -> Result<(Self, &str), PackageReqPartsParseError> {
// Strip leading slash, which might come from import map
let input = input.strip_prefix('/').unwrap_or(input);
Expand All @@ -272,7 +273,7 @@ impl PackageReq {
let (last_name_part, version_req) = if let Some((last_name_part, version)) =
last_name_part.rsplit_once('@')
{
(last_name_part, Some(parse_version(version)?))
(last_name_part, Some(parse_version_req(version)?))
} else {
(last_name_part, None)
};
Expand Down
8 changes: 5 additions & 3 deletions src/specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ pub fn parse_version_req_from_specifier(
match range_result {
Ok(range) => RangeSetOrTag::RangeSet(VersionRangeSet(vec![range])),
Err(err) => {
if !is_valid_tag(input) {
return Err(err);
} else {
if is_valid_tag(input) {
RangeSetOrTag::Tag(input.to_string())
} else if input.trim().is_empty() {
return ParseError::fail(input, "Empty version constraint.");
} else {
return Err(err);
}
}
},
Expand Down

0 comments on commit 2091f59

Please sign in to comment.