Skip to content

Commit

Permalink
refactor: refactor static regex out of for loops (renovatebot#13065)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Dec 29, 2021
1 parent 05d7c45 commit 4f65b57
Show file tree
Hide file tree
Showing 55 changed files with 149 additions and 159 deletions.
4 changes: 2 additions & 2 deletions lib/config/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export async function decryptConfig(
}
logger.debug(`Decrypted ${eKey}`);
if (eKey === 'npmToken') {
const token = decryptedStr.replace(regEx(/\n$/), ''); // TODO #12071
const token = decryptedStr.replace(regEx(/\n$/), '');
add(token);
logger.debug(
{ decryptedToken: maskToken(token) },
Expand All @@ -191,7 +191,7 @@ export async function decryptConfig(
} else {
logger.debug('Appending _authToken= to end of existing npmrc');
decryptedConfig.npmrc = decryptedConfig.npmrc.replace(
regEx(/\n?$/), // TODO #12071
regEx(/\n?$/),
`\n_authToken=${token}\n`
);
}
Expand Down
35 changes: 14 additions & 21 deletions lib/config/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import type {
import { mergeChildConfig } from './utils';

const options = getOptions();
export function fixShortHours(input: string): string {
return input.replace(regEx(/( \d?\d)((a|p)m)/g), '$1:00$2');
}

let optionTypes: Record<string, RenovateOptions['type']>;

// Returns a migrated config
export function migrateConfig(
config: RenovateConfig,
Expand Down Expand Up @@ -157,12 +159,12 @@ export function migrateConfig(
}
} else if (is.string(val) && val.includes('{{baseDir}}')) {
migratedConfig[key] = val.replace(
regEx(/{{baseDir}}/g), // TODO #12071
regEx(/{{baseDir}}/g),
'{{packageFileDir}}'
);
} else if (is.string(val) && val.includes('{{depNameShort}}')) {
migratedConfig[key] = val.replace(
regEx(/{{depNameShort}}/g), // TODO #12071
regEx(/{{depNameShort}}/g),
'{{depName}}'
);
} else if (key === 'gitFs') {
Expand Down Expand Up @@ -340,6 +342,9 @@ export function migrateConfig(
const schedules = is.string(val) ? [val] : [...(val as string[])];
// split 'and'
const schedulesLength = schedules.length;
const afterBeforeRe = regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
);
for (let i = 0; i < schedulesLength; i += 1) {
if (
schedules[i].includes(' and ') &&
Expand All @@ -348,28 +353,16 @@ export function migrateConfig(
) {
const parsedSchedule = later.parse.text(
// We need to massage short hours first before we can parse it
schedules[i].replace(regEx(/( \d?\d)((a|p)m)/g), '$1:00$2') // TODO #12071
fixShortHours(schedules[i])
).schedules[0];
// Only migrate if the after time is greater than before, e.g. "after 10pm and before 5am"
if (parsedSchedule?.t_a?.[0] > parsedSchedule?.t_b?.[0]) {
const toSplit = schedules[i];
schedules[i] = toSplit
.replace(
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
), // TODO #12071
'$1$2 $3 $7'
)
.replace(afterBeforeRe, '$1$2 $3 $7')
.trim();
schedules.push(
toSplit
.replace(
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
), // TODO #12071
'$1$4 $5 $7'
)
.trim()
toSplit.replace(afterBeforeRe, '$1$4 $5 $7').trim()
);
}
}
Expand All @@ -393,10 +386,10 @@ export function migrateConfig(
if (
regEx(/every (mon|tues|wednes|thurs|fri|satur|sun)day$/).test(
schedules[i]
) // TODO #12071
)
) {
schedules[i] = schedules[i].replace(
regEx(/every ([a-z]*day)$/), // TODO #12071
regEx(/every ([a-z]*day)$/),
'on $1'
);
}
Expand Down Expand Up @@ -514,7 +507,7 @@ export function migrateConfig(
if (is.string(migratedConfig[key])) {
for (const [from, to] of Object.entries(migratedTemplates)) {
migratedConfig[key] = (migratedConfig[key] as string).replace(
regEx(from, 'g'), // TODO #12071
regEx(from, 'g'),
to
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const ignoredNodes = [
'prBody', // deprecated
'minimumConfidence', // undocumented feature flag
];

const tzRe = regEx(/^:timezone\((.+)\)$/);
const rulesRe = regEx(/p.*Rules\[\d+\]$/);
function isManagerPath(parentPath: string): boolean {
return (
regEx(/^regexManagers\[[0-9]+]$/).test(parentPath) ||
Expand Down Expand Up @@ -253,7 +254,6 @@ export async function validateConfig(
}
}
if (key === 'extends') {
const tzRe = regEx(/^:timezone\((.+)\)$/); // TODO #12071
for (const subval of val) {
if (is.string(subval)) {
if (
Expand Down Expand Up @@ -488,7 +488,7 @@ export async function validateConfig(
}
if (
(selectors.includes(key) || key === 'matchCurrentVersion') &&
!regEx(/p.*Rules\[\d+\]$/).test(parentPath) && // Inside a packageRule // TODO #12071
!rulesRe.test(parentPath) && // Inside a packageRule
(parentPath || !isPreset) // top level in a preset
) {
errors.push({
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/artifactory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ export class ArtifactoryDatasource extends Datasource {
}

private static parseReleaseTimestamp(rawText: string): string {
return rawText.trim().replace(regEx(/ ?-$/), '') + 'Z'; // TODO #12071
return rawText.trim().replace(regEx(/ ?-$/), '') + 'Z';
}
}
2 changes: 1 addition & 1 deletion lib/datasource/github-releases/digest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function findDigestFile(
for (const asset of smallAssets) {
const res = await http.get(asset.browser_download_url);
for (const line of res.body.split('\n')) {
const [lineDigest, lineFn] = line.split(regEx(/\s+/), 2); // TODO #12071
const [lineDigest, lineFn] = line.split(regEx(/\s+/), 2);
if (lineDigest === digest) {
return {
assetName: asset.name,
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/maven/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { XmlDocument } from 'xmldoc';
import { logger } from '../../logger';
import * as packageCache from '../../util/cache/package';
import { regEx } from '../../util/regex';
import { ensureTrailingSlash } from '../../util/url';
import mavenVersion from '../../versioning/maven';
import * as mavenVersioning from '../../versioning/maven';
import { compare } from '../../versioning/maven/compare';
Expand Down Expand Up @@ -306,7 +307,7 @@ export async function getReleases({
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const dependency = getDependencyParts(lookupName);
const repoUrl = registryUrl.replace(/\/?$/, '/'); // TODO #12071
const repoUrl = ensureTrailingSlash(registryUrl);

logger.debug(`Looking up ${dependency.display} in repository ${repoUrl}`);

Expand Down
1 change: 0 additions & 1 deletion lib/datasource/pod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ async function getReleasesFromCDN(
const line = lines[idx];
const [name, ...versions] = line.split('/');
if (name === lookupName.replace(regEx(/\/.*$/), '')) {
// TODO #12071
const releases = versions.map((version) => ({ version }));
return { releases };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/pypi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class PypiDatasource extends Datasource {
const srcPrefix = `${packageName}-`;
const srcSuffix = '.tar.gz';
if (srcText.startsWith(srcPrefix) && srcText.endsWith(srcSuffix)) {
return srcText.replace(srcPrefix, '').replace(regEx(/\.tar\.gz$/), ''); // TODO #12071
return srcText.replace(srcPrefix, '').replace(regEx(/\.tar\.gz$/), '');
}

// pep-0427 wheel packages
Expand Down
8 changes: 4 additions & 4 deletions lib/datasource/sbt-package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ export async function getUrls(
const sourceUrl = pomXml.valueWithPath('scm.url');
if (sourceUrl) {
result.sourceUrl = sourceUrl
.replace(regEx(/^scm:/), '') // TODO #12071
.replace(regEx(/^git:/), '') // TODO #12071
.replace(regEx(/^[email protected]:/), 'https://github.com/') // TODO #12071
.replace(regEx(/\.git$/), ''); // TODO #12071
.replace(regEx(/^scm:/), '')
.replace(regEx(/^git:/), '')
.replace(regEx(/^[email protected]:/), 'https://github.com/')
.replace(regEx(/\.git$/), '');
}

return result;
Expand Down
3 changes: 1 addition & 2 deletions lib/datasource/sbt-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '../../logger';
import { regEx } from '../../util/regex';
import { ensureTrailingSlash } from '../../util/url';
import * as ivyVersioning from '../../versioning/ivy';
import { compare } from '../../versioning/maven/compare';
import { downloadHttpProtocol } from '../maven/util';
Expand All @@ -18,8 +19,6 @@ export const defaultRegistryUrls = [SBT_PLUGINS_REPO];
export const defaultVersioning = ivyVersioning.id;
export const registryStrategy = 'hunt';

const ensureTrailingSlash = (str: string): string => str.replace(/\/?$/, '/'); // TODO #12071

async function resolvePluginReleases(
rootUrl: string,
artifact: string,
Expand Down
2 changes: 0 additions & 2 deletions lib/manager/ansible-galaxy/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ export default function extractPackageFile(
// find role and collection block
lines.forEach((line, index) => {
if (regEx(/^collections:/).exec(line)) {
// TODO #12071
positions.collections = index;
}
if (regEx(/^roles:/).exec(line)) {
// TODO #12071
positions.roles = index;
}
});
Expand Down
6 changes: 3 additions & 3 deletions lib/manager/buildkite/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function extractPackageFile(content: string): PackageFile | null {
const line = lines[lineIdx];
const pluginsSection = regEx(
/^(?<pluginsIndent>\s*)(-?\s*)plugins:/
).exec(line); // TODO #12071
).exec(line);
if (pluginsSection) {
logger.trace(`Matched plugins on line ${lineNumber}`);
isPluginsSection = true;
Expand All @@ -25,10 +25,10 @@ export function extractPackageFile(content: string): PackageFile | null {
logger.debug(`serviceImageLine: "${line}"`);
const { currentIndent } = regEx(/^(?<currentIndent>\s*)/).exec(
line
).groups; // TODO #12071
).groups;
const depLineMatch = regEx(
/^\s+(?:-\s+)?(?<depName>[^#]+)#(?<currentValue>[^:]+)/
).exec(line); // TODO #12071
).exec(line);
if (currentIndent.length <= pluginsIndent.length) {
isPluginsSection = false;
pluginsIndent = '';
Expand Down
29 changes: 16 additions & 13 deletions lib/manager/bundler/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { regEx } from '../../util/regex';
import type { PackageDependency, PackageFile } from '../types';
import { extractLockFileEntries } from './locked-version';

function formatContent(input: string): string {
return input.replace(regEx(/^ {2}/), '') + '\n'; //remove leading witespace and add a new line at the end
}

export async function extractPackageFile(
content: string,
fileName?: string
Expand All @@ -22,7 +26,6 @@ export async function extractPackageFile(
sourceMatch =
sourceMatch ||
regEx(`^source ${delimiter}([^${delimiter}]+)${delimiter}\\s*$`).exec(
// TODO #12071
line
);
}
Expand All @@ -33,14 +36,14 @@ export async function extractPackageFile(
for (const delimiter of delimiters) {
rubyMatch =
rubyMatch ||
regEx(`^ruby ${delimiter}([^${delimiter}]+)${delimiter}`).exec(line); // TODO #12071
regEx(`^ruby ${delimiter}([^${delimiter}]+)${delimiter}`).exec(line);
}
if (rubyMatch) {
res.constraints = { ruby: rubyMatch[1] };
}
const gemMatchRegex = regEx(
`^\\s*gem\\s+(['"])(?<depName>[^'"]+)(['"])(\\s*,\\s*(?<currentValue>(['"])[^'"]+['"](\\s*,\\s*['"][^'"]+['"])?))?`
); // TODO #12071
);
const gemMatch = gemMatchRegex.exec(line);
if (gemMatch) {
const dep: PackageDependency = {
Expand All @@ -49,27 +52,27 @@ export async function extractPackageFile(
};
if (gemMatch.groups.currentValue) {
const currentValue = gemMatch.groups.currentValue;
dep.currentValue = regEx(/\s*,\s*/).test(currentValue) // TODO #12071
dep.currentValue = regEx(/\s*,\s*/).test(currentValue)
? currentValue
: currentValue.slice(1, -1);
}
dep.datasource = RubyGemsDatasource.id;
res.deps.push(dep);
}
const groupMatch = regEx(/^group\s+(.*?)\s+do/).exec(line); // TODO #12071
const groupMatch = regEx(/^group\s+(.*?)\s+do/).exec(line);
if (groupMatch) {
const depTypes = groupMatch[1]
.split(',')
.map((group) => group.trim())
.map((group) => group.replace(regEx(/^:/), '')); // TODO #12071
.map((group) => group.replace(regEx(/^:/), ''));
const groupLineNumber = lineNumber;
let groupContent = '';
let groupLine = '';
while (lineNumber < lines.length && groupLine !== 'end') {
lineNumber += 1;
groupLine = lines[lineNumber];
if (groupLine !== 'end') {
groupContent += (groupLine || '').replace(regEx(/^ {2}/), '') + '\n'; // TODO #12071
groupContent += formatContent(groupLine || '');
}
}
const groupRes = await extractPackageFile(groupContent);
Expand All @@ -88,7 +91,7 @@ export async function extractPackageFile(
}
for (const delimiter of delimiters) {
const sourceBlockMatch = regEx(
`^source\\s+${delimiter}(.*?)${delimiter}\\s+do` // TODO #12071
`^source\\s+${delimiter}(.*?)${delimiter}\\s+do`
).exec(line);
if (sourceBlockMatch) {
const repositoryUrl = sourceBlockMatch[1];
Expand All @@ -104,7 +107,7 @@ export async function extractPackageFile(
sourceLine = 'end';
}
if (sourceLine !== 'end') {
sourceContent += sourceLine.replace(regEx(/^ {2}/), '') + '\n'; // TODO #12071
sourceContent += formatContent(sourceLine);
}
}
const sourceRes = await extractPackageFile(sourceContent);
Expand All @@ -122,7 +125,7 @@ export async function extractPackageFile(
}
}
}
const platformsMatch = regEx(/^platforms\s+(.*?)\s+do/).test(line); // TODO #12071
const platformsMatch = regEx(/^platforms\s+(.*?)\s+do/).test(line);
if (platformsMatch) {
const platformsLineNumber = lineNumber;
let platformsContent = '';
Expand All @@ -131,7 +134,7 @@ export async function extractPackageFile(
lineNumber += 1;
platformsLine = lines[lineNumber];
if (platformsLine !== 'end') {
platformsContent += platformsLine.replace(regEx(/^ {2}/), '') + '\n'; // TODO #12071
platformsContent += formatContent(platformsLine);
}
}
const platformsRes = await extractPackageFile(platformsContent);
Expand All @@ -147,7 +150,7 @@ export async function extractPackageFile(
);
}
}
const ifMatch = regEx(/^if\s+(.*?)/).test(line); // TODO #12071
const ifMatch = regEx(/^if\s+(.*?)/).test(line);
if (ifMatch) {
const ifLineNumber = lineNumber;
let ifContent = '';
Expand All @@ -156,7 +159,7 @@ export async function extractPackageFile(
lineNumber += 1;
ifLine = lines[lineNumber];
if (ifLine !== 'end') {
ifContent += ifLine.replace(regEx(/^ {2}/), '') + '\n'; // TODO #12071
ifContent += formatContent(ifLine);
}
}
const ifRes = await extractPackageFile(ifContent);
Expand Down
Loading

0 comments on commit 4f65b57

Please sign in to comment.