From 03311165503644f0f124337d6d405615cef164f0 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh <rahultesnik@gmail.com> Date: Wed, 5 Jan 2022 21:18:37 +0545 Subject: [PATCH] refactor(ruby): ensure strict null check (#13278) Co-authored-by: Rhys Arkins <rhys@arkins.net> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> --- lib/versioning/ruby/index.spec.ts | 6 +++--- lib/versioning/ruby/index.ts | 22 +++++++++++++++------- lib/versioning/ruby/range.ts | 8 ++++---- lib/versioning/ruby/strategies/replace.ts | 3 ++- lib/versioning/types.ts | 2 +- tsconfig.strict.json | 3 +-- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/versioning/ruby/index.spec.ts b/lib/versioning/ruby/index.spec.ts index 3e2c9af76f..40829c1026 100644 --- a/lib/versioning/ruby/index.spec.ts +++ b/lib/versioning/ruby/index.spec.ts @@ -174,12 +174,12 @@ describe('versioning/ruby/index', () => { ${'2.0.0'} | ${'>= 1.1.5, < 2.0'} | ${false} ${'1.2.0-beta'} | ${'1.2.0-alpha'} | ${false} ${'2.0.0'} | ${'> 1.2.2, ~> 2.0.0'} | ${false} - ${'asdf'} | ${'> 1.2.2, ~> 2.0.0'} | ${null} - ${null} | ${'> 1.2.2, ~> 2.0.0'} | ${null} + ${'asdf'} | ${'> 1.2.2, ~> 2.0.0'} | ${false} + ${null} | ${'> 1.2.2, ~> 2.0.0'} | ${false} `( 'isLessThanRange("$version", "$range") === "$expected"', ({ version, range, expected }) => { - expect(semverRuby.isLessThanRange(version, range)).toBe(expected); + expect(semverRuby.isLessThanRange?.(version, range)).toBe(expected); } ); diff --git a/lib/versioning/ruby/index.ts b/lib/versioning/ruby/index.ts index e0566af075..b2218b6873 100644 --- a/lib/versioning/ruby/index.ts +++ b/lib/versioning/ruby/index.ts @@ -45,7 +45,7 @@ export const isVersion = (version: string): boolean => !!valid(vtrim(version)); const isGreaterThan = (left: string, right: string): boolean => gt(vtrim(left), vtrim(right)); const isLessThanRange = (version: string, range: string): boolean => - ltr(vtrim(version), vtrim(range)); + !!ltr(vtrim(version), vtrim(range)); const isSingleVersion = (range: string): boolean => { const { version, operator } = parseRange(vtrim(range)); @@ -74,17 +74,25 @@ export const isValid = (input: string): boolean => export const matches = (version: string, range: string): boolean => satisfies(vtrim(version), vtrim(range)); -const getSatisfyingVersion = (versions: string[], range: string): string => - maxSatisfying(versions.map(vtrim), vtrim(range)); -const minSatisfyingVersion = (versions: string[], range: string): string => - minSatisfying(versions.map(vtrim), vtrim(range)); +function getSatisfyingVersion( + versions: string[], + range: string +): string | null { + return maxSatisfying(versions.map(vtrim), vtrim(range)); +} +function minSatisfyingVersion( + versions: string[], + range: string +): string | null { + return minSatisfying(versions.map(vtrim), vtrim(range)); +} const getNewValue = ({ currentValue, rangeStrategy, currentVersion, newVersion, -}: NewValueConfig): string => { +}: NewValueConfig): string | null => { let newValue = null; if (isVersion(currentValue)) { newValue = currentValue.startsWith('v') ? 'v' + newVersion : newVersion; @@ -123,7 +131,7 @@ const getNewValue = ({ logger.warn(`Unsupported strategy ${rangeStrategy}`); } } - if (regEx(/^('|")/).exec(currentValue)) { + if (newValue && regEx(/^('|")/).exec(currentValue)) { const delimiter = currentValue[0]; return newValue .split(',') diff --git a/lib/versioning/ruby/range.ts b/lib/versioning/ruby/range.ts index c6e8b0d72c..8230d1d7fe 100644 --- a/lib/versioning/ruby/range.ts +++ b/lib/versioning/ruby/range.ts @@ -18,14 +18,14 @@ const parse = (range: string): Range => { const value = (range || '').trim(); const match = regExp.exec(value); - if (match) { - const { version = null, operator = null, delimiter = ' ' } = match.groups; + if (match?.groups) { + const { version = '', operator = '', delimiter = ' ' } = match.groups; return { version, operator, delimiter }; } return { - version: null, - operator: null, + version: '', + operator: '', delimiter: ' ', }; }; diff --git a/lib/versioning/ruby/strategies/replace.ts b/lib/versioning/ruby/strategies/replace.ts index 848fba0f37..6b37aa27ef 100644 --- a/lib/versioning/ruby/strategies/replace.ts +++ b/lib/versioning/ruby/strategies/replace.ts @@ -71,7 +71,8 @@ export default ({ to, range }: { range: string; to: string }): string => { const lastPart = range .split(',') .map((part) => part.trim()) - .pop(); + .slice(-1) + .join(); const lastPartPrecision = lastPart.split('.').length; const toPrecision = to.split('.').length; let massagedTo: string = to; diff --git a/lib/versioning/types.ts b/lib/versioning/types.ts index 0e046086ff..cb2091c9f7 100644 --- a/lib/versioning/types.ts +++ b/lib/versioning/types.ts @@ -26,7 +26,7 @@ export interface VersioningApi { isLessThanRange?(version: string, range: string): boolean; getSatisfyingVersion(versions: string[], range: string): string | null; minSatisfyingVersion(versions: string[], range: string): string | null; - getNewValue(newValueConfig: NewValueConfig): string; + getNewValue(newValueConfig: NewValueConfig): string | null; sortVersions(version: string, other: string): number; matches(version: string, range: string | Range): boolean; diff --git a/tsconfig.strict.json b/tsconfig.strict.json index 1cda9093b7..2008407999 100644 --- a/tsconfig.strict.json +++ b/tsconfig.strict.json @@ -60,8 +60,7 @@ "lib/util/split.ts", "lib/util/url.ts", "lib/versioning/maven/**/*.ts", - "lib/versioning/ruby/operator.ts", - "lib/versioning/ruby/version.ts", + "lib/versioning/ruby/**/*.ts", "lib/versioning/semver-coerced/**/*.ts", "lib/versioning/semver/**/*.ts", "lib/versioning/swift/**/*.ts", -- GitLab