Skip to content

Commit

Permalink
fix(versioning): strip v prefix when bumping semver ranges (#24357)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Sep 12, 2023
1 parent 496bc19 commit 7d4de49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/modules/versioning/npm/index.spec.ts
Expand Up @@ -131,6 +131,7 @@ describe('modules/versioning/npm/index', () => {
${'^2.3.4'} | ${'replace'} | ${'2.3.4'} | ${'2.3.5'} | ${'^2.3.4'}
${'~2.3.4'} | ${'replace'} | ${'2.3.4'} | ${'2.3.5'} | ${'~2.3.0'}
${'^0.0.1'} | ${'replace'} | ${'0.0.1'} | ${'0.0.2'} | ${'^0.0.2'}
${'^0.0.1'} | ${'replace'} | ${'v0.0.1'} | ${'v0.0.2'} | ${'^0.0.2'}
${'^1.0.1'} | ${'replace'} | ${'1.0.1'} | ${'2.0.2'} | ${'^2.0.0'}
${'^1.2.3'} | ${'replace'} | ${'1.2.3'} | ${'1.2.3'} | ${'^1.2.3'}
${'^1.2.3'} | ${'replace'} | ${'1.2.3'} | ${'1.2.2'} | ${'^1.2.2'}
Expand All @@ -143,13 +144,15 @@ describe('modules/versioning/npm/index', () => {
${'1.x >2.0.0'} | ${'widen'} | ${'1.0.0'} | ${'2.1.0'} | ${null}
${'^1.0.0'} | ${'bump'} | ${'1.0.0'} | ${'2.0.0'} | ${'^2.0.0'}
${'~1.0.0'} | ${'bump'} | ${'1.0.0'} | ${'2.0.0'} | ${'~2.0.0'}
${'~1.0.0'} | ${'bump'} | ${'v1.0.0'} | ${'v2.0.0'} | ${'~2.0.0'}
${'>1.0.0'} | ${'bump'} | ${'1.0.0'} | ${'2.1.0'} | ${null}
${'^1.0.0-alpha'} | ${'replace'} | ${'1.0.0-alpha'} | ${'1.0.0-beta'} | ${'^1.0.0-beta'}
${'~1.0.0'} | ${'replace'} | ${'1.0.0'} | ${'1.1.0'} | ${'~1.1.0'}
${'1.0.x'} | ${'replace'} | ${'1.0.0'} | ${'1.1.0'} | ${'1.1.x'}
${'<=1.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.0'} | ${'<=1.2'}
${'<=1'} | ${'replace'} | ${'1.0.0'} | ${'2.0.0'} | ${'<=2'}
${'<= 1'} | ${'replace'} | ${'1.0.0'} | ${'2.0.0'} | ${'<= 2'}
${'>=18.17.0'} | ${'bump'} | ${'v18.17.0'} | ${'v18.17.1'} | ${'>=18.17.1'}
`(
'getNewValue("$currentValue", "$rangeStrategy", "$currentVersion", "$newVersion") === "$expected"',
({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
Expand Down
18 changes: 11 additions & 7 deletions lib/modules/versioning/npm/range.ts
Expand Up @@ -57,6 +57,10 @@ function replaceCaretValue(oldValue: string, newValue: string): string {
return needReplace ? resultTuple.join('.') : oldValue;
}

function stripV(value: string): string {
return value.replace(/^v/, '');
}

// TODO: #22198
export function getNewValue({
currentValue,
Expand Down Expand Up @@ -136,18 +140,18 @@ export function getNewValue({
});
}
if (element.operator === '^') {
return `^${newVersion}`;
return `^${stripV(newVersion)}`;
}
if (element.operator === '~') {
return `~${newVersion}`;
return `~${stripV(newVersion)}`;
}
if (element.operator === '=') {
return `=${newVersion}`;
return `=${stripV(newVersion)}`;
}
if (element.operator === '>=') {
return currentValue.includes('>= ')
? `>= ${newVersion}`
: `>=${newVersion}`;
? `>= ${stripV(newVersion)}`
: `>=${stripV(newVersion)}`;
}
if (element.operator.startsWith('<')) {
return currentValue;
Expand Down Expand Up @@ -193,7 +197,7 @@ export function getNewValue({
return `^${replaceCaretValue(currentVersion, newVersion)}`;
}
if (element.operator === '=') {
return `=${newVersion}`;
return `=${stripV(newVersion)}`;
}
if (element.operator === '~') {
if (suffix.length) {
Expand All @@ -204,7 +208,7 @@ export function getNewValue({
if (element.operator === '<=') {
let res;
if (!!element.patch || suffix.length) {
res = `<=${newVersion}`;
res = `<=${stripV(newVersion)}`;
} else if (element.minor) {
res = `<=${toVersionMajor}.${toVersionMinor}`;
} else {
Expand Down

0 comments on commit 7d4de49

Please sign in to comment.