Skip to content

Commit

Permalink
fix: Fix non-satisfiable ranges so they no longer intersect with anyt…
Browse files Browse the repository at this point in the history
…hing
  • Loading branch information
Max Bittman authored and isaacs committed Mar 26, 2019
1 parent 8473d65 commit 9b8e961
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 20 additions & 2 deletions semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,15 +935,33 @@ Range.prototype.intersects = function (range, options) {

return this.set.some(function (thisComparators) {
return range.set.some(function (rangeComparators) {
return thisComparators.every(function (thisComparator) {
return rangeComparators.every(function (rangeComparator) {
return isSatisfiable(thisComparators, options) && thisComparators.every(function (thisComparator) {
return isSatisfiable(rangeComparators, options) && rangeComparators.every(function (rangeComparator) {
return thisComparator.intersects(rangeComparator, options)
})
})
})
})
}

// take a set of comparators and determine whether there
// exists a version which can satisfy it
function isSatisfiable (comparators, options) {
var result = true
var remainingComparators = comparators.slice()
var testComparator = remainingComparators.pop()

while (result && remainingComparators.length) {
result = remainingComparators.every(function (otherComparator) {
return testComparator.intersects(otherComparator, options)
})

testComparator = remainingComparators.pop()
}

return result
}

// Mostly just for testing and legacy API reasons
exports.toComparators = toComparators
function toComparators (range, options) {
Expand Down
8 changes: 6 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,15 +875,19 @@ test('missing comparator parameter in intersect comparators', function (t) {
test('ranges intersect', function (t) {
[
['1.3.0 || <1.0.0 >2.0.0', '1.3.0 || <1.0.0 >2.0.0', true],
['<1.0.0 >2.0.0', '>0.0.0', true],
['<1.0.0 >2.0.0', '>0.0.0', false],
['>0.0.0', '<1.0.0 >2.0.0', false],
['<1.0.0 >2.0.0', '>1.4.0 <1.6.0', false],
['<1.0.0 >2.0.0', '>1.4.0 <1.6.0 || 2.0.0', false],
['>1.0.0 <=2.0.0', '2.0.0', true],
['<1.0.0 >=2.0.0', '2.1.0', false],
['<1.0.0 >=2.0.0', '>1.4.0 <1.6.0 || 2.0.0', false],
['1.5.x', '<1.5.0 || >=1.6.0', false],
['<1.5.0 || >=1.6.0', '1.5.x', false],
['<1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2', '>=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2', false]
['<1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2', '>=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2', false],
['<=1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2', '>=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2', true],
['>=1.0.0', '<=1.0.0', true],
['>1.0.0 <1.0.0', '<=0.0.0', false]
].forEach(function (v) {
var range1 = new Range(v[0])
var range2 = new Range(v[1])
Expand Down

0 comments on commit 9b8e961

Please sign in to comment.