From 35fea5ea74f1bb28db54554b8f95d14c563a3e88 Mon Sep 17 00:00:00 2001 From: Brandon Cheng Date: Sat, 31 Dec 2022 02:52:23 -0500 Subject: [PATCH] fix(dependencies-hierarchy): initialize resultHeight to null In a debugger, I noticed a test package with no dependencies return a height of `0` from getTree. By the comment in `DependencyInfo`, this should instead be `null` since the dependencies array is empty. It turns out this is the case when `deps == null`. The `getTree` function early returns with a `null` height: ```ts if (deps == null) { return { dependencies: [], isPartiallyVisited: false, height: null } } ``` However, when `deps` is `{}`, `resultHeight` gets initialized to `0`. The subsequent `.forEach` loop then iterates over the non-empty object. --- reviewing/dependencies-hierarchy/src/getTree.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reviewing/dependencies-hierarchy/src/getTree.ts b/reviewing/dependencies-hierarchy/src/getTree.ts index 68e4a24ff6..1b6d312420 100644 --- a/reviewing/dependencies-hierarchy/src/getTree.ts +++ b/reviewing/dependencies-hierarchy/src/getTree.ts @@ -80,7 +80,7 @@ function getTreeHelper ( const peers = new Set(Object.keys(opts.currentPackages[parentId].peerDependencies ?? {})) const resultDependencies: PackageNode[] = [] - let resultHeight = 0 + let resultHeight: number | null = null let resultCircular: boolean = false let resultIsPartiallyVisited = false @@ -119,7 +119,7 @@ function getTreeHelper ( const children = getChildrenTree(keypath.concat([relativeId]), relativeId) dependencies = children.dependencies const heightOfCurrentDepNode = children.height == null ? 0 : children.height + 1 - resultHeight = Math.max(resultHeight, heightOfCurrentDepNode + 1) + resultHeight = Math.max(resultHeight ?? 0, heightOfCurrentDepNode + 1) resultIsPartiallyVisited = resultIsPartiallyVisited || children.isPartiallyVisited if (children.circular) {