From 692f4b72ae5781e245f5f964368e40c36ffe343a Mon Sep 17 00:00:00 2001 From: RahulGautamSingh <rahultesnik@gmail.com> Date: Fri, 7 Jan 2022 01:18:52 +0545 Subject: [PATCH] refactor: ensure strict null check node,npm (#13404) * add strictNullCheck for versioning/npm,node * refactor(npm): ensure strict null * refactor(node): ensure strict null * refactor: added variable to handle null error * refactor: revert last commit instead use non-null assertion --- lib/versioning/node/index.spec.ts | 2 +- lib/versioning/node/index.ts | 7 +++-- lib/versioning/node/schedule.ts | 3 +- lib/versioning/npm/index.ts | 13 +++++--- lib/versioning/npm/range.ts | 52 +++++++++++++++++-------------- tsconfig.strict.json | 2 ++ 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/lib/versioning/node/index.spec.ts b/lib/versioning/node/index.spec.ts index f3fb338371..6c4225b9dc 100644 --- a/lib/versioning/node/index.spec.ts +++ b/lib/versioning/node/index.spec.ts @@ -47,7 +47,7 @@ describe('versioning/node/index', () => { ${'10.0.0a'} | ${t1} | ${false} ${'9.0.0'} | ${t1} | ${false} `('isStable("$version") === $expected', ({ version, time, expected }) => { - DateTime.local = (...args) => + DateTime.local = (...args: (string | any)[]) => args.length ? dtLocal.apply(DateTime, args) : time; expect(isStable(version as string)).toBe(expected); }); diff --git a/lib/versioning/node/index.ts b/lib/versioning/node/index.ts index 97aa87c307..39f6483d32 100644 --- a/lib/versioning/node/index.ts +++ b/lib/versioning/node/index.ts @@ -1,4 +1,5 @@ import { DateTime } from 'luxon'; +import { valid } from 'semver'; import npm, { isValid, isVersion } from '../npm'; import type { NewValueConfig, VersioningApi } from '../types'; import { nodeSchedule } from './schedule'; @@ -13,16 +14,16 @@ function getNewValue({ rangeStrategy, currentVersion, newVersion, -}: NewValueConfig): string { +}: NewValueConfig): string | null { const res = npm.getNewValue({ currentValue, rangeStrategy, currentVersion, newVersion, }); - if (isVersion(res)) { + if (res && isVersion(res)) { // normalize out any 'v' prefix - return isVersion(res); + return valid(res); } return res; } diff --git a/lib/versioning/node/schedule.ts b/lib/versioning/node/schedule.ts index 7514ceb564..cbf3f5a2d5 100644 --- a/lib/versioning/node/schedule.ts +++ b/lib/versioning/node/schedule.ts @@ -11,5 +11,6 @@ interface NodeJsSchedule { export type NodeJsData = Record<string, NodeJsSchedule>; export const nodeSchedule: NodeJsData = JSON.parse( - dataFiles.get('data/node-js-schedule.json') + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + dataFiles.get('data/node-js-schedule.json')! ); diff --git a/lib/versioning/npm/index.ts b/lib/versioning/npm/index.ts index 32a6c15782..9736b893dc 100644 --- a/lib/versioning/npm/index.ts +++ b/lib/versioning/npm/index.ts @@ -30,12 +30,15 @@ const { } = semver; // If this is left as an alias, inputs like "17.04.0" throw errors -export const isValid = (input: string): string => validRange(input); -export const isVersion = (input: string): string => valid(input); +export const isValid = (input: string): boolean => !!validRange(input); +export const isVersion = (input: string): boolean => !!valid(input); -const isSingleVersion = (constraint: string): string => - isVersion(constraint) || - (constraint?.startsWith('=') && isVersion(constraint.substring(1).trim())); +function isSingleVersion(constraint: string): boolean { + return ( + isVersion(constraint) || + (constraint?.startsWith('=') && isVersion(constraint.substring(1).trim())) + ); +} export const api: VersioningApi = { equals, diff --git a/lib/versioning/npm/range.ts b/lib/versioning/npm/range.ts index 71638bf871..5c0ad05652 100644 --- a/lib/versioning/npm/range.ts +++ b/lib/versioning/npm/range.ts @@ -1,3 +1,4 @@ +import is from '@sindresorhus/is'; import semver from 'semver'; import semverUtils from 'semver-utils'; import { logger } from '../../logger'; @@ -60,7 +61,7 @@ export function getNewValue({ rangeStrategy, currentVersion, newVersion, -}: NewValueConfig): string { +}: NewValueConfig): string | null { if (rangeStrategy === 'pin' || isVersion(currentValue)) { return newVersion; } @@ -110,9 +111,8 @@ export function getNewValue({ const toVersionMajor = major(newVersion); const toVersionMinor = minor(newVersion); const toVersionPatch = patch(newVersion); - const suffix = prerelease(newVersion) - ? '-' + String(prerelease(newVersion)[0]) - : ''; + const toNewVersion = prerelease(newVersion); + const suffix = toNewVersion ? `-${toNewVersion[0]}` : ''; // Simple range if (rangeStrategy === 'bump') { if (parsedRange.length === 1) { @@ -166,26 +166,30 @@ export function getNewValue({ return currentValue; } } else { - const newRange = semverUtils.parseRange(currentValue); - const versions = newRange.map((x) => { - const subRange = x.semver; - const bumpedSubRange = getNewValue({ - currentValue: subRange, - rangeStrategy: 'bump', - currentVersion, - newVersion, - }); - if (satisfies(newVersion, bumpedSubRange)) { - return bumpedSubRange; - } - return getNewValue({ - currentValue: subRange, - rangeStrategy: 'replace', - currentVersion, - newVersion, - }); - }); - return versions.filter((x) => x !== null && x !== '').join(' '); + return semverUtils + .parseRange(currentValue) + .map((x) => x.semver) + .filter(is.string) + .map((subRange) => { + const bumpedSubRange = getNewValue({ + currentValue: subRange, + rangeStrategy: 'bump', + currentVersion, + newVersion, + }); + if (bumpedSubRange && satisfies(newVersion, bumpedSubRange)) { + return bumpedSubRange; + } + + return getNewValue({ + currentValue: subRange, + rangeStrategy: 'replace', + currentVersion, + newVersion, + }); + }) + .filter((x) => x !== null && x !== '') + .join(' '); } logger.debug( 'Unsupported range type for rangeStrategy=bump: ' + currentValue diff --git a/tsconfig.strict.json b/tsconfig.strict.json index e1883cb404..852d522237 100644 --- a/tsconfig.strict.json +++ b/tsconfig.strict.json @@ -70,6 +70,8 @@ "lib/util/url.ts", "lib/versioning/gradle/compare.ts", "lib/versioning/maven/**/*.ts", + "lib/versioning/node/**/*.ts", + "lib/versioning/npm/**/*.ts", "lib/versioning/ruby/**/*.ts", "lib/versioning/semver-coerced/**/*.ts", "lib/versioning/semver/**/*.ts", -- GitLab