Skip to content

Commit

Permalink
Fix extending behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Zharinov committed Jun 29, 2020
1 parent e10b8b3 commit c0de13d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
58 changes: 33 additions & 25 deletions lib/versioning/maven/compare.ts
Expand Up @@ -458,23 +458,18 @@ function coerceRangeValue(prev: string, next: string): string {
const nextTokens = tokenize(next, true);
const resultTokens = nextTokens.slice(0, prevTokens.length);
const align = Math.max(0, prevTokens.length - nextTokens.length);
for (let i = 0; i < align; i += 1) {
const tokenIdx = prevTokens.length + i - 1;
const token = prevTokens[tokenIdx];
resultTokens.push(nullFor(token));
if (align > 0) {
resultTokens.push(...prevTokens.slice(prevTokens.length - align));
}
return tokensToStr(resultTokens);
}

function incrementRangeValue(value: string): string {
if (value === null) {
return value;
}
const tokens = tokenize(value);
const lastToken = tokens[tokens.length - 1];
if (typeof lastToken.val === 'number') {
lastToken.val += 1;
return tokensToStr(tokens);
return coerceRangeValue(value, tokensToStr(tokens));
}
return value;
}
Expand Down Expand Up @@ -517,27 +512,40 @@ function autoExtendMavenRange(
return currentRepresentation;
}
}
const interval = range[nearestIntervalIdx];
if (interval.rightValue !== null) {
const newLeftValue = incrementRangeValue(interval.leftValue);
const newRightValue = coerceRangeValue(interval.rightValue, newValue);

if (
newLeftValue &&
newLeftValue === interval.rightValue &&
newLeftValue !== newRightValue
) {
interval.leftValue = newLeftValue;
const interval = range[nearestIntervalIdx];
let { leftValue, rightValue } = interval;
if (
leftValue !== null &&
rightValue !== null &&
incrementRangeValue(leftValue) === rightValue
) {
interval.leftValue = coerceRangeValue(leftValue, newValue);
interval.rightValue = incrementRangeValue(interval.leftValue);
} else if (rightValue !== null) {
if (interval.rightType === INCLUDING_POINT) {
const tokens = tokenize(rightValue);
const lastToken = tokens[tokens.length - 1];
if (typeof lastToken.val === 'number') {
interval.rightValue = coerceRangeValue(rightValue, newValue);
} else {
interval.rightValue = newValue;
}
} else {
interval.rightValue = incrementRangeValue(
coerceRangeValue(rightValue, newValue)
);
}
interval.rightValue = newRightValue;
} else {
interval.leftValue = coerceRangeValue(interval.leftValue, newValue);
} else if (leftValue !== null) {
interval.leftValue = coerceRangeValue(leftValue, newValue);
}

if (interval.leftValue && interval.rightValue) {
if (compare(interval.leftValue, interval.rightValue) !== 1) {
return rangeToStr(range);
}
return currentRepresentation;
const correctRepresentation =
compare(interval.leftValue, interval.rightValue) !== 1
? rangeToStr(range)
: null;
return correctRepresentation || currentRepresentation;
}
return rangeToStr(range);
}
Expand Down
26 changes: 13 additions & 13 deletions lib/versioning/maven/index.spec.ts
Expand Up @@ -249,27 +249,27 @@ describe('versioning/maven/compare', () => {
['[1.0.0,1.2.23]', '1.1.0', '[1.0.0,1.2.23]'],
['(,1.0]', '2.0', '(,2.0]'],
['],1.0]', '2.0', '],2.0]'],
['(,1.0)', '2.0', '(,2.0)'],
['],1.0[', '2.0', '],2.0['],
['(,1.0)', '2.0', '(,3.0)'],
['],1.0[', '2.0', '],3.0['],
['[1.0,1.2.3],[1.3,1.5)', '1.2.4', '[1.0,1.2.4],[1.3,1.5)'],
['[1.0,1.2.3],[1.3,1.5[', '1.2.4', '[1.0,1.2.4],[1.3,1.5['],
['[1.2.3,)', '1.2.4', '[1.2.4,)'],
['[1.2.3,[', '1.2.4', '[1.2.4,['],
['[1.2.3,]', '1.2.4', '[1.2.3,]'], // invalid range
['[0.21,0.22)', '0.20.21', '[0.21,0.22)'],
['[0.21,0.22)', '0.20.21', '[0.20,0.21)'],
['[0.21,0.22)', '0.21.1', '[0.21,0.22)'],
['[0.21,0.22.0)', '0.22.1', '[0.21,0.22.1)'],
['[0.21,0.22)', '0.23', '[0.22,0.23)'],
['[0.21,0.22.0)', '0.22.1', '[0.21,0.22.2)'],
['[0.21,0.22)', '0.23', '[0.23,0.24)'],

['[1.8,1.9)', '1.9.0.1', '[1.8,1.9)'],
['[1.8a,1.9)', '1.9.0.1', '[1.8a,1.9)'],
['[1.8,1.9.0)', '1.9.0.1', '[1.8,1.9.0)'],
['[1.8,1.9.0.0)', '1.9.0.1', '[1.8,1.9.0.1)'],
['[1.8,1.9.0.0)', '1.10.1', '[1.8,1.10.1.0)'],
['[1.8,1.9)', '1.9.0.1', '[1.9,1.10)'],
['[1.8a,1.9)', '1.9.0.1', '[1.8a,1.10)'],
['[1.8,1.9.0)', '1.9.0.1', '[1.8,1.10.0)'],
['[1.8,1.9.0.0)', '1.9.0.1', '[1.8,1.9.0.2)'],
['[1.8,1.9.0.0)', '1.10.1', '[1.8,1.10.2.0)'],

['[1.8,1.9)', '1.9.1', '[1.8,1.9)'],
['[1.8,1.9)', '1.10.0', '[1.9,1.10)'],
['[1.8,1.9)', '1.10.1', '[1.9,1.10)'],
['[1.8,1.9)', '1.9.1', '[1.9,1.10)'],
['[1.8,1.9)', '1.10.0', '[1.10,1.11)'],
['[1.8,1.9)', '1.10.1', '[1.10,1.11)'],

['(,1.0.0]', '2.0.0', '(,2.0.0]'],
['(,1.0]', '2.0.0', '(,2.0]'],
Expand Down

0 comments on commit c0de13d

Please sign in to comment.