fix(npm): Fix replace strategy edge-case for carets (#9106)

This commit is contained in:
renovate-testing 2021-03-21 02:19:59 +03:00 committed by GitHub
parent e5c92e4cd9
commit 3ff8649e9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 12 deletions

View file

@ -84,6 +84,16 @@ describe('semver.getNewValue()', () => {
['^1.2.3', '4.0.0', '^4.0.0'], ['^1.2.3', '4.0.0', '^4.0.0'],
['^1.2.3', '4.5.6', '^4.0.0'], ['^1.2.3', '4.5.6', '^4.0.0'],
['^1.0.0', '4.5.6', '^4.0.0'], ['^1.0.0', '4.5.6', '^4.0.0'],
['^0.2.3', '0.2.4', '^0.2.3'],
['^2.3.0', '2.4.0', '^2.3.0'],
['^2.3.4', '2.4.5', '^2.3.4'],
['^0.0.1', '0.0.2', '^0.0.2'],
['^1.0.1', '2.0.2', '^2.0.0'],
['^1.2.3', '1.2.3', '^1.2.3'],
['^1.2.3', '1.2.2', '^1.2.2'],
['^0.9.21', '0.9.22', '^0.9.21'], // #4762
].forEach(([currentValue, newVersion, expectedValue]) => { ].forEach(([currentValue, newVersion, expectedValue]) => {
expect( expect(
semver.getNewValue({ semver.getNewValue({

View file

@ -11,6 +11,47 @@ import { parseRange } from 'semver-utils';
import { logger } from '../../logger'; import { logger } from '../../logger';
import type { NewValueConfig } from '../types'; import type { NewValueConfig } from '../types';
function replaceCaretValue(oldValue: string, newValue: string): string {
const toVersionMajor = major(newValue);
const toVersionMinor = minor(newValue);
const toVersionPatch = patch(newValue);
const currentMajor = major(oldValue);
const currentMinor = minor(oldValue);
const currentPatch = patch(oldValue);
const oldTuple = [currentMajor, currentMinor, currentPatch];
const newTuple = [toVersionMajor, toVersionMinor, toVersionPatch];
const resultTuple = [];
let leadingZero = true;
let needReplace = false;
for (let idx = 0; idx < 3; idx += 1) {
const oldVal = oldTuple[idx];
const newVal = newTuple[idx];
let leadingDigit = false;
if (oldVal !== 0 || newVal !== 0) {
if (leadingZero) {
leadingZero = false;
leadingDigit = true;
}
}
if (leadingDigit && newVal > oldVal) {
needReplace = true;
}
if (!needReplace && newVal < oldVal) {
return newValue;
}
resultTuple.push(leadingDigit ? newVal : 0);
}
return needReplace ? resultTuple.join('.') : oldValue;
}
export function getNewValue({ export function getNewValue({
currentValue, currentValue,
rangeStrategy, rangeStrategy,
@ -150,16 +191,7 @@ export function getNewValue({
if (suffix.length || !currentVersion) { if (suffix.length || !currentVersion) {
return `^${toVersionMajor}.${toVersionMinor}.${toVersionPatch}${suffix}`; return `^${toVersionMajor}.${toVersionMinor}.${toVersionPatch}${suffix}`;
} }
if (toVersionMajor === major(currentVersion)) { return `^${replaceCaretValue(currentVersion, newVersion)}`;
if (toVersionMajor === 0) {
if (toVersionMinor === 0) {
return `^${newVersion}`;
}
return `^${toVersionMajor}.${toVersionMinor}.0`;
}
return `^${newVersion}`;
}
return `^${toVersionMajor}.0.0`;
} }
if (element.operator === '=') { if (element.operator === '=') {
return `=${newVersion}`; return `=${newVersion}`;

View file

@ -420,9 +420,9 @@ describe(getName(__filename), () => {
currentValue: '^1.0.0', currentValue: '^1.0.0',
rangeStrategy: 'replace', rangeStrategy: 'replace',
currentVersion: '1.0.0', currentVersion: '1.0.0',
newVersion: '1.0.7', newVersion: '1.2.3',
}) })
).toEqual('^1.0.7'); ).toEqual('^1.0.0');
}); });
it('bumps short tilde', () => { it('bumps short tilde', () => {
expect( expect(