Skip to content
Snippets Groups Projects
Unverified Commit 60af784e authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(versioning/composer): Enable strict null checks (#14027)

parent e57d2fa3
No related branches found
No related tags found
No related merge requests found
...@@ -90,51 +90,79 @@ function composer2npm(input: string): string { ...@@ -90,51 +90,79 @@ function composer2npm(input: string): string {
return output + stability; return output + stability;
} }
const equals = (a: string, b: string): boolean => function equals(a: string, b: string): boolean {
npm.equals(composer2npm(a), composer2npm(b)); return npm.equals(composer2npm(a), composer2npm(b));
}
const getMajor = (version: string): number => function getMajor(version: string): number | null {
npm.getMajor(semver.coerce(composer2npm(version))); const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getMajor(semverVersion) : null;
}
const getMinor = (version: string): number => function getMinor(version: string): number | null {
npm.getMinor(semver.coerce(composer2npm(version))); const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getMinor(semverVersion) : null;
}
const getPatch = (version: string): number => function getPatch(version: string): number | null {
npm.getPatch(semver.coerce(composer2npm(version))); const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getPatch(semverVersion) : null;
}
const isGreaterThan = (a: string, b: string): boolean => function isGreaterThan(a: string, b: string): boolean {
npm.isGreaterThan(composer2npm(a), composer2npm(b)); return npm.isGreaterThan(composer2npm(a), composer2npm(b));
}
const isLessThanRange = (version: string, range: string): boolean => function isLessThanRange(version: string, range: string): boolean {
npm.isLessThanRange(composer2npm(version), composer2npm(range)); return !!npm.isLessThanRange?.(composer2npm(version), composer2npm(range));
}
const isSingleVersion = (input: string): boolean => function isSingleVersion(input: string): boolean {
!!input && npm.isSingleVersion(composer2npm(input)); return !!input && npm.isSingleVersion(composer2npm(input));
}
const isStable = (version: string): boolean => function isStable(version: string): boolean {
version && npm.isStable(composer2npm(version)); return !!(version && npm.isStable(composer2npm(version)));
}
export const isValid = (input: string): boolean => export function isValid(input: string): boolean {
!!input && npm.isValid(composer2npm(input)); return !!input && npm.isValid(composer2npm(input));
}
export const isVersion = (input: string): boolean => export function isVersion(input: string): boolean {
!!input && npm.isVersion(composer2npm(input)); return !!input && npm.isVersion(composer2npm(input));
}
const matches = (version: string, range: string): boolean => function matches(version: string, range: string): boolean {
npm.matches(composer2npm(version), composer2npm(range)); return npm.matches(composer2npm(version), composer2npm(range));
}
const getSatisfyingVersion = (versions: string[], range: string): string => function getSatisfyingVersion(
npm.getSatisfyingVersion(versions.map(composer2npm), composer2npm(range)); versions: string[],
range: string
): string | null {
return npm.getSatisfyingVersion(
versions.map(composer2npm),
composer2npm(range)
);
}
const minSatisfyingVersion = (versions: string[], range: string): string => function minSatisfyingVersion(
npm.minSatisfyingVersion(versions.map(composer2npm), composer2npm(range)); versions: string[],
range: string
): string | null {
return npm.minSatisfyingVersion(
versions.map(composer2npm),
composer2npm(range)
);
}
function getNewValue({ function getNewValue({
currentValue, currentValue,
rangeStrategy, rangeStrategy,
currentVersion, currentVersion,
newVersion, newVersion,
}: NewValueConfig): string { }: NewValueConfig): string | null {
if (rangeStrategy === 'pin') { if (rangeStrategy === 'pin') {
return newVersion; return newVersion;
} }
...@@ -149,9 +177,10 @@ function getNewValue({ ...@@ -149,9 +177,10 @@ function getNewValue({
newVersion, newVersion,
}); });
} }
const currentMajor = currentVersion ? getMajor(currentVersion) : null;
const toMajor = getMajor(newVersion); const toMajor = getMajor(newVersion);
const toMinor = getMinor(newVersion); const toMinor = getMinor(newVersion);
let newValue: string; let newValue: string | null = null;
if (isVersion(currentValue)) { if (isVersion(currentValue)) {
newValue = newVersion; newValue = newVersion;
} else if (regEx(/^[~^](0\.[1-9][0-9]*)$/).test(currentValue)) { } else if (regEx(/^[~^](0\.[1-9][0-9]*)$/).test(currentValue)) {
...@@ -166,15 +195,19 @@ function getNewValue({ ...@@ -166,15 +195,19 @@ function getNewValue({
// handle ~4 case // handle ~4 case
const operator = currentValue.substr(0, 1); const operator = currentValue.substr(0, 1);
newValue = `${operator}${toMajor}`; 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); const operator = currentValue.substr(0, 1);
// handle ~4.1 case // handle ~4.1 case
if (currentVersion && toMajor > getMajor(currentVersion)) { if ((currentMajor && toMajor > currentMajor) || !toMinor) {
newValue = `${operator}${toMajor}.0`; newValue = `${operator}${toMajor}.0`;
} else { } else {
newValue = `${operator}${toMajor}.${toMinor}`; newValue = `${operator}${toMajor}.${toMinor}`;
} }
} else if ( } else if (
currentVersion &&
npm.isVersion(padZeroes(normalizeVersion(newVersion))) && npm.isVersion(padZeroes(normalizeVersion(newVersion))) &&
npm.isValid(normalizeVersion(currentValue)) && npm.isValid(normalizeVersion(currentValue)) &&
composer2npm(currentValue) === normalizeVersion(currentValue) composer2npm(currentValue) === normalizeVersion(currentValue)
...@@ -192,18 +225,17 @@ function getNewValue({ ...@@ -192,18 +225,17 @@ function getNewValue({
} else { } else {
const hasOr = currentValue.includes(' || '); const hasOr = currentValue.includes(' || ');
if (hasOr || rangeStrategy === 'widen') { if (hasOr || rangeStrategy === 'widen') {
const lastValue = hasOr const splitValues = currentValue.split('||');
? currentValue.split('||').pop().trim() const lastValue = splitValues[splitValues.length - 1];
: currentValue;
const replacementValue = getNewValue({ const replacementValue = getNewValue({
currentValue: lastValue, currentValue: lastValue.trim(),
rangeStrategy: 'replace', rangeStrategy: 'replace',
currentVersion, currentVersion,
newVersion, newVersion,
}); });
if (rangeStrategy === 'replace') { if (rangeStrategy === 'replace') {
newValue = replacementValue; newValue = replacementValue;
} else { } else if (replacementValue) {
const parsedRange = parseRange(replacementValue); const parsedRange = parseRange(replacementValue);
const element = parsedRange[parsedRange.length - 1]; const element = parsedRange[parsedRange.length - 1];
if (element.operator?.startsWith('<')) { if (element.operator?.startsWith('<')) {
......
...@@ -389,7 +389,6 @@ ...@@ -389,7 +389,6 @@
"lib/versioning/api.ts", "lib/versioning/api.ts",
"lib/versioning/aws-machine-image/index.ts", "lib/versioning/aws-machine-image/index.ts",
"lib/versioning/common.ts", "lib/versioning/common.ts",
"lib/versioning/composer/index.ts",
"lib/versioning/generic.ts", "lib/versioning/generic.ts",
"lib/versioning/git/index.ts", "lib/versioning/git/index.ts",
"lib/versioning/hashicorp/index.ts", "lib/versioning/hashicorp/index.ts",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment