diff --git a/lib/util/package-rules.ts b/lib/util/package-rules.ts index aa0af30dec9b883135e2fbb9165e32dccd07649a..c112ee21ba84af0ba947ae612901033d9731698f 100644 --- a/lib/util/package-rules.ts +++ b/lib/util/package-rules.ts @@ -5,7 +5,7 @@ import { mergeChildConfig } from '../config'; import type { PackageRule, PackageRuleInputConfig } from '../config/types'; import { logger } from '../logger'; import * as allVersioning from '../versioning'; -import { configRegexPredicate, isConfigRegex, regEx } from './regex'; +import { configRegexPredicate, regEx } from './regex'; function matchesRule( inputConfig: PackageRuleInputConfig, @@ -205,9 +205,11 @@ function matchesRule( if (matchCurrentVersion) { const version = allVersioning.get(versioning); const matchCurrentVersionStr = matchCurrentVersion.toString(); - if (isConfigRegex(matchCurrentVersionStr)) { - const matches = configRegexPredicate(matchCurrentVersionStr); - if (!unconstrainedValue && !matches(currentValue)) { + const matchCurrentVersionPred = configRegexPredicate( + matchCurrentVersionStr + ); + if (matchCurrentVersionPred) { + if (!unconstrainedValue && !matchCurrentVersionPred(currentValue)) { return false; } positiveMatch = true; diff --git a/lib/util/regex.ts b/lib/util/regex.ts index fe3479c444b21a31357cf9ca93584c8498df2d35..a59bf7b0ccf8926f37701082c86692e50ab3da17 100644 --- a/lib/util/regex.ts +++ b/lib/util/regex.ts @@ -22,8 +22,9 @@ try { export function regEx(pattern: string | RegExp, flags?: string): RegExp { const key = `${pattern.toString()}:${flags}`; - if (cache.has(key)) { - return cache.get(key); + const cachedResult = cache.get(key); + if (cachedResult) { + return cachedResult; } try { @@ -63,16 +64,20 @@ function parseConfigRegex(input: string): RegExp | null { return null; } -type ConfigRegexPredicate = (string) => boolean; +type ConfigRegexPredicate = (s: string) => boolean; -export function configRegexPredicate(input: string): ConfigRegexPredicate { - const configRegex = parseConfigRegex(input); - if (configRegex) { - const isPositive = !input.startsWith('!'); - return (x: string): boolean => { - const res = configRegex.test(x); - return isPositive ? res : !res; - }; +export function configRegexPredicate( + input: string +): ConfigRegexPredicate | null { + if (isConfigRegex(input)) { + const configRegex = parseConfigRegex(input); + if (configRegex) { + const isPositive = !input.startsWith('!'); + return (x: string): boolean => { + const res = configRegex.test(x); + return isPositive ? res : !res; + }; + } } return null; } diff --git a/lib/workers/repository/process/lookup/filter.ts b/lib/workers/repository/process/lookup/filter.ts index 60c290a637dfeb49e2a1c46af73bfb2060061164..74730e8fff129021ea4f768a4e604c4aa4a29bd7 100644 --- a/lib/workers/repository/process/lookup/filter.ts +++ b/lib/workers/repository/process/lookup/filter.ts @@ -2,7 +2,7 @@ import * as semver from 'semver'; import { CONFIG_VALIDATION } from '../../../../constants/error-messages'; import type { Release } from '../../../../datasource/types'; import { logger } from '../../../../logger'; -import { configRegexPredicate, isConfigRegex } from '../../../../util/regex'; +import { configRegexPredicate } from '../../../../util/regex'; import type { VersioningApi } from '../../../../versioning'; import * as npmVersioning from '../../../../versioning/npm'; import * as pep440 from '../../../../versioning/pep440'; @@ -60,10 +60,10 @@ export function filterVersions( } if (allowedVersions) { - if (isConfigRegex(allowedVersions)) { - const isAllowed = configRegexPredicate(allowedVersions); + const isAllowedPred = configRegexPredicate(allowedVersions); + if (isAllowedPred) { filteredVersions = filteredVersions.filter(({ version }) => - isAllowed(version) + isAllowedPred(version) ); } else if (versioning.isValid(allowedVersions)) { filteredVersions = filteredVersions.filter((v) => diff --git a/tsconfig.strict.json b/tsconfig.strict.json index 250c37b2050abfdb47587df254d4d0d9e1dbd102..546c9d9a867d18c3b8ec7900998d74ad983dd165 100644 --- a/tsconfig.strict.json +++ b/tsconfig.strict.json @@ -127,6 +127,8 @@ "./lib/util/mask.spec.ts", "./lib/util/mask.ts", "./lib/util/object.ts", + "./lib/util/regex.spec.ts", + "./lib/util/regex.ts", "./lib/util/sanitize.ts", "./lib/util/split.ts", "./lib/workers/pr/changelog/hbs-template.ts",