Skip to content

Commit

Permalink
Merge pull request #7 from atlassian-forks/MONO-136-fix-semver-range-…
Browse files Browse the repository at this point in the history
…check

MONO-136 fix semver range check
  • Loading branch information
Blasz authored Jun 13, 2023
2 parents 50c6a8e + 4ac0631 commit 19c91de
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/cli/src/checks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function weakMemoize<Arg, Ret>(func: (arg: Arg) => Ret): (arg: Arg) => Ret {
export let getMostCommonRangeMap = weakMemoize(function getMostCommonRanges(
allPackages: Map<string, Package>
) {
let dependencyRangesMapping = new Map<string, {[key: string]: number}>();
let dependencyRangesMapping = new Map<string, { [key: string]: number }>();

for (let [pkgName, pkg] of allPackages) {
for (let depType of NORMAL_DEPENDENCY_TYPES) {
Expand Down Expand Up @@ -155,21 +155,21 @@ export let getMostCommonRangeMap = weakMemoize(function getMostCommonRanges(

const [first] = specifierMapEntryArray;
const maxValue = specifierMapEntryArray.reduce((acc, value) => {
if(acc[1] === value[1]) {
if (acc[1] === value[1]) {
// If all dependency ranges occurances are equal, pick the highest.
// It's impossible to infer intention of the developer
// when all ranges occur an equal amount of times
const highestRange = highest([acc[0], value[0]]);
return [ highestRange, acc[1] ];
return [highestRange, acc[1]];
}

if(acc[1] > value[1]) {
if (acc[1] > value[1]) {
return acc;
}
return value;
}, first);

mostCommonRangeMap.set(depName, maxValue[0])
mostCommonRangeMap.set(depName, maxValue[0]);
}
return mostCommonRangeMap;
});
Expand Down Expand Up @@ -207,12 +207,9 @@ export function getClosestAllowedRange(
}

function getVersionFromRange(range: string) {
if (semver.parse(range)) {
return range;
}
const version = range.substring(1);
if (semver.parse(version)) {
return version;
const minVersion = semver.minVersion(range);
if (minVersion) {
return minVersion;
}
logger.error(`Invalid range: ${range}`);
throw new ExitError(1);
Expand Down

0 comments on commit 19c91de

Please sign in to comment.