Skip to content

Commit

Permalink
fix(npm): Fix replace strategy edge-case for carets (#9106)
Browse files Browse the repository at this point in the history
  • Loading branch information
renovate-testing committed Mar 20, 2021
1 parent e5c92e4 commit 3ff8649
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
10 changes: 10 additions & 0 deletions lib/versioning/npm/index.spec.ts
Expand Up @@ -84,6 +84,16 @@ describe('semver.getNewValue()', () => {
['^1.2.3', '4.0.0', '^4.0.0'],
['^1.2.3', '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]) => {
expect(
semver.getNewValue({
Expand Down
52 changes: 42 additions & 10 deletions lib/versioning/npm/range.ts
Expand Up @@ -11,6 +11,47 @@ import { parseRange } from 'semver-utils';
import { logger } from '../../logger';
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({
currentValue,
rangeStrategy,
Expand Down Expand Up @@ -150,16 +191,7 @@ export function getNewValue({
if (suffix.length || !currentVersion) {
return `^${toVersionMajor}.${toVersionMinor}.${toVersionPatch}${suffix}`;
}
if (toVersionMajor === major(currentVersion)) {
if (toVersionMajor === 0) {
if (toVersionMinor === 0) {
return `^${newVersion}`;
}
return `^${toVersionMajor}.${toVersionMinor}.0`;
}
return `^${newVersion}`;
}
return `^${toVersionMajor}.0.0`;
return `^${replaceCaretValue(currentVersion, newVersion)}`;
}
if (element.operator === '=') {
return `=${newVersion}`;
Expand Down
4 changes: 2 additions & 2 deletions lib/versioning/poetry/index.spec.ts
Expand Up @@ -420,9 +420,9 @@ describe(getName(__filename), () => {
currentValue: '^1.0.0',
rangeStrategy: 'replace',
currentVersion: '1.0.0',
newVersion: '1.0.7',
newVersion: '1.2.3',
})
).toEqual('^1.0.7');
).toEqual('^1.0.0');
});
it('bumps short tilde', () => {
expect(
Expand Down

0 comments on commit 3ff8649

Please sign in to comment.