Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(npm): support ranges separated by && or comma #38

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 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, whitespace_or_and), |ranges| {
let mut final_range = VersionRange::all();
for range in ranges {
final_range = final_range.clamp(&range);
Expand All @@ -227,6 +227,10 @@ fn range(input: &str) -> ParseResult<VersionRange> {
)(input)
}

fn whitespace_or_and(input: &str) -> ParseResult<&str> {
or(logical_and, whitespace)(input)
}

#[derive(Debug, Clone)]
struct Hyphen {
start: Partial,
Expand Down Expand Up @@ -254,6 +258,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 +644,17 @@ 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_with_tag() {
let req = parse_npm_version_req("latest").unwrap();
Expand Down
Loading