refactor: move nested function to module scope (#17361)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
This commit is contained in:
Michael Kriese 2022-08-23 21:38:24 +02:00 committed by GitHub
parent 57ade2b33e
commit 33e3bf7ce2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,15 +20,11 @@ export const validMatchFields = [
type ValidMatchFields = typeof validMatchFields[number];
export function createDependency(
extractionTemplate: ExtractionTemplate,
config: CustomExtractConfig,
dep?: PackageDependency
): PackageDependency | null {
const dependency = dep ?? {};
const { groups, replaceString } = extractionTemplate;
function updateDependency(field: ValidMatchFields, value: string): void {
function updateDependency(
dependency: PackageDependency,
field: ValidMatchFields,
value: string
): void {
switch (field) {
case 'registryUrl':
// check if URL is valid and pack inside an array
@ -45,13 +41,21 @@ export function createDependency(
}
}
export function createDependency(
extractionTemplate: ExtractionTemplate,
config: CustomExtractConfig,
dep?: PackageDependency
): PackageDependency | null {
const dependency = dep ?? {};
const { groups, replaceString } = extractionTemplate;
for (const field of validMatchFields) {
const fieldTemplate = `${field}Template` as keyof RegexManagerTemplates;
const tmpl = config[fieldTemplate];
if (tmpl) {
try {
const compiled = template.compile(tmpl, groups, false);
updateDependency(field, compiled);
updateDependency(dependency, field, compiled);
} catch (err) {
logger.warn(
{ template: tmpl },
@ -60,7 +64,7 @@ export function createDependency(
return null;
}
} else if (groups[field]) {
updateDependency(field, groups[field]);
updateDependency(dependency, field, groups[field]);
}
}
dependency.replaceString = replaceString;