|
3 | 3 | const { resolve } = require('path')
|
4 | 4 | const { parser, arrayDelimiter } = require('@npmcli/query')
|
5 | 5 | const localeCompare = require('@isaacs/string-locale-compare')('en')
|
6 |
| -const npa = require('npm-package-arg') |
| 6 | +const log = require('proc-log') |
7 | 7 | const minimatch = require('minimatch')
|
| 8 | +const npa = require('npm-package-arg') |
8 | 9 | const semver = require('semver')
|
9 | 10 |
|
10 | 11 | // handle results for parsed query asts, results are stored in a map that has a
|
@@ -291,11 +292,115 @@ class Results {
|
291 | 292 | }
|
292 | 293 |
|
293 | 294 | semverPseudo () {
|
294 |
| - if (!this.currentAstNode.semverValue) { |
| 295 | + const { |
| 296 | + attributeMatcher, |
| 297 | + lookupProperties, |
| 298 | + semverFunc = 'infer', |
| 299 | + semverValue, |
| 300 | + } = this.currentAstNode |
| 301 | + const { qualifiedAttribute } = attributeMatcher |
| 302 | + |
| 303 | + if (!semverValue) { |
| 304 | + // DEPRECATED: remove this warning and throw an error as part of @npmcli/arborist@6 |
| 305 | + log.warn('query', 'usage of :semver() with no parameters is deprecated') |
295 | 306 | return this.initialItems
|
296 | 307 | }
|
297 |
| - return this.initialItems.filter(node => |
298 |
| - semver.satisfies(node.version, this.currentAstNode.semverValue)) |
| 308 | + |
| 309 | + if (!semver.valid(semverValue) && !semver.validRange(semverValue)) { |
| 310 | + throw Object.assign( |
| 311 | + new Error(`\`${semverValue}\` is not a valid semver version or range`), |
| 312 | + { code: 'EQUERYINVALIDSEMVER' }) |
| 313 | + } |
| 314 | + |
| 315 | + const valueIsVersion = !!semver.valid(semverValue) |
| 316 | + |
| 317 | + const nodeMatches = (node, obj) => { |
| 318 | + // if we already have an operator, the user provided some test as part of the selector |
| 319 | + // we evaluate that first because if it fails we don't want this node anyway |
| 320 | + if (attributeMatcher.operator) { |
| 321 | + if (!attributeMatch(attributeMatcher, obj)) { |
| 322 | + // if the initial operator doesn't match, we're done |
| 323 | + return false |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + const attrValue = obj[qualifiedAttribute] |
| 328 | + // both valid and validRange return null for undefined, so this will skip both nodes that |
| 329 | + // do not have the attribute defined as well as those where the attribute value is invalid |
| 330 | + // and those where the value from the package.json is not a string |
| 331 | + if ((!semver.valid(attrValue) && !semver.validRange(attrValue)) || |
| 332 | + typeof attrValue !== 'string') { |
| 333 | + return false |
| 334 | + } |
| 335 | + |
| 336 | + const attrIsVersion = !!semver.valid(attrValue) |
| 337 | + |
| 338 | + let actualFunc = semverFunc |
| 339 | + |
| 340 | + // if we're asked to infer, we examine outputs to make a best guess |
| 341 | + if (actualFunc === 'infer') { |
| 342 | + if (valueIsVersion && attrIsVersion) { |
| 343 | + // two versions -> semver.eq |
| 344 | + actualFunc = 'eq' |
| 345 | + } else if (!valueIsVersion && !attrIsVersion) { |
| 346 | + // two ranges -> semver.intersects |
| 347 | + actualFunc = 'intersects' |
| 348 | + } else { |
| 349 | + // anything else -> semver.satisfies |
| 350 | + actualFunc = 'satisfies' |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + if (['eq', 'neq', 'gt', 'gte', 'lt', 'lte'].includes(actualFunc)) { |
| 355 | + // both sides must be versions, but one is not |
| 356 | + if (!valueIsVersion || !attrIsVersion) { |
| 357 | + return false |
| 358 | + } |
| 359 | + |
| 360 | + return semver[actualFunc](attrValue, semverValue) |
| 361 | + } else if (['gtr', 'ltr', 'satisfies'].includes(actualFunc)) { |
| 362 | + // at least one side must be a version, but neither is |
| 363 | + if (!valueIsVersion && !attrIsVersion) { |
| 364 | + return false |
| 365 | + } |
| 366 | + |
| 367 | + return valueIsVersion |
| 368 | + ? semver[actualFunc](semverValue, attrValue) |
| 369 | + : semver[actualFunc](attrValue, semverValue) |
| 370 | + } else if (['intersects', 'subset'].includes(actualFunc)) { |
| 371 | + // these accept two ranges and since a version is also a range, anything goes |
| 372 | + return semver[actualFunc](attrValue, semverValue) |
| 373 | + } else { |
| 374 | + // user provided a function we don't know about, throw an error |
| 375 | + throw Object.assign(new Error(`\`semver.${actualFunc}\` is not a supported operator.`), |
| 376 | + { code: 'EQUERYINVALIDOPERATOR' }) |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + return this.initialItems.filter((node) => { |
| 381 | + // no lookupProperties just means its a top level property, see if it matches |
| 382 | + if (!lookupProperties.length) { |
| 383 | + return nodeMatches(node, node.package) |
| 384 | + } |
| 385 | + |
| 386 | + // this code is mostly duplicated from attrPseudo to traverse into the package until we get |
| 387 | + // to our deepest requested object |
| 388 | + let objs = [node.package] |
| 389 | + for (const prop of lookupProperties) { |
| 390 | + if (prop === arrayDelimiter) { |
| 391 | + objs = objs.flat() |
| 392 | + continue |
| 393 | + } |
| 394 | + |
| 395 | + objs = objs.flatMap(obj => obj[prop] || []) |
| 396 | + const noAttr = objs.every(obj => !obj) |
| 397 | + if (noAttr) { |
| 398 | + return false |
| 399 | + } |
| 400 | + |
| 401 | + return objs.some(obj => nodeMatches(node, obj)) |
| 402 | + } |
| 403 | + }) |
299 | 404 | }
|
300 | 405 |
|
301 | 406 | typePseudo () {
|
@@ -358,6 +463,7 @@ const attributeOperator = ({ attr, value, insensitive, operator }) => {
|
358 | 463 | if (insensitive) {
|
359 | 464 | attr = attr.toLowerCase()
|
360 | 465 | }
|
| 466 | + |
361 | 467 | return attributeOperators[operator]({
|
362 | 468 | attr,
|
363 | 469 | insensitive,
|
|
0 commit comments