@npmcli/arborist 5.3.1 → 5.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1076,9 +1076,8 @@ This is a one-time fix-up, please be patient...
1076
1076
  // if it fails at this point, though, dont' worry because it
1077
1077
  // may well be an optional dep that has gone missing. it'll
1078
1078
  // fail later anyway.
1079
- const from = fromPath(placed)
1080
1079
  promises.push(...this[_problemEdges](placed).map(e =>
1081
- this[_fetchManifest](npa.resolve(e.name, e.spec, from))
1080
+ this[_fetchManifest](npa.resolve(e.name, e.spec, fromPath(placed, e)))
1082
1081
  .catch(er => null)))
1083
1082
  },
1084
1083
  })
@@ -74,8 +74,12 @@ class Arborist extends Base {
74
74
  cache: options.cache || `${homedir()}/.npm/_cacache`,
75
75
  packumentCache: options.packumentCache || new Map(),
76
76
  workspacesEnabled: options.workspacesEnabled !== false,
77
+ replaceRegistryHost: options.replaceRegistryHost,
77
78
  lockfileVersion: lockfileVersion(options.lockfileVersion),
78
79
  }
80
+ this.replaceRegistryHost = this.options.replaceRegistryHost =
81
+ (!this.options.replaceRegistryHost || this.options.replaceRegistryHost === 'npmjs') ?
82
+ 'registry.npmjs.org' : this.options.replaceRegistryHost
79
83
 
80
84
  this[_workspacesEnabled] = this.options.workspacesEnabled
81
85
 
@@ -115,6 +115,7 @@ module.exports = cls => class ActualLoader extends cls {
115
115
  root = null,
116
116
  transplantFilter = () => true,
117
117
  ignoreMissing = false,
118
+ forceActual = false,
118
119
  } = options
119
120
  this[_filter] = filter
120
121
  this[_transplantFilter] = transplantFilter
@@ -141,26 +142,30 @@ module.exports = cls => class ActualLoader extends cls {
141
142
 
142
143
  this[_actualTree].assertRootOverrides()
143
144
 
144
- // Note: hidden lockfile will be rejected if it's not the latest thing
145
- // in the folder, or if any of the entries in the hidden lockfile are
146
- // missing.
147
- const meta = await Shrinkwrap.load({
148
- path: this[_actualTree].path,
149
- hiddenLockfile: true,
150
- resolveOptions: this.options,
151
- })
152
- if (meta.loadedFromDisk) {
153
- this[_actualTree].meta = meta
154
- return this[_loadActualVirtually]({ root })
155
- } else {
145
+ // if forceActual is set, don't even try the hidden lockfile
146
+ if (!forceActual) {
147
+ // Note: hidden lockfile will be rejected if it's not the latest thing
148
+ // in the folder, or if any of the entries in the hidden lockfile are
149
+ // missing.
156
150
  const meta = await Shrinkwrap.load({
157
151
  path: this[_actualTree].path,
158
- lockfileVersion: this.options.lockfileVersion,
152
+ hiddenLockfile: true,
159
153
  resolveOptions: this.options,
160
154
  })
161
- this[_actualTree].meta = meta
162
- return this[_loadActualActually]({ root, ignoreMissing })
155
+
156
+ if (meta.loadedFromDisk) {
157
+ this[_actualTree].meta = meta
158
+ return this[_loadActualVirtually]({ root })
159
+ }
163
160
  }
161
+
162
+ const meta = await Shrinkwrap.load({
163
+ path: this[_actualTree].path,
164
+ lockfileVersion: this.options.lockfileVersion,
165
+ resolveOptions: this.options,
166
+ })
167
+ this[_actualTree].meta = meta
168
+ return this[_loadActualActually]({ root, ignoreMissing })
164
169
  }
165
170
 
166
171
  async [_loadActualVirtually] ({ root }) {
@@ -712,13 +712,19 @@ module.exports = cls => class Reifier extends cls {
712
712
  [_registryResolved] (resolved) {
713
713
  // the default registry url is a magic value meaning "the currently
714
714
  // configured registry".
715
+ // `resolved` must never be falsey.
715
716
  //
716
717
  // XXX: use a magic string that isn't also a valid value, like
717
718
  // ${REGISTRY} or something. This has to be threaded through the
718
719
  // Shrinkwrap and Node classes carefully, so for now, just treat
719
720
  // the default reg as the magical animal that it has been.
720
- return resolved && resolved
721
- .replace(/^https?:\/\/registry\.npmjs\.org\//, this.registry)
721
+ const resolvedURL = new URL(resolved)
722
+ if ((this.options.replaceRegistryHost === resolvedURL.hostname)
723
+ || this.options.replaceRegistryHost === 'always') {
724
+ // this.registry always has a trailing slash
725
+ resolved = `${this.registry.slice(0, -1)}${resolvedURL.pathname}${resolvedURL.searchParams}`
726
+ }
727
+ return resolved
722
728
  }
723
729
 
724
730
  // bundles are *sort of* like shrinkwraps, in that the branch is defined
package/lib/dep-valid.js CHANGED
@@ -20,7 +20,7 @@ const depValid = (child, requested, requestor) => {
20
20
  // file: deps that depend on other files/dirs, we must resolve the
21
21
  // location based on the *requestor* file/dir, not where it ends up.
22
22
  // '' is equivalent to '*'
23
- requested = npa.resolve(child.name, requested || '*', fromPath(requestor))
23
+ requested = npa.resolve(child.name, requested || '*', fromPath(requestor, requestor.edgesOut.get(child.name)))
24
24
  } catch (er) {
25
25
  // Not invalid because the child doesn't match, but because
26
26
  // the spec itself is not supported. Nothing would match,
package/lib/edge.js CHANGED
@@ -169,7 +169,11 @@ class Edge {
169
169
  if (this.overrides && this.overrides.value && this.overrides.name === this.name) {
170
170
  if (this.overrides.value.startsWith('$')) {
171
171
  const ref = this.overrides.value.slice(1)
172
- const pkg = this.from.root.package
172
+ // we may be a virtual root, if we are we want to resolve reference overrides
173
+ // from the real root, not the virtual one
174
+ const pkg = this.from.sourceReference
175
+ ? this.from.sourceReference.root.package
176
+ : this.from.root.package
173
177
  const overrideSpec = (pkg.devDependencies && pkg.devDependencies[ref]) ||
174
178
  (pkg.optionalDependencies && pkg.optionalDependencies[ref]) ||
175
179
  (pkg.dependencies && pkg.dependencies[ref]) ||
package/lib/from-path.js CHANGED
@@ -6,8 +6,19 @@
6
6
  const { dirname } = require('path')
7
7
  const npa = require('npm-package-arg')
8
8
 
9
- const fromPath = (node, spec) =>
10
- spec && spec.type === 'file' ? dirname(spec.fetchSpec)
11
- : node.realpath
9
+ const fromPath = (node, spec, edge) => {
10
+ if (edge && edge.overrides && edge.overrides.name === edge.name && edge.overrides.value) {
11
+ // fromPath could be called with a node that has a virtual root, if that happens
12
+ // we want to make sure we get the real root node when overrides are in use. this
13
+ // is to allow things like overriding a dependency with a tarball file that's a
14
+ // relative path from the project root
15
+ return node.sourceReference
16
+ ? node.sourceReference.root.realpath
17
+ : node.root.realpath
18
+ }
12
19
 
13
- module.exports = node => fromPath(node, node.resolved && npa(node.resolved))
20
+ return spec && spec.type === 'file' ? dirname(spec.fetchSpec)
21
+ : node.realpath
22
+ }
23
+
24
+ module.exports = (node, edge) => fromPath(node, node.resolved && npa(node.resolved), edge)
package/lib/node.js CHANGED
@@ -69,6 +69,8 @@ const consistentResolve = require('./consistent-resolve.js')
69
69
  const printableTree = require('./printable.js')
70
70
  const CaseInsensitiveMap = require('./case-insensitive-map.js')
71
71
 
72
+ const querySelectorAll = require('./query-selector-all.js')
73
+
72
74
  class Node {
73
75
  constructor (options) {
74
76
  // NB: path can be null if it's a link target
@@ -332,6 +334,10 @@ class Node {
332
334
  return `${myname}@${alias}${version}`
333
335
  }
334
336
 
337
+ get overridden () {
338
+ return !!(this.overrides && this.overrides.value && this.overrides.name === this.name)
339
+ }
340
+
335
341
  get package () {
336
342
  return this[_package]
337
343
  }
@@ -1446,6 +1452,12 @@ class Node {
1446
1452
  return base === name && basename(nm) === 'node_modules' ? dir : false
1447
1453
  }
1448
1454
 
1455
+ // maybe accept both string value or array of strings
1456
+ // seems to be what dom API does
1457
+ querySelectorAll (query) {
1458
+ return querySelectorAll(this, query)
1459
+ }
1460
+
1449
1461
  toJSON () {
1450
1462
  return printableTree(this)
1451
1463
  }
@@ -0,0 +1,565 @@
1
+ 'use strict'
2
+
3
+ const { resolve } = require('path')
4
+ const { parser, arrayDelimiter } = require('@npmcli/query')
5
+ const localeCompare = require('@isaacs/string-locale-compare')('en')
6
+ const npa = require('npm-package-arg')
7
+ const minimatch = require('minimatch')
8
+ const semver = require('semver')
9
+
10
+ // handle results for parsed query asts, results are stored in a map that has a
11
+ // key that points to each ast selector node and stores the resulting array of
12
+ // arborist nodes as its value, that is essential to how we handle multiple
13
+ // query selectors, e.g: `#a, #b, #c` <- 3 diff ast selector nodes
14
+ class Results {
15
+ #currentAstSelector
16
+ #initialItems
17
+ #inventory
18
+ #pendingCombinator
19
+ #results = new Map()
20
+ #targetNode
21
+
22
+ constructor (opts) {
23
+ this.#currentAstSelector = opts.rootAstNode.nodes[0]
24
+ this.#inventory = opts.inventory
25
+ this.#initialItems = opts.initialItems
26
+ this.#targetNode = opts.targetNode
27
+
28
+ this.currentResults = this.#initialItems
29
+
30
+ // reset by rootAstNode walker
31
+ this.currentAstNode = opts.rootAstNode
32
+ }
33
+
34
+ get currentResults () {
35
+ return this.#results.get(this.#currentAstSelector)
36
+ }
37
+
38
+ set currentResults (value) {
39
+ this.#results.set(this.#currentAstSelector, value)
40
+ }
41
+
42
+ // retrieves the initial items to which start the filtering / matching
43
+ // for most of the different types of recognized ast nodes, e.g: class (aka
44
+ // depType), id, *, etc in different contexts we need to start with the
45
+ // current list of filtered results, for example a query for `.workspace`
46
+ // actually means the same as `*.workspace` so we want to start with the full
47
+ // inventory if that's the first ast node we're reading but if it appears in
48
+ // the middle of a query it should respect the previous filtered results,
49
+ // combinators are a special case in which we always want to have the
50
+ // complete inventory list in order to use the left-hand side ast node as a
51
+ // filter combined with the element on its right-hand side
52
+ get initialItems () {
53
+ const firstParsed =
54
+ (this.currentAstNode.parent.nodes[0] === this.currentAstNode) &&
55
+ (this.currentAstNode.parent.parent.type === 'root')
56
+
57
+ if (firstParsed) {
58
+ return this.#initialItems
59
+ }
60
+ if (this.currentAstNode.prev().type === 'combinator') {
61
+ return this.#inventory
62
+ }
63
+ return this.currentResults
64
+ }
65
+
66
+ // combinators need information about previously filtered items along
67
+ // with info of the items parsed / retrieved from the selector right
68
+ // past the combinator, for this reason combinators are stored and
69
+ // only ran as the last part of each selector logic
70
+ processPendingCombinator (nextResults) {
71
+ if (this.#pendingCombinator) {
72
+ const res = this.#pendingCombinator(this.currentResults, nextResults)
73
+ this.#pendingCombinator = null
74
+ this.currentResults = res
75
+ } else {
76
+ this.currentResults = nextResults
77
+ }
78
+ }
79
+
80
+ // when collecting results to a root astNode, we traverse the list of child
81
+ // selector nodes and collect all of their resulting arborist nodes into a
82
+ // single/flat Set of items, this ensures we also deduplicate items
83
+ collect (rootAstNode) {
84
+ return new Set(rootAstNode.nodes.flatMap(n => this.#results.get(n)))
85
+ }
86
+
87
+ // selector types map to the '.type' property of the ast nodes via `${astNode.type}Type`
88
+ //
89
+ // attribute selector [name=value], etc
90
+ attributeType () {
91
+ const nextResults = this.initialItems.filter(node =>
92
+ attributeMatch(this.currentAstNode, node.package)
93
+ )
94
+ this.processPendingCombinator(nextResults)
95
+ }
96
+
97
+ // dependency type selector (i.e. .prod, .dev, etc)
98
+ // css calls this class, we interpret is as dependency type
99
+ classType () {
100
+ const depTypeFn = depTypes[String(this.currentAstNode)]
101
+ if (!depTypeFn) {
102
+ throw Object.assign(
103
+ new Error(`\`${String(this.currentAstNode)}\` is not a supported dependency type.`),
104
+ { code: 'EQUERYNODEPTYPE' }
105
+ )
106
+ }
107
+ const nextResults = depTypeFn(this.initialItems)
108
+ this.processPendingCombinator(nextResults)
109
+ }
110
+
111
+ // combinators (i.e. '>', ' ', '~')
112
+ combinatorType () {
113
+ this.#pendingCombinator = combinators[String(this.currentAstNode)]
114
+ }
115
+
116
+ // name selectors (i.e. #foo, #foo@1.0.0)
117
+ // css calls this id, we interpret it as name
118
+ idType () {
119
+ const spec = npa(this.currentAstNode.value)
120
+ const nextResults = this.initialItems.filter(node =>
121
+ (node.name === spec.name || node.package.name === spec.name) &&
122
+ (semver.satisfies(node.version, spec.fetchSpec) || !spec.rawSpec))
123
+ this.processPendingCombinator(nextResults)
124
+ }
125
+
126
+ // pseudo selectors (prefixed with :)
127
+ pseudoType () {
128
+ const pseudoFn = `${this.currentAstNode.value.slice(1)}Pseudo`
129
+ if (!this[pseudoFn]) {
130
+ throw Object.assign(
131
+ new Error(`\`${this.currentAstNode.value
132
+ }\` is not a supported pseudo selector.`),
133
+ { code: 'EQUERYNOPSEUDO' }
134
+ )
135
+ }
136
+ const nextResults = this[pseudoFn]()
137
+ this.processPendingCombinator(nextResults)
138
+ }
139
+
140
+ selectorType () {
141
+ this.#currentAstSelector = this.currentAstNode
142
+ // starts a new array in which resulting items
143
+ // can be stored for each given ast selector
144
+ if (!this.currentResults) {
145
+ this.currentResults = []
146
+ }
147
+ }
148
+
149
+ universalType () {
150
+ this.processPendingCombinator(this.initialItems)
151
+ }
152
+
153
+ // pseudo selectors map to the 'value' property of the pseudo selectors in the ast nodes
154
+ // via selectors via `${value.slice(1)}Pseudo`
155
+ attrPseudo () {
156
+ const { lookupProperties, attributeMatcher } = this.currentAstNode
157
+
158
+ return this.initialItems.filter(node => {
159
+ let objs = [node.package]
160
+ for (const prop of lookupProperties) {
161
+ // if an isArray symbol is found that means we'll need to iterate
162
+ // over the previous found array to basically make sure we traverse
163
+ // all its indexes testing for possible objects that may eventually
164
+ // hold more keys specified in a selector
165
+ if (prop === arrayDelimiter) {
166
+ objs = objs.flat()
167
+ continue
168
+ }
169
+
170
+ // otherwise just maps all currently found objs
171
+ // to the next prop from the lookup properties list,
172
+ // filters out any empty key lookup
173
+ objs = objs.flatMap(obj => obj[prop] || [])
174
+
175
+ // in case there's no property found in the lookup
176
+ // just filters that item out
177
+ const noAttr = objs.every(obj => !obj)
178
+ if (noAttr) {
179
+ return false
180
+ }
181
+ }
182
+
183
+ // if any of the potential object matches
184
+ // that item should be in the final result
185
+ return objs.some(obj => attributeMatch(attributeMatcher, obj))
186
+ })
187
+ }
188
+
189
+ emptyPseudo () {
190
+ return this.initialItems.filter(node => node.edgesOut.size === 0)
191
+ }
192
+
193
+ extraneousPseudo () {
194
+ return this.initialItems.filter(node => node.extraneous)
195
+ }
196
+
197
+ hasPseudo () {
198
+ const found = []
199
+ for (const item of this.initialItems) {
200
+ const res = retrieveNodesFromParsedAst({
201
+ // This is the one time initialItems differs from inventory
202
+ initialItems: [item],
203
+ inventory: this.#inventory,
204
+ rootAstNode: this.currentAstNode.nestedNode,
205
+ targetNode: item,
206
+ })
207
+ if (res.size > 0) {
208
+ found.push(item)
209
+ }
210
+ }
211
+ return found
212
+ }
213
+
214
+ invalidPseudo () {
215
+ const found = []
216
+ for (const node of this.initialItems) {
217
+ for (const edge of node.edgesIn) {
218
+ if (edge.invalid) {
219
+ found.push(node)
220
+ break
221
+ }
222
+ }
223
+ }
224
+ return found
225
+ }
226
+
227
+ isPseudo () {
228
+ const res = retrieveNodesFromParsedAst({
229
+ initialItems: this.initialItems,
230
+ inventory: this.#inventory,
231
+ rootAstNode: this.currentAstNode.nestedNode,
232
+ targetNode: this.currentAstNode,
233
+ })
234
+ return [...res]
235
+ }
236
+
237
+ linkPseudo () {
238
+ return this.initialItems.filter(node => node.isLink || (node.isTop && !node.isRoot))
239
+ }
240
+
241
+ missingPseudo () {
242
+ return this.#inventory.reduce((res, node) => {
243
+ for (const edge of node.edgesOut.values()) {
244
+ if (edge.missing) {
245
+ const pkg = { name: edge.name, version: edge.spec }
246
+ res.push(new this.#targetNode.constructor({ pkg }))
247
+ }
248
+ }
249
+ return res
250
+ }, [])
251
+ }
252
+
253
+ notPseudo () {
254
+ const res = retrieveNodesFromParsedAst({
255
+ initialItems: this.initialItems,
256
+ inventory: this.#inventory,
257
+ rootAstNode: this.currentAstNode.nestedNode,
258
+ targetNode: this.currentAstNode,
259
+ })
260
+ const internalSelector = new Set(res)
261
+ return this.initialItems.filter(node =>
262
+ !internalSelector.has(node))
263
+ }
264
+
265
+ overriddenPseudo () {
266
+ return this.initialItems.filter(node => node.overridden)
267
+ }
268
+
269
+ pathPseudo () {
270
+ return this.initialItems.filter(node => {
271
+ if (!this.currentAstNode.pathValue) {
272
+ return true
273
+ }
274
+ return minimatch(
275
+ node.realpath.replace(/\\+/g, '/'),
276
+ resolve(node.root.realpath, this.currentAstNode.pathValue).replace(/\\+/g, '/')
277
+ )
278
+ })
279
+ }
280
+
281
+ privatePseudo () {
282
+ return this.initialItems.filter(node => node.package.private)
283
+ }
284
+
285
+ rootPseudo () {
286
+ return this.initialItems.filter(node => node === this.#targetNode.root)
287
+ }
288
+
289
+ scopePseudo () {
290
+ return this.initialItems.filter(node => node === this.#targetNode)
291
+ }
292
+
293
+ semverPseudo () {
294
+ if (!this.currentAstNode.semverValue) {
295
+ return this.initialItems
296
+ }
297
+ return this.initialItems.filter(node =>
298
+ semver.satisfies(node.version, this.currentAstNode.semverValue))
299
+ }
300
+
301
+ typePseudo () {
302
+ if (!this.currentAstNode.typeValue) {
303
+ return this.initialItems
304
+ }
305
+ return this.initialItems
306
+ .flatMap(node => {
307
+ const found = []
308
+ for (const edge of node.edgesIn) {
309
+ if (npa(`${edge.name}@${edge.spec}`).type === this.currentAstNode.typeValue) {
310
+ found.push(edge.to)
311
+ }
312
+ }
313
+ return found
314
+ })
315
+ }
316
+
317
+ dedupedPseudo () {
318
+ return this.initialItems.filter(node => node.target.edgesIn.size > 1)
319
+ }
320
+ }
321
+
322
+ // operators for attribute selectors
323
+ const attributeOperators = {
324
+ // attribute value is equivalent
325
+ '=' ({ attr, value, insensitive }) {
326
+ return attr === value
327
+ },
328
+ // attribute value contains word
329
+ '~=' ({ attr, value, insensitive }) {
330
+ return (attr.match(/\w+/g) || []).includes(value)
331
+ },
332
+ // attribute value contains string
333
+ '*=' ({ attr, value, insensitive }) {
334
+ return attr.includes(value)
335
+ },
336
+ // attribute value is equal or starts with
337
+ '|=' ({ attr, value, insensitive }) {
338
+ return attr.startsWith(`${value}-`)
339
+ },
340
+ // attribute value starts with
341
+ '^=' ({ attr, value, insensitive }) {
342
+ return attr.startsWith(value)
343
+ },
344
+ // attribute value ends with
345
+ '$=' ({ attr, value, insensitive }) {
346
+ return attr.endsWith(value)
347
+ },
348
+ }
349
+
350
+ const attributeOperator = ({ attr, value, insensitive, operator }) => {
351
+ if (typeof attr === 'number') {
352
+ attr = String(attr)
353
+ }
354
+ if (typeof attr !== 'string') {
355
+ // It's an object or an array, bail
356
+ return false
357
+ }
358
+ if (insensitive) {
359
+ attr = attr.toLowerCase()
360
+ }
361
+ return attributeOperators[operator]({
362
+ attr,
363
+ insensitive,
364
+ value,
365
+ })
366
+ }
367
+
368
+ const attributeMatch = (matcher, obj) => {
369
+ const insensitive = !!matcher.insensitive
370
+ const operator = matcher.operator || ''
371
+ const attribute = matcher.qualifiedAttribute
372
+ let value = matcher.value || ''
373
+ // return early if checking existence
374
+ if (operator === '') {
375
+ return Boolean(obj[attribute])
376
+ }
377
+ if (insensitive) {
378
+ value = value.toLowerCase()
379
+ }
380
+ // in case the current object is an array
381
+ // then we try to match every item in the array
382
+ if (Array.isArray(obj[attribute])) {
383
+ return obj[attribute].find((i, index) => {
384
+ const attr = obj[attribute][index] || ''
385
+ return attributeOperator({ attr, value, insensitive, operator })
386
+ })
387
+ } else {
388
+ const attr = obj[attribute] || ''
389
+ return attributeOperator({ attr, value, insensitive, operator })
390
+ }
391
+ }
392
+
393
+ const edgeIsType = (node, type, seen = new Set()) => {
394
+ for (const edgeIn of node.edgesIn) {
395
+ // TODO Need a test with an infinite loop
396
+ if (seen.has(edgeIn)) {
397
+ continue
398
+ }
399
+ seen.add(edgeIn)
400
+ if (edgeIn.type === type || edgeIn.from[type] || edgeIsType(edgeIn.from, type, seen)) {
401
+ return true
402
+ }
403
+ }
404
+ return false
405
+ }
406
+
407
+ const filterByType = (nodes, type) => {
408
+ const found = []
409
+ for (const node of nodes) {
410
+ if (node[type] || edgeIsType(node, type)) {
411
+ found.push(node)
412
+ }
413
+ }
414
+ return found
415
+ }
416
+
417
+ const depTypes = {
418
+ // dependency
419
+ '.prod' (prevResults) {
420
+ const found = []
421
+ for (const node of prevResults) {
422
+ if (!node.dev) {
423
+ found.push(node)
424
+ }
425
+ }
426
+ return found
427
+ },
428
+ // devDependency
429
+ '.dev' (prevResults) {
430
+ return filterByType(prevResults, 'dev')
431
+ },
432
+ // optionalDependency
433
+ '.optional' (prevResults) {
434
+ return filterByType(prevResults, 'optional')
435
+ },
436
+ // peerDependency
437
+ '.peer' (prevResults) {
438
+ return filterByType(prevResults, 'peer')
439
+ },
440
+ // workspace
441
+ '.workspace' (prevResults) {
442
+ return prevResults.filter(node => node.isWorkspace)
443
+ },
444
+ // bundledDependency
445
+ '.bundled' (prevResults) {
446
+ return prevResults.filter(node => node.inBundle)
447
+ },
448
+ }
449
+
450
+ // checks if a given node has a direct parent in any of the nodes provided in
451
+ // the compare nodes array
452
+ const hasParent = (node, compareNodes) => {
453
+ // All it takes is one so we loop and return on the first hit
454
+ for (const compareNode of compareNodes) {
455
+ // follows logical parent for link anscestors
456
+ if (node.isTop && (node.resolveParent === compareNode)) {
457
+ return true
458
+ }
459
+ // follows edges-in to check if they match a possible parent
460
+ for (const edge of node.edgesIn) {
461
+ if (edge && edge.from === compareNode) {
462
+ return true
463
+ }
464
+ }
465
+ }
466
+ return false
467
+ }
468
+
469
+ // checks if a given node is a descendant of any of the nodes provided in the
470
+ // compareNodes array
471
+ const hasAscendant = (node, compareNodes, seen = new Set()) => {
472
+ // TODO (future) loop over ancestry property
473
+ if (hasParent(node, compareNodes)) {
474
+ return true
475
+ }
476
+
477
+ if (node.isTop && node.resolveParent) {
478
+ return hasAscendant(node.resolveParent, compareNodes)
479
+ }
480
+ for (const edge of node.edgesIn) {
481
+ // TODO Need a test with an infinite loop
482
+ if (seen.has(edge)) {
483
+ continue
484
+ }
485
+ seen.add(edge)
486
+ if (edge && edge.from && hasAscendant(edge.from, compareNodes, seen)) {
487
+ return true
488
+ }
489
+ }
490
+ return false
491
+ }
492
+
493
+ const combinators = {
494
+ // direct descendant
495
+ '>' (prevResults, nextResults) {
496
+ return nextResults.filter(node => hasParent(node, prevResults))
497
+ },
498
+ // any descendant
499
+ ' ' (prevResults, nextResults) {
500
+ return nextResults.filter(node => hasAscendant(node, prevResults))
501
+ },
502
+ // sibling
503
+ '~' (prevResults, nextResults) {
504
+ // Return any node in nextResults that is a sibling of (aka shares a
505
+ // parent with) a node in prevResults
506
+ const parentNodes = new Set() // Parents of everything in prevResults
507
+ for (const node of prevResults) {
508
+ for (const edge of node.edgesIn) {
509
+ // edge.from always exists cause it's from another node's edgesIn
510
+ parentNodes.add(edge.from)
511
+ }
512
+ }
513
+ return nextResults.filter(node =>
514
+ !prevResults.includes(node) && hasParent(node, [...parentNodes])
515
+ )
516
+ },
517
+ }
518
+
519
+ const retrieveNodesFromParsedAst = (opts) => {
520
+ // when we first call this it's the parsed query. all other times it's
521
+ // results.currentNode.nestedNode
522
+ const rootAstNode = opts.rootAstNode
523
+
524
+ if (!rootAstNode.nodes) {
525
+ return new Set()
526
+ }
527
+
528
+ const results = new Results(opts)
529
+
530
+ rootAstNode.walk((nextAstNode) => {
531
+ // This is the only place we reset currentAstNode
532
+ results.currentAstNode = nextAstNode
533
+ const updateFn = `${results.currentAstNode.type}Type`
534
+ if (typeof results[updateFn] !== 'function') {
535
+ throw Object.assign(
536
+ new Error(`\`${results.currentAstNode.type}\` is not a supported selector.`),
537
+ { code: 'EQUERYNOSELECTOR' }
538
+ )
539
+ }
540
+ results[updateFn]()
541
+ })
542
+
543
+ return results.collect(rootAstNode)
544
+ }
545
+
546
+ // We are keeping this async in the event that we do add async operators, we
547
+ // won't have to have a breaking change on this function signature.
548
+ const querySelectorAll = async (targetNode, query) => {
549
+ // This never changes ever we just pass it around. But we can't scope it to
550
+ // this whole file if we ever want to support concurrent calls to this
551
+ // function.
552
+ const inventory = [...targetNode.root.inventory.values()]
553
+ // res is a Set of items returned for each parsed css ast selector
554
+ const res = retrieveNodesFromParsedAst({
555
+ initialItems: inventory,
556
+ inventory,
557
+ rootAstNode: parser(query),
558
+ targetNode,
559
+ })
560
+
561
+ // returns nodes ordered by realpath
562
+ return [...res].sort((a, b) => localeCompare(a.location, b.location))
563
+ }
564
+
565
+ module.exports = querySelectorAll
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/arborist",
3
- "version": "5.3.1",
3
+ "version": "5.6.0",
4
4
  "description": "Manage node_modules trees",
5
5
  "dependencies": {
6
6
  "@isaacs/string-locale-compare": "^1.1.0",
@@ -11,15 +11,17 @@
11
11
  "@npmcli/name-from-folder": "^1.0.1",
12
12
  "@npmcli/node-gyp": "^2.0.0",
13
13
  "@npmcli/package-json": "^2.0.0",
14
+ "@npmcli/query": "^1.1.1",
14
15
  "@npmcli/run-script": "^4.1.3",
15
16
  "bin-links": "^3.0.0",
16
17
  "cacache": "^16.0.6",
17
18
  "common-ancestor-path": "^1.0.1",
18
19
  "json-parse-even-better-errors": "^2.3.1",
19
20
  "json-stringify-nice": "^1.1.4",
21
+ "minimatch": "^5.1.0",
20
22
  "mkdirp": "^1.0.4",
21
23
  "mkdirp-infer-owner": "^2.0.0",
22
- "nopt": "^5.0.0",
24
+ "nopt": "^6.0.0",
23
25
  "npm-install-checks": "^5.0.0",
24
26
  "npm-package-arg": "^9.0.0",
25
27
  "npm-pick-manifest": "^7.0.0",