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

refactor(versioning/hashicorp): Enable strict null checks (#14030)

parent 4fa5eeb8
Branches
No related tags found
No related merge requests found
...@@ -15,8 +15,9 @@ function hashicorp2npm(input: string): string { ...@@ -15,8 +15,9 @@ function hashicorp2npm(input: string): string {
return input.replace(regEx(/~>(\s*\d+\.\d+$)/), '^$1').replace(',', ''); return input.replace(regEx(/~>(\s*\d+\.\d+$)/), '^$1').replace(',', '');
} }
const isLessThanRange = (version: string, range: string): boolean => function isLessThanRange(version: string, range: string): boolean {
npm.isLessThanRange(hashicorp2npm(version), hashicorp2npm(range)); return !!npm.isLessThanRange?.(hashicorp2npm(version), hashicorp2npm(range));
}
export const isValid = (input: string): boolean => export const isValid = (input: string): boolean =>
!!input && npm.isValid(hashicorp2npm(input)); !!input && npm.isValid(hashicorp2npm(input));
...@@ -24,29 +25,42 @@ export const isValid = (input: string): boolean => ...@@ -24,29 +25,42 @@ export const isValid = (input: string): boolean =>
const matches = (version: string, range: string): boolean => const matches = (version: string, range: string): boolean =>
npm.matches(hashicorp2npm(version), hashicorp2npm(range)); npm.matches(hashicorp2npm(version), hashicorp2npm(range));
const getSatisfyingVersion = (versions: string[], range: string): string => function getSatisfyingVersion(
npm.getSatisfyingVersion(versions.map(hashicorp2npm), hashicorp2npm(range)); versions: string[],
range: string
): string | null {
return npm.getSatisfyingVersion(
versions.map(hashicorp2npm),
hashicorp2npm(range)
);
}
const minSatisfyingVersion = (versions: string[], range: string): string => function minSatisfyingVersion(
npm.minSatisfyingVersion(versions.map(hashicorp2npm), hashicorp2npm(range)); versions: string[],
range: string
): string | null {
return npm.minSatisfyingVersion(
versions.map(hashicorp2npm),
hashicorp2npm(range)
);
}
function getNewValue({ function getNewValue({
currentValue, currentValue,
rangeStrategy, rangeStrategy,
currentVersion, currentVersion,
newVersion, newVersion,
}: NewValueConfig): string { }: NewValueConfig): string | null {
if (['replace', 'update-lockfile'].includes(rangeStrategy)) { if (['replace', 'update-lockfile'].includes(rangeStrategy)) {
if ( const minor = npm.getMinor(newVersion);
regEx(/~>\s*0\.\d+/).test(currentValue) && const major = npm.getMajor(newVersion);
npm.getMajor(newVersion) === 0 if (regEx(/~>\s*0\.\d+/).test(currentValue) && major === 0 && minor) {
) {
const testFullVersion = regEx(/(~>\s*0\.)(\d+)\.\d$/); const testFullVersion = regEx(/(~>\s*0\.)(\d+)\.\d$/);
let replaceValue = ''; let replaceValue = '';
if (testFullVersion.test(currentValue)) { if (testFullVersion.test(currentValue)) {
replaceValue = `$<prefix>${npm.getMinor(newVersion)}.0`; replaceValue = `$<prefix>${minor}.0`;
} else { } else {
replaceValue = `$<prefix>${npm.getMinor(newVersion)}$<suffix>`; replaceValue = `$<prefix>${minor}$<suffix>`;
} }
return currentValue.replace( return currentValue.replace(
regEx(`(?<prefix>~>\\s*0\\.)\\d+(?<suffix>.*)$`), regEx(`(?<prefix>~>\\s*0\\.)\\d+(?<suffix>.*)$`),
...@@ -54,10 +68,10 @@ function getNewValue({ ...@@ -54,10 +68,10 @@ function getNewValue({
); );
} }
// handle special ~> 1.2 case // handle special ~> 1.2 case
if (regEx(/(~>\s*)\d+\.\d+$/).test(currentValue)) { if (major && regEx(/(~>\s*)\d+\.\d+$/).test(currentValue)) {
return currentValue.replace( return currentValue.replace(
regEx(`(?<prefix>~>\\s*)\\d+\\.\\d+$`), regEx(`(?<prefix>~>\\s*)\\d+\\.\\d+$`),
`$<prefix>${npm.getMajor(newVersion)}.0` `$<prefix>${major}.0`
); );
} }
} }
......
...@@ -386,11 +386,7 @@ ...@@ -386,11 +386,7 @@
"lib/util/merge-confidence/index.ts", "lib/util/merge-confidence/index.ts",
"lib/util/package-rules.ts", "lib/util/package-rules.ts",
"lib/versioning/api.ts", "lib/versioning/api.ts",
"lib/versioning/aws-machine-image/index.ts",
"lib/versioning/common.ts", "lib/versioning/common.ts",
"lib/versioning/generic.ts",
"lib/versioning/git/index.ts",
"lib/versioning/hashicorp/index.ts",
"lib/versioning/helm/index.ts", "lib/versioning/helm/index.ts",
"lib/versioning/index.ts", "lib/versioning/index.ts",
"lib/versioning/poetry/index.ts", "lib/versioning/poetry/index.ts",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment