Skip to content

Commit

Permalink
fix(npm): support ranges separated by && or comma (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Oct 21, 2024
1 parent 7b8d051 commit 1c376cd
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn range(input: &str) -> ParseResult<VersionRange> {
start: hyphen.start.as_lower_bound(),
end: hyphen.end.as_upper_bound(),
}),
map(separated_list(simple, whitespace), |ranges| {
map(separated_list(simple, range_separator), |ranges| {
let mut final_range = VersionRange::all();
for range in ranges {
final_range = final_range.clamp(&range);
Expand All @@ -227,6 +227,14 @@ fn range(input: &str) -> ParseResult<VersionRange> {
)(input)
}

fn range_separator(input: &str) -> ParseResult<()> {
fn comma(input: &str) -> ParseResult<()> {
map(delimited(skip_whitespace, ch(','), skip_whitespace), |_| ())(input)
}

or3(map(logical_and, |_| ()), comma, map(whitespace, |_| ()))(input)
}

#[derive(Debug, Clone)]
struct Hyphen {
start: Partial,
Expand Down Expand Up @@ -254,6 +262,11 @@ fn logical_or(input: &str) -> ParseResult<&str> {
delimited(skip_whitespace, tag("||"), skip_whitespace)(input)
}

// logical-and ::= ( ' ' ) * '&&' ( ' ' ) *
fn logical_and(input: &str) -> ParseResult<&str> {
delimited(skip_whitespace, tag("&&"), skip_whitespace)(input)
}

fn skip_whitespace_or_v(input: &str) -> ParseResult<()> {
map(
pair(skip_whitespace, pair(maybe(ch('v')), skip_whitespace)),
Expand Down Expand Up @@ -635,6 +648,28 @@ mod tests {
assert!(!tester.matches("6.1.0"));
}

#[test]
pub fn npm_version_req_and_range() {
let tester = NpmVersionReqTester::new(">= 1.2 && <= 2.0.0");
assert!(!tester.matches("1.1.9"));
assert!(tester.matches("1.2.2"));
assert!(tester.matches("1.2.0"));
assert!(tester.matches("1.9.9"));
assert!(!tester.matches("2.1.1"));
assert!(!tester.matches("2.0.1"));
}

#[test]
pub fn npm_version_req_comma_range() {
let tester = NpmVersionReqTester::new(">= 1.2, <= 2.0.0");
assert!(!tester.matches("1.1.9"));
assert!(tester.matches("1.2.2"));
assert!(tester.matches("1.2.0"));
assert!(tester.matches("1.9.9"));
assert!(!tester.matches("2.1.1"));
assert!(!tester.matches("2.0.1"));
}

#[test]
pub fn npm_version_req_with_tag() {
let req = parse_npm_version_req("latest").unwrap();
Expand Down

0 comments on commit 1c376cd

Please sign in to comment.