feat(shared): support shorthand (major-only) versions in the semver helpers (#5027)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
Arham AminandClaude Opus 4.8 authored and GitHub committed 2026-07-30 22:13:46 +00:00
1 parent 34b15a9ace
commit 063ed7120a
2 files changed
+31 -2

No files matched your search

+25 -1
View File
@@ -1,6 +1,11 @@
import { describe, expect, it } from "vite-plus/test";
import { compareSemverVersions, normalizeSemverVersion, satisfiesSemverRange } from "./semver.ts";
import {
compareSemverVersions,
normalizeSemverVersion,
parseSemver,
satisfiesSemverRange,
} from "./semver.ts";
describe("semver helpers", () => {
it("matches supported range groups", () => {
@@ -18,6 +23,25 @@ describe("semver helpers", () => {
expect(normalizeSemverVersion("2.1")).toBe("2.1.0");
});
it("normalizes and parses shorthand major-only versions", () => {
expect(normalizeSemverVersion("20")).toBe("20.0.0");
expect(normalizeSemverVersion("v18")).toBe("v18.0.0");
expect(normalizeSemverVersion("20-rc.1")).toBe("20.0.0-rc.1");
expect(parseSemver("20")).toEqual({ major: 20, minor: 0, patch: 0, prerelease: [] });
});
it("compares shorthand versions numerically instead of lexically", () => {
// Regression: "20" vs "9" previously fell back to string comparison, which
// ordered "20" before "9" ("2" < "9").
expect(compareSemverVersions("20", "9")).toBeGreaterThan(0);
expect(compareSemverVersions("18", "18.0.0")).toBe(0);
});
it("still rejects non-numeric shorthand and keeps empty input empty", () => {
expect(parseSemver("abc")).toBeNull();
expect(normalizeSemverVersion("")).toBe("");
});
it("compares prerelease versions before stable versions", () => {
expect(compareSemverVersions("2.1.111-beta.1", "2.1.111")).toBeLessThan(0);
});
+6 -1
View File
@@ -17,7 +17,12 @@ export function normalizeSemverVersion(version: string): string {
}
}
if (segments.length === 2) {
// Pad shorthand versions ("20" or "20.1") up to three segments so major-only
// and minor-only inputs parse and compare numerically. This matches
// satisfiesSemverRange, which already treats a missing minor/patch as 0. The
// length > 0 guard keeps empty/garbage input empty (parseSemver still
// rejects it), and inputs with more than three segments are left untouched.
while (segments.length > 0 && segments.length < 3) {
segments.push("0");
}