Skip to content

Commit

Permalink
refactor(versioning/composer): Enable strict null checks (#14027)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 6, 2022
1 parent e57d2fa commit 60af784
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
102 changes: 67 additions & 35 deletions lib/versioning/composer/index.ts
Expand Up @@ -90,51 +90,79 @@ function composer2npm(input: string): string {
return output + stability;
}

const equals = (a: string, b: string): boolean =>
npm.equals(composer2npm(a), composer2npm(b));
function equals(a: string, b: string): boolean {
return npm.equals(composer2npm(a), composer2npm(b));
}

const getMajor = (version: string): number =>
npm.getMajor(semver.coerce(composer2npm(version)));
function getMajor(version: string): number | null {
const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getMajor(semverVersion) : null;
}

const getMinor = (version: string): number =>
npm.getMinor(semver.coerce(composer2npm(version)));
function getMinor(version: string): number | null {
const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getMinor(semverVersion) : null;
}

const getPatch = (version: string): number =>
npm.getPatch(semver.coerce(composer2npm(version)));
function getPatch(version: string): number | null {
const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getPatch(semverVersion) : null;
}

const isGreaterThan = (a: string, b: string): boolean =>
npm.isGreaterThan(composer2npm(a), composer2npm(b));
function isGreaterThan(a: string, b: string): boolean {
return npm.isGreaterThan(composer2npm(a), composer2npm(b));
}

const isLessThanRange = (version: string, range: string): boolean =>
npm.isLessThanRange(composer2npm(version), composer2npm(range));
function isLessThanRange(version: string, range: string): boolean {
return !!npm.isLessThanRange?.(composer2npm(version), composer2npm(range));
}

const isSingleVersion = (input: string): boolean =>
!!input && npm.isSingleVersion(composer2npm(input));
function isSingleVersion(input: string): boolean {
return !!input && npm.isSingleVersion(composer2npm(input));
}

const isStable = (version: string): boolean =>
version && npm.isStable(composer2npm(version));
function isStable(version: string): boolean {
return !!(version && npm.isStable(composer2npm(version)));
}

export const isValid = (input: string): boolean =>
!!input && npm.isValid(composer2npm(input));
export function isValid(input: string): boolean {
return !!input && npm.isValid(composer2npm(input));
}

export const isVersion = (input: string): boolean =>
!!input && npm.isVersion(composer2npm(input));
export function isVersion(input: string): boolean {
return !!input && npm.isVersion(composer2npm(input));
}

const matches = (version: string, range: string): boolean =>
npm.matches(composer2npm(version), composer2npm(range));
function matches(version: string, range: string): boolean {
return npm.matches(composer2npm(version), composer2npm(range));
}

const getSatisfyingVersion = (versions: string[], range: string): string =>
npm.getSatisfyingVersion(versions.map(composer2npm), composer2npm(range));
function getSatisfyingVersion(
versions: string[],
range: string
): string | null {
return npm.getSatisfyingVersion(
versions.map(composer2npm),
composer2npm(range)
);
}

const minSatisfyingVersion = (versions: string[], range: string): string =>
npm.minSatisfyingVersion(versions.map(composer2npm), composer2npm(range));
function minSatisfyingVersion(
versions: string[],
range: string
): string | null {
return npm.minSatisfyingVersion(
versions.map(composer2npm),
composer2npm(range)
);
}

function getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
}: NewValueConfig): string {
}: NewValueConfig): string | null {
if (rangeStrategy === 'pin') {
return newVersion;
}
Expand All @@ -149,9 +177,10 @@ function getNewValue({
newVersion,
});
}
const currentMajor = currentVersion ? getMajor(currentVersion) : null;
const toMajor = getMajor(newVersion);
const toMinor = getMinor(newVersion);
let newValue: string;
let newValue: string | null = null;
if (isVersion(currentValue)) {
newValue = newVersion;
} else if (regEx(/^[~^](0\.[1-9][0-9]*)$/).test(currentValue)) {
Expand All @@ -166,15 +195,19 @@ function getNewValue({
// handle ~4 case
const operator = currentValue.substr(0, 1);
newValue = `${operator}${toMajor}`;
} else if (regEx(/^[~^]([0-9]*(?:\.[0-9]*)?)$/).test(currentValue)) {
} else if (
toMajor &&
regEx(/^[~^]([0-9]*(?:\.[0-9]*)?)$/).test(currentValue)
) {
const operator = currentValue.substr(0, 1);
// handle ~4.1 case
if (currentVersion && toMajor > getMajor(currentVersion)) {
if ((currentMajor && toMajor > currentMajor) || !toMinor) {
newValue = `${operator}${toMajor}.0`;
} else {
newValue = `${operator}${toMajor}.${toMinor}`;
}
} else if (
currentVersion &&
npm.isVersion(padZeroes(normalizeVersion(newVersion))) &&
npm.isValid(normalizeVersion(currentValue)) &&
composer2npm(currentValue) === normalizeVersion(currentValue)
Expand All @@ -192,18 +225,17 @@ function getNewValue({
} else {
const hasOr = currentValue.includes(' || ');
if (hasOr || rangeStrategy === 'widen') {
const lastValue = hasOr
? currentValue.split('||').pop().trim()
: currentValue;
const splitValues = currentValue.split('||');
const lastValue = splitValues[splitValues.length - 1];
const replacementValue = getNewValue({
currentValue: lastValue,
currentValue: lastValue.trim(),
rangeStrategy: 'replace',
currentVersion,
newVersion,
});
if (rangeStrategy === 'replace') {
newValue = replacementValue;
} else {
} else if (replacementValue) {
const parsedRange = parseRange(replacementValue);
const element = parsedRange[parsedRange.length - 1];
if (element.operator?.startsWith('<')) {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.strict.json
Expand Up @@ -389,7 +389,6 @@
"lib/versioning/api.ts",
"lib/versioning/aws-machine-image/index.ts",
"lib/versioning/common.ts",
"lib/versioning/composer/index.ts",
"lib/versioning/generic.ts",
"lib/versioning/git/index.ts",
"lib/versioning/hashicorp/index.ts",
Expand Down

0 comments on commit 60af784

Please sign in to comment.