diff --git a/lib/versioning/maven/compare.ts b/lib/versioning/maven/compare.ts index 4e13d83ea106bfc6180f2e9dcc85782a24ef4420..cf3980772a6db256cd5030b98123fa6b558e901f 100644 --- a/lib/versioning/maven/compare.ts +++ b/lib/versioning/maven/compare.ts @@ -295,7 +295,7 @@ function isVersion(version: string): boolean { const INCLUDING_POINT = 'INCLUDING_POINT'; const EXCLUDING_POINT = 'EXCLUDING_POINT'; -function parseRange(rangeStr: string): any { +function parseRange(rangeStr: string): Range[] { function emptyInterval(): Range { return { leftType: null, @@ -499,24 +499,20 @@ function autoExtendMavenRange( if (isPoint(range)) { return `[${newValue}]`; } - let nearestIntervalIdx = 0; - const len = range.length; - for (let idx = len - 1; idx >= 0; idx = -1) { - const { leftValue, rightValue } = range[idx]; - if (rightValue === null) { - nearestIntervalIdx = idx; - break; - } - if (compare(rightValue, newValue) === -1) { - nearestIntervalIdx = idx; - break; - } - if (leftValue && compare(leftValue, newValue) !== 1) { - return currentRepresentation; - } + + const interval = [...range].reverse().find((elem) => { + const { rightType, rightValue } = elem; + return ( + rightValue === null || + (rightType === INCLUDING_POINT && compare(rightValue, newValue) === -1) || + (rightType === EXCLUDING_POINT && compare(rightValue, newValue) !== 1) + ); + }); + + if (!interval) { + return currentRepresentation; } - const interval = range[nearestIntervalIdx]; const { leftValue, rightValue } = interval; if ( leftValue !== null && @@ -545,13 +541,6 @@ function autoExtendMavenRange( interval.leftValue = coerceRangeValue(leftValue, newValue); } - if (interval.leftValue && interval.rightValue) { - const correctRepresentation = - compare(interval.leftValue, interval.rightValue) === 1 - ? null - : rangeToStr(range); - return correctRepresentation || currentRepresentation; - } return rangeToStr(range); } diff --git a/lib/versioning/maven/index.spec.ts b/lib/versioning/maven/index.spec.ts index 537d8ba7958aabddc4946a8fd6e3df92122f8229..8ca9826afa855710e47be217a56be02a7abd7869 100644 --- a/lib/versioning/maven/index.spec.ts +++ b/lib/versioning/maven/index.spec.ts @@ -279,6 +279,17 @@ describe(getName(), () => { ['(,1.0]', '2.0.0', '(,2.0]'], ['(,1]', '2.0.0', '(,2]'], ['(,1.0.0-foobar]', '2.0.0', '(,2.0.0]'], + + ['[1,2]', '2', '[1,2]'], + ['[1,2)', '2', '[2,3)'], + ['[0,2)', '2', '[0,3)'], + ['[1.2,1.3]', '1.3', '[1.2,1.3]'], + ['[1.2,1.3)', '1.3', '[1.3,1.4)'], + ['[1.1,1.3)', '1.3', '[1.1,1.4)'], + ['[1.2.3,1.2.4]', '1.2.4', '[1.2.3,1.2.4]'], + ['[1.2.3,1.2.4)', '1.2.4', '[1.2.4,1.2.5)'], + ['[1.2.1,1.2.4)', '1.2.4', '[1.2.1,1.2.5)'], + ['[1,1.2.3)', '1.2.3', '[1,1.2.4)'], ]; sample.forEach(([oldRepr, newValue, newRepr]) => { expect(autoExtendMavenRange(oldRepr, newValue)).toEqual(newRepr);