Skip to content

Commit 2b1e15c

Browse files
fix(shared): preserve hyphens in prerelease versions (#23430)
Co-authored-by: maria-rcks <[email protected]>
1 parent c65e488 commit 2b1e15c

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ describe("semver helpers", () => {
3535
expect(compareSemverVersions("2.1.111-beta.1", "2.1.111")).toBeLessThan(0);
3636
});
3737

38+
it("preserves hyphens within prerelease identifiers", () => {
39+
expect(normalizeSemverVersion("20-alpha-1")).toBe("20.0.0-alpha-1");
40+
expect(parseSemver("1.2.3-alpha-1.2")).toEqual({
41+
major: 1,
42+
minor: 2,
43+
patch: 3,
44+
prerelease: ["alpha-1", "2"],
45+
});
46+
expect(parseSemver("1.2.3-x-y-z.--")?.prerelease).toEqual(["x-y-z", "--"]);
47+
});
48+
49+
it("distinguishes releases whose identifiers differ after a hyphen", () => {
50+
expect(compareSemverVersions("1.2.3-alpha-1", "1.2.3-alpha-2")).toBeLessThan(0);
51+
expect(compareSemverVersions("1.2.3-alpha-2", "1.2.3-alpha-1")).toBeGreaterThan(0);
52+
expect(compareSemverVersions("1.2.3-alpha-1.2", "1.2.3-alpha-1.10")).toBeLessThan(0);
53+
expect(compareSemverVersions("1.2.3-alpha.1+build-2", "1.2.3-alpha.1+build-3")).toBe(0);
54+
});
55+
3856
it("supports comparison comparators", () => {
3957
expect(satisfiesSemverRange("24.9.0", ">=24.0 <24.10")).toBe(true);
4058
expect(satisfiesSemverRange("24.10.0", ">=24.0 <24.10")).toBe(false);

‎packages/shared/src/semver.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ interface ParsedSemver {
88
const SEMVER_NUMBER_SEGMENT = /^\d+$/;
99

1010
export function normalizeSemverVersion(version: string): string {
11-
const [main, prerelease] = version.trim().split("-", 2);
11+
const [versionWithoutBuildMetadata = ""] = version.trim().split("+", 1);
12+
const [main, ...prereleaseParts] = versionWithoutBuildMetadata.split("-");
13+
const prerelease = prereleaseParts.join("-");
1214
const segments: string[] = [];
1315
for (const segment of (main ?? "").split(".")) {
1416
const trimmed = segment.trim();
@@ -26,7 +28,8 @@ export function normalizeSemverVersion(version: string): string {
2628

2729
export function parseSemver(value: string): ParsedSemver | null {
2830
const normalized = normalizeSemverVersion(value).replace(/^v/, "");
29-
const [main = "", prerelease] = normalized.split("-", 2);
31+
const [main = "", ...prereleaseParts] = normalized.split("-");
32+
const prerelease = prereleaseParts.join("-");
3033
const segments = main.split(".");
3134
if (segments.length !== 3) {
3235
return null;

0 commit comments

Comments
 (0)