Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONO-136 fix semver range check #7

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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