Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions src/semver/SemverQuery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result<Group, AllocError> {
}
b'=' | b'v' => {
token.tag = TokenTag::Version;
is_or = true;
i += 1;
while i < input.len() && input[i] == b' ' {
i += 1;
Expand All @@ -901,7 +900,6 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result<Group, AllocError> {
}
b'0'..=b'9' | b'X' | b'x' | b'*' => {
token.tag = TokenTag::Version;
is_or = true;
}
b'|' => {
i += 1;
Expand All @@ -912,6 +910,8 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result<Group, AllocError> {
while i < input.len() && input[i] == b' ' {
i += 1;
}
// "||" is the only token that starts a new OR alternative; whitespace
// between comparators is an intersection (node-semver's range grammar).
Comment thread
robobun marked this conversation as resolved.
Resolved
is_or = true;
token.tag = TokenTag::None;
skip_round = true;
Expand Down
33 changes: 33 additions & 0 deletions test/cli/install/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,39 @@ describe("Bun.semver.satisfies()", () => {
}
});

test("space-separated comparators are an intersection, not alternatives", () => {
// In npm's range grammar only "||" starts a new alternative; whitespace between
// comparators is an intersection. Comparators starting with a digit, "x", "*",
// "=", or "v" used to begin a new "||" group. Every expectation matches node-semver.
const cases: [version: string, range: string, expected: boolean][] = [
["2.5.0", "1.x 2.x", false],
["1.0.1", "<1.0.0 1.x", false],
["1.2.3", ">=2.0.0 1.2.3", false],
["1.2.3", "1.2.3 4.5.6", false],
["4.5.6", "1.2.3 4.5.6", false],
["1.2.3", ">=2.0.0 =1.2.3", false],
["1.2.3", ">=2.0.0 v1.2.3", false],
["1.0.0", ">=2.0.0 *", false],
["1.0.0", ">=2.0.0 x", false],
["1.0.0", ">=2.0.0 x.x.x", false],
["1.5.0", ">=1.0.0 1.x <1.4.0", false],
["4.5.6", "4.5.6 7.8.9 || 1.2.3", false],
// the intersection holds, or an "||" alternative holds
["1.3.0", ">=1.0.0 1.x <1.4.0", true],
["2.5.0", "1.x || 2.x", true],
["1.0.1", "<1.0.0 || 1.x", true],
["1.2.3", ">=2.0.0 || 1.2.3", true],
["1.2.3", "1.2.3 4.5.6 || 1.2.3", true],
["1.2.3", "~1.2.1 1.2.3", true],
["1.2.3", "~1.2.1 =1.2.3", true],
["1.2.3", ">=1.2.1 1.2.3", true],
["1.2.3", "1.2.3 >=1.2.1", true],
];
expect(cases.map(([version, range]) => ({ version, range, satisfies: satisfies(version, range) }))).toEqual(
cases.map(([version, range, expected]) => ({ version, range, satisfies: expected })),
);
});

test("pre-release snapshot", () => {
expect(unsortedPrereleases.sort(Bun.semver.order)).toMatchSnapshot();
});
Expand Down
Loading