From 31172392f3a56678466d1bc2a6d6dd237b928da4 Mon Sep 17 00:00:00 2001 From: "Kyle E. Mitchell" Date: Tue, 23 Aug 2022 08:27:18 -0700 Subject: [PATCH] Refactor findIssues used to recurse, but no longer has to. --- index.js | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index eecfa99..9ccdd7b 100644 --- a/index.js +++ b/index.js @@ -36,11 +36,11 @@ function licensee (configuration, path, callback) { var arborist = new Arborist({ path }) arborist.loadActual({ forceActual: true }) .then(function (tree) { - var children = Array.from(tree.children.values()) + var dependencies = Array.from(tree.children.values()) if (configuration.filterPackages) { - children = configuration.filterPackages(children) + dependencies = configuration.filterPackages(dependencies) } - callback(null, findIssues(configuration, children, [])) + callback(null, findIssues(configuration, dependencies)) }) .catch(function (error) { return callback(error) @@ -73,33 +73,32 @@ function isString (argument) { return typeof argument === 'string' } -function findIssues (configuration, children, results) { - if (children) { - children.forEach(function (child) { - if ( - configuration.productionOnly && - child.dev - ) return - var result = resultForPackage(configuration, child) - // Deduplicate. - var existing = results.find(function (existing) { - return ( - existing.name === result.name && - existing.version === result.version - ) - }) - if (existing) { - if (existing.duplicates) { - existing.duplicates.push(result) - } else { - existing.duplicates = [result] - } +function findIssues (configuration, dependencies) { + var results = [] + dependencies.forEach(function (dependency) { + if ( + configuration.productionOnly && + dependency.dev + ) return + var result = resultForPackage(configuration, dependency) + // Deduplicate. + var existing = results.find(function (existing) { + return ( + existing.name === result.name && + existing.version === result.version + ) + }) + if (existing) { + if (existing.duplicates) { + existing.duplicates.push(result) } else { - results.push(result) + existing.duplicates = [result] } - }) - return results - } else return results + } else { + results.push(result) + } + }) + return results } function resultForPackage (configuration, tree) {