From a81a8c4c466f510215a51cef1bb08544d11844fe Mon Sep 17 00:00:00 2001 From: --get Date: Tue, 2 Jul 2019 01:50:39 +0200 Subject: [PATCH] install: improve isOnly(Dev,Optional) Instead of creating a new set each time a new node gets visited, so that its siblings do not have it in `seen`, just remove the node from the original set right after all child nodes are visited. See #76 Credit: @larsgw PR-URL: https://github.com/npm/cli/pull/206 Close: #206 Reviewed-by: @isaacs --- lib/install/is-only-dev.js | 5 +++-- lib/install/is-only-optional.js | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/install/is-only-dev.js b/lib/install/is-only-dev.js index 2877c61a227d0..804497393eaa3 100644 --- a/lib/install/is-only-dev.js +++ b/lib/install/is-only-dev.js @@ -28,9 +28,10 @@ function andIsOnlyDev (name, seen) { return isDev && !isProd } else { if (seen.has(req)) return true - seen = new Set(seen) seen.add(req) - return isOnlyDev(req, seen) + const result = isOnlyDev(req, seen) + seen.delete(req) + return result } } } diff --git a/lib/install/is-only-optional.js b/lib/install/is-only-optional.js index 81e227bae7a89..ea27eadcfa809 100644 --- a/lib/install/is-only-optional.js +++ b/lib/install/is-only-optional.js @@ -11,11 +11,12 @@ function isOptional (node, seen) { if (seen.has(node) || node.requiredBy.length === 0) { return false } - seen = new Set(seen) seen.add(node) const swOptional = node.fromShrinkwrap && node.package._optional - return node.requiredBy.every(function (req) { + const result = node.requiredBy.every(function (req) { if (req.fakeChild && swOptional) return true return isOptDep(req, moduleName(node)) || isOptional(req, seen) }) + seen.delete(node) + return result }