Skip to content

Commit

Permalink
fix(versioning/composer): support range versions normalization (#20558)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <[email protected]>
Co-authored-by: Michael Kriese <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2023
1 parent c7254ab commit 27eda56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
8 changes: 8 additions & 0 deletions lib/modules/versioning/composer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ describe('modules/versioning/composer/index', () => {
${'1.2.3'} | ${true}
${'2.5'} | ${true}
${'v2.5'} | ${true}
${'^1.0|^2.0'} | ${true}
${'^1.0 | ^2.0'} | ${true}
${'^1.0||^2.0'} | ${true}
${'^1.0 || ^2.0'} | ${true}
${'~1.0|~2.0'} | ${true}
${'~1.0 | ~2.0'} | ${true}
${'~1.0||~2.0'} | ${true}
${'~1.0 || ~2.0'} | ${true}
`('isValid("$version") === $expected', ({ version, expected }) => {
const res = !!semver.isValid(version);
expect(res).toBe(expected);
Expand Down
49 changes: 27 additions & 22 deletions lib/modules/versioning/composer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,33 @@ function normalizeVersion(input: string): string {
}

function composer2npm(input: string): string {
const cleanInput = normalizeVersion(input);
if (npm.isVersion(cleanInput)) {
return cleanInput;
}
if (npm.isVersion(padZeroes(cleanInput))) {
return padZeroes(cleanInput);
}
const [versionId, stability] = getVersionParts(cleanInput);
let output = versionId;

// ~4 to ^4 and ~4.1 to ^4.1
output = output.replace(
regEx(/(?:^|\s)~([1-9][0-9]*(?:\.[0-9]*)?)(?: |$)/g),
'^$1'
);
// ~0.4 to >=0.4 <1
output = output.replace(
regEx(/(?:^|\s)~(0\.[1-9][0-9]*)(?: |$)/g),
'>=$1 <1'
);

return output + stability;
return input
.split(regEx(/\s*\|\|?\s*/g))
.map((part): string => {
const cleanInput = normalizeVersion(part);
if (npm.isVersion(cleanInput)) {
return cleanInput;
}
if (npm.isVersion(padZeroes(cleanInput))) {
return padZeroes(cleanInput);
}
const [versionId, stability] = getVersionParts(cleanInput);
let output = versionId;

// ~4 to ^4 and ~4.1 to ^4.1
output = output.replace(
regEx(/(?:^|\s)~([1-9][0-9]*(?:\.[0-9]*)?)(?: |$)/g),
'^$1'
);
// ~0.4 to >=0.4 <1
output = output.replace(
regEx(/(?:^|\s)~(0\.[1-9][0-9]*)(?: |$)/g),
'>=$1 <1'
);

return output + stability;
})
.join(' || ');
}

function equals(a: string, b: string): boolean {
Expand Down

0 comments on commit 27eda56

Please sign in to comment.