Skip to content

Commit d11687c

Browse files
niemyjskiCopilot
andcommitted
Fix parseVersion trailing separator regression
readVersionIdentifiers incorrectly included trailing dots/dashes when the input ended immediately after a separator. Track lastValidEnd to return the position of the last complete identifier segment instead. Added test cases for trailing separator edge cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3d03e4d commit d11687c

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

packages/core/src/Utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,27 @@ function readDigits(input: string, start: number): number {
8888

8989
function readVersionIdentifiers(input: string, start: number): number {
9090
let index = start;
91+
let lastValidEnd = -1;
9192
while (index < input.length) {
9293
const segmentStart = index;
9394
while (index < input.length && isVersionIdentifierCode(input.charCodeAt(index))) {
9495
index++;
9596
}
9697

9798
if (index === segmentStart) {
98-
return -1;
99+
return lastValidEnd;
99100
}
100101

102+
lastValidEnd = index;
103+
101104
if (input[index] !== ".") {
102105
return index;
103106
}
104107

105108
index++;
106109
}
107110

108-
return index;
111+
return lastValidEnd;
109112
}
110113

111114
export function parseVersion(source: string): string | null {

packages/core/test/Utils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ describe("Utils", () => {
627627
test("should parse semantic version labels without regex backtracking", () => {
628628
expect(parseVersion("release/v1.2.3-rc.1+build.5/index.js")).toBe("v1.2.3-rc.1+build.5");
629629
expect(parseVersion("release-1.2.3-.js")).toBe("1.2.3");
630+
expect(parseVersion("1.2.3-")).toBe("1.2.3");
631+
expect(parseVersion("1.2.3+")).toBe("1.2.3");
632+
expect(parseVersion("1.2.3-rc.")).toBe("1.2.3-rc");
633+
expect(parseVersion("1.2.3-rc.1+build.")).toBe("1.2.3-rc.1+build");
630634
});
631635

632636
test("should generate guids without Math.random", () => {

0 commit comments

Comments
 (0)