Skip to content

Commit 1b6336d

Browse files
Jo2234maria-rcks
andauthored
fix(shared): preserve omitted components in zero-major caret ranges (#29335)
Co-authored-by: maria-rcks <[email protected]>
1 parent df2dfde commit 1b6336d

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎packages/shared/src/semver.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,18 @@ describe("semver helpers", () => {
5959
});
6060

6161
it("honors caret range upper bounds for zero-major versions", () => {
62+
expect(satisfiesSemverRange("0.5.0", "^0")).toBe(true);
63+
expect(satisfiesSemverRange("0.0.4", "^0.0")).toBe(true);
64+
expect(satisfiesSemverRange("1.0.0", "^0")).toBe(false);
65+
expect(satisfiesSemverRange("0.1.0", "^0.0")).toBe(false);
6266
expect(satisfiesSemverRange("0.2.3", "^0.2.3")).toBe(true);
6367
expect(satisfiesSemverRange("0.2.9", "^0.2.3")).toBe(true);
6468
expect(satisfiesSemverRange("0.3.0", "^0.2.3")).toBe(false);
6569
expect(satisfiesSemverRange("0.5.0", "^0.2.3")).toBe(false);
6670
expect(satisfiesSemverRange("0.0.3", "^0.0.3")).toBe(true);
6771
expect(satisfiesSemverRange("0.0.4", "^0.0.3")).toBe(false);
72+
expect(satisfiesSemverRange("0.0.0", "^0.0.0")).toBe(true);
73+
expect(satisfiesSemverRange("0.0.1", "^0.0.0")).toBe(false);
6874
});
6975

7076
it("keeps the range checker stringifiable and executable as plain JavaScript", () => {
@@ -74,6 +80,8 @@ describe("semver helpers", () => {
7480
expect(source).toContain("function satisfiesSemverRange");
7581
expect(source).not.toContain(": string");
7682
expect(source).not.toContain(": boolean");
83+
expect(recreated("0.5.0", "^0")).toBe(true);
84+
expect(recreated("0.0.4", "^0.0")).toBe(true);
7785
expect(recreated("24.10.0", ">=24.10")).toBe(true);
7886
expect(recreated("24.9.9", ">=24.10")).toBe(false);
7987
});

‎packages/shared/src/semver.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ export const satisfiesSemverRange: (rawVersion: string, range: string) => boolea
198198
if (compared < 0) {
199199
return false;
200200
}
201-
if (target.major > 0) {
201+
if (target.major > 0 || targetMatch[2] === undefined) {
202202
return version.major === target.major;
203203
}
204-
if (target.minor > 0) {
204+
if (target.minor > 0 || targetMatch[3] === undefined) {
205205
return version.major === 0 && version.minor === target.minor;
206206
}
207207
return version.major === 0 && version.minor === 0 && version.patch === target.patch;

0 commit comments

Comments
 (0)