From bbe29956a01b726ef668df509858fd8279f2c16f Mon Sep 17 00:00:00 2001 From: hussainweb <hussainweb@gmail.com> Date: Fri, 1 May 2020 01:43:45 -0400 Subject: [PATCH] fix(composer): handle carets in the same way as tilde for ranges (#6093) --- lib/versioning/composer/index.spec.ts | 38 +++++++++++++++++++++++++++ lib/versioning/composer/index.ts | 37 ++++++++++++++------------ 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/lib/versioning/composer/index.spec.ts b/lib/versioning/composer/index.spec.ts index aa333abe89..95e746584f 100644 --- a/lib/versioning/composer/index.spec.ts +++ b/lib/versioning/composer/index.spec.ts @@ -127,6 +127,14 @@ describe('semver.getNewValue()', () => { toVersion: 'V1.1', }) ).toEqual('V1.1'); + expect( + semver.getNewValue({ + currentValue: '^1.0', + rangeStrategy: 'pin', + fromVersion: '1.0', + toVersion: 'V1.1', + }) + ).toEqual('V1.1'); }); it('returns toVersion', () => { expect( @@ -148,6 +156,36 @@ describe('semver.getNewValue()', () => { }) ).toEqual('^1.0'); }); + it('bumps caret to same', () => { + expect( + semver.getNewValue({ + currentValue: '^1.0.0', + rangeStrategy: 'bump', + fromVersion: '1.0.0', + toVersion: '1.3.5', + }) + ).toEqual('^1.3.5'); + }); + it('replaces caret to same', () => { + expect( + semver.getNewValue({ + currentValue: '^1', + rangeStrategy: 'replace', + fromVersion: '1.0.0', + toVersion: '1.3.5', + }) + ).toEqual('^1'); + }); + it('replaces short caret', () => { + expect( + semver.getNewValue({ + currentValue: '^1.0', + rangeStrategy: 'replace', + fromVersion: '1.0.0', + toVersion: '2.3.5', + }) + ).toEqual('^2.0'); + }); it('handles tilde zero', () => { expect( semver.getNewValue({ diff --git a/lib/versioning/composer/index.ts b/lib/versioning/composer/index.ts index cc5d0de454..c66b70561a 100644 --- a/lib/versioning/composer/index.ts +++ b/lib/versioning/composer/index.ts @@ -95,6 +95,26 @@ function getNewValue({ let newValue: string; if (isVersion(currentValue)) { newValue = toVersion; + } else if (/^[~^](0\.[1-9][0-9]*)$/.test(currentValue)) { + const operator = currentValue.substr(0, 1); + // handle ~0.4 case first + if (toMajor === 0) { + newValue = `${operator}0.${toMinor}`; + } else { + newValue = `${operator}${toMajor}.0`; + } + } else if (/^[~^]([0-9]*)$/.test(currentValue)) { + // handle ~4 case + const operator = currentValue.substr(0, 1); + newValue = `${operator}${toMajor}`; + } else if (/^[~^]([0-9]*(?:\.[0-9]*)?)$/.test(currentValue)) { + const operator = currentValue.substr(0, 1); + // handle ~4.1 case + if (fromVersion && toMajor > getMajor(fromVersion)) { + newValue = `${operator}${toMajor}.0`; + } else { + newValue = `${operator}${toMajor}.${toMinor}`; + } } else if ( npm.isVersion(padZeroes(toVersion)) && npm.isValid(currentValue) && @@ -106,23 +126,6 @@ function getNewValue({ fromVersion, toVersion: padZeroes(toVersion), }); - } else if (/^~(0\.[1-9][0-9]*)$/.test(currentValue)) { - // handle ~0.4 case first - if (toMajor === 0) { - newValue = `~0.${toMinor}`; - } else { - newValue = `~${toMajor}.0`; - } - } else if (/^~([0-9]*)$/.test(currentValue)) { - // handle ~4 case - newValue = `~${toMajor}`; - } else if (/^~([0-9]*(?:\.[0-9]*)?)$/.test(currentValue)) { - // handle ~4.1 case - if (fromVersion && toMajor > getMajor(fromVersion)) { - newValue = `~${toMajor}.0`; - } else { - newValue = `~${toMajor}.${toMinor}`; - } } if (currentValue.includes(' || ')) { const lastValue = currentValue.split('||').pop().trim(); -- GitLab