@@ -138,7 +138,12 @@ class LS extends ArboristWorkspaceCmd {
138138 const item = json
139139 ? getJsonOutputItem ( node , { global, long } )
140140 : parseable
141- ? { }
141+ ? {
142+ pkgid : node . pkgid ,
143+ path : node . path ,
144+ [ _dedupe ] : node [ _dedupe ] ,
145+ [ _parent ] : node [ _parent ] ,
146+ }
142147 : getHumanOutputItem ( node , { args, chalk, global, long } )
143148
144149 // loop through list of node problems to add them to global list
@@ -157,13 +162,11 @@ class LS extends ArboristWorkspaceCmd {
157162 ? filterDefaultDepth
158163 : ( depth || 0 )
159164
160- // add root node of tree to list of seenNodes
161-
162165 const seenNodes = new Map ( )
163166 const problems = new Set ( )
164167 const cache = new Map ( )
165- const traversePathMap = new Map ( )
166168
169+ // add root node of tree to list of seenNodes
167170 seenNodes . set ( tree . path , tree )
168171
169172 const result = exploreDependencyGraph (
@@ -173,8 +176,7 @@ class LS extends ArboristWorkspaceCmd {
173176 { json, parseable } ,
174177 seenNodes ,
175178 problems ,
176- cache ,
177- traversePathMap
179+ cache
178180 )
179181
180182 // handle the special case of a broken package.json in the root folder
@@ -237,35 +239,40 @@ const exploreDependencyGraph = (
237239 seenNodes ,
238240 problems ,
239241 cache ,
240- traversePathMap ,
242+ traversePathMap = new Map ( ) ,
241243 encounterCount = new Map ( )
242244) => {
243245 // Track the number of encounters for the current node
244246 // Why because we want to start storing/caching after the node is identified as a deduped edge
247+
245248 const count = node . path ? ( encounterCount . get ( node . path ) || 0 ) + 1 : 0
246249 node . path && encounterCount . set ( node . path , count )
247250
248- if ( node . path && cache . has ( node . path ) ) {
251+ if ( cache . has ( node . path ) ) {
249252 return cache . get ( node . path )
250253 }
251254
252255 const currentNodeResult = visit ( node , problems )
253256
257+ // how the this node is explored
258+ // so if the explored path contains this node again then it's a cycle
259+ // and we don't want to explore it again
254260 const traversePath = [ ...( traversePathMap . get ( currentNodeResult [ _parent ] ) || [ ] ) ]
255261 const isCircular = traversePath ?. includes ( node . pkgid )
256262 traversePath . push ( node . pkgid )
257263 traversePathMap . set ( currentNodeResult , traversePath )
258264
259- if ( count > 1 ) {
265+ // we want to start using cache after node is identified as a deduped
266+ if ( count > 1 && node . path && node [ _dedupe ] ) {
260267 cache . set ( node . path , currentNodeResult )
261268 }
262269
263270 // Get children of current node
264271 const children = isCircular
265272 ? [ ]
266- : getChildren ( node , currentNodeResult , seenNodes , traversePathMap )
273+ : getChildren ( node , currentNodeResult , seenNodes )
267274
268- // Recurse on each child
275+ // Recurse on each child node
269276 for ( const child of children ) {
270277 const childResult = exploreDependencyGraph (
271278 child ,
@@ -278,7 +285,9 @@ const exploreDependencyGraph = (
278285 traversePathMap ,
279286 encounterCount
280287 )
288+ // include current node if any of its children are included
281289 currentNodeResult [ _include ] = currentNodeResult [ _include ] || childResult [ _include ]
290+
282291 if ( childResult [ _include ] && ! parseable ) {
283292 if ( json ) {
284293 currentNodeResult . dependencies = currentNodeResult . dependencies || { }
@@ -386,7 +395,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
386395 ) +
387396 ( isGitNode ( node ) ? ` (${ node . resolved } )` : '' ) +
388397 ( node . isLink ? ` -> ${ relativePrefix } ${ targetLocation } ` : '' ) +
389- ( long ? `\n${ node . package . description || '' } ` : '' )
398+ ( long ? `\n${ node . package ? .description || '' } ` : '' )
390399
391400 return ( { label, nodes : [ ] , [ _include ] : node [ _include ] , [ _parent ] : node [ _parent ] } )
392401}
@@ -560,16 +569,6 @@ const augmentNodesWithMetadata = ({
560569const sortAlphabetically = ( { pkgid : a } , { pkgid : b } ) => localeCompare ( a , b )
561570
562571const humanOutput = ( { chalk, result, unicode } ) => {
563- // we need to traverse the entire tree in order to determine which items
564- // should be included (since a nested transitive included dep will make it
565- // so that all its ancestors should be displayed)
566- // here is where we put items in their expected place for archy output
567- // for (const item of seenItems) {
568- // if (item[_include] && item[_parent]) {
569- // item[_parent].nodes.push(item)
570- // }
571- // }
572-
573572 if ( ! result . nodes . length ) {
574573 result . nodes = [ '(empty)' ]
575574 }
@@ -591,11 +590,6 @@ const jsonOutput = ({ path, problems, result, rootError }) => {
591590 result . invalid = true
592591 }
593592
594- // we need to traverse the entire tree in order to determine which items
595- // should be included (since a nested transitive included dep will make it
596- // so that all its ancestors should be displayed)
597- // here is where we put items in their expected place for json output
598-
599593 return result
600594}
601595
0 commit comments