mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-12 06:56:24 +00:00
fix(npm): Fix replace strategy edge-case for carets (#9106)
This commit is contained in:
parent
e5c92e4cd9
commit
3ff8649e9a
3 changed files with 54 additions and 12 deletions
|
@ -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({
|
||||||
|
|
|
@ -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}`;
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in a new issue