Skip to content

Commit

Permalink
Merge pull request #42 from snyk/fix/simplify-repetition-counter
Browse files Browse the repository at this point in the history
fix: simplify package repetition counter
  • Loading branch information
kyegupov authored May 2, 2019
2 parents 3a08d62 + c73bbae commit 400cfcb
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function recursivelyBuildPkgTree(
node,
lockedVersions,
projectRootPath,
counts,
totalPackageOccurenceCounter: CountDict,
): DepTree {

const isRoot = (node.Name === VIRTUAL_ROOT_NODE_ID);
Expand All @@ -251,14 +251,12 @@ function recursivelyBuildPkgTree(
pkg.version = lockedVersions[pkg.name].version;
}

pkg._counts = {};

const children = graph.successors(node.Name).sort();
children.forEach((depName) => {

// We drop branches of overly common pkgs:
// this looses some paths, but avoids explosion in result size
if ((counts[depName] || 0) + (pkg._counts[depName] || 0) > 10) {
if ((totalPackageOccurenceCounter[depName] || 0) > 10) {
return;
}

Expand All @@ -269,12 +267,9 @@ function recursivelyBuildPkgTree(
dep,
lockedVersions,
projectRootPath,
sumCounts(counts, pkg._counts),
totalPackageOccurenceCounter,
);

pkg._counts = sumCounts(pkg._counts, child._counts);
delete child._counts;

if (child._isProjSubpkg) {
Object.keys(child.dependencies!).forEach((grandChildName) => {
// don't merge grandchild if already a child,
Expand All @@ -287,24 +282,14 @@ function recursivelyBuildPkgTree(
// in case was already added via a grandchild
if (!pkg.dependencies![child.name]) {
pkg.dependencies![child.name] = child;
pkg._counts[child.name] = (pkg._counts[child.name] || 0) + 1;
totalPackageOccurenceCounter[child.name] = (totalPackageOccurenceCounter[child.name] || 0) + 1;
}
}
});

return pkg;
}

function sumCounts(a: CountDict, b: CountDict): CountDict {
const sum = {...a};

for (const k of Object.keys(b)) {
sum[k] = (sum[k] || 0) + b[k];
}

return sum;
}

function isProjSubpackage(pkgPath, projectRootPath) {
if (pkgPath === projectRootPath) {
return true;
Expand All @@ -326,8 +311,6 @@ function isProjSubpackage(pkgPath, projectRootPath) {
return true;
}

// TODO(kyegupov): the part below will move to snyk-go-parser

interface LockedDep {
name: string;
version: string;
Expand Down

0 comments on commit 400cfcb

Please sign in to comment.