Skip to content

Commit

Permalink
refactor(hex): ensure strict null check (#13394)
Browse files Browse the repository at this point in the history
* added hex to strictNullCheck files

* enabled strict null check in versioning/hex

* modified npm/range
  • Loading branch information
RahulGautamSingh committed Jan 6, 2022
1 parent 692f4b7 commit fdb55ae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/versioning/hex/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('versioning/hex/index', () => {
`(
'isLessThanRange($version, $range) === $expected',
({ version, range, expected }) => {
expect(hexScheme.isLessThanRange(version, range)).toBe(expected);
expect(hexScheme.isLessThanRange?.(version, range)).toBe(expected);
}
);

Expand Down
65 changes: 38 additions & 27 deletions lib/versioning/hex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,63 @@ function npm2hex(input: string): string {
return output;
}

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

const isValid = (input: string): string | boolean =>
npm.isValid(hex2npm(input));
const isValid = (input: string): boolean => !!npm.isValid(hex2npm(input));

const matches = (version: string, range: string): boolean =>
npm.matches(hex2npm(version), hex2npm(range));

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

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

const getNewValue = ({
function getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
}: NewValueConfig): string => {
}: NewValueConfig): string | null {
let newSemver = npm.getNewValue({
currentValue: hex2npm(currentValue),
rangeStrategy,
currentVersion,
newVersion,
});
newSemver = npm2hex(newSemver);
if (regEx(/~>\s*(\d+\.\d+\.\d+)$/).test(currentValue)) {
newSemver = newSemver.replace(
regEx(/[\^~]\s*(\d+\.\d+\.\d+)/),
(_str, p1: string) => `~> ${p1}`
);
} else if (regEx(/~>\s*(\d+\.\d+)$/).test(currentValue)) {
newSemver = newSemver.replace(
regEx(/\^\s*(\d+\.\d+)(\.\d+)?/),
(_str, p1: string) => `~> ${p1}`
);
} else {
newSemver = newSemver.replace(regEx(/~\s*(\d+\.\d+\.\d)/), '~> $1');
}
if (npm.isVersion(newSemver)) {
newSemver = `== ${newSemver}`;
if (newSemver) {
newSemver = npm2hex(newSemver);

if (regEx(/~>\s*(\d+\.\d+\.\d+)$/).test(currentValue)) {
newSemver = newSemver.replace(
regEx(/[\^~]\s*(\d+\.\d+\.\d+)/),
(_str, p1: string) => `~> ${p1}`
);
} else if (regEx(/~>\s*(\d+\.\d+)$/).test(currentValue)) {
newSemver = newSemver.replace(
regEx(/\^\s*(\d+\.\d+)(\.\d+)?/),
(_str, p1: string) => `~> ${p1}`
);
} else {
newSemver = newSemver.replace(regEx(/~\s*(\d+\.\d+\.\d)/), '~> $1');
}
if (npm.isVersion(newSemver)) {
newSemver = `== ${newSemver}`;
}
}
return newSemver;
};
}

export { isValid };

Expand Down
1 change: 1 addition & 0 deletions tsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"lib/util/url.ts",
"lib/versioning/gradle/compare.ts",
"lib/versioning/maven/**/*.ts",
"lib/versioning/hex/**/*.ts",
"lib/versioning/node/**/*.ts",
"lib/versioning/npm/**/*.ts",
"lib/versioning/ruby/**/*.ts",
Expand Down

0 comments on commit fdb55ae

Please sign in to comment.