Skip to content

Commit 9b8e961

Browse files
Max Bittmanisaacs
authored andcommitted
fix: Fix non-satisfiable ranges so they no longer intersect with anything
1 parent 8473d65 commit 9b8e961

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

semver.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,15 +935,33 @@ Range.prototype.intersects = function (range, options) {
935935

936936
return this.set.some(function (thisComparators) {
937937
return range.set.some(function (rangeComparators) {
938-
return thisComparators.every(function (thisComparator) {
939-
return rangeComparators.every(function (rangeComparator) {
938+
return isSatisfiable(thisComparators, options) && thisComparators.every(function (thisComparator) {
939+
return isSatisfiable(rangeComparators, options) && rangeComparators.every(function (rangeComparator) {
940940
return thisComparator.intersects(rangeComparator, options)
941941
})
942942
})
943943
})
944944
})
945945
}
946946

947+
// take a set of comparators and determine whether there
948+
// exists a version which can satisfy it
949+
function isSatisfiable (comparators, options) {
950+
var result = true
951+
var remainingComparators = comparators.slice()
952+
var testComparator = remainingComparators.pop()
953+
954+
while (result && remainingComparators.length) {
955+
result = remainingComparators.every(function (otherComparator) {
956+
return testComparator.intersects(otherComparator, options)
957+
})
958+
959+
testComparator = remainingComparators.pop()
960+
}
961+
962+
return result
963+
}
964+
947965
// Mostly just for testing and legacy API reasons
948966
exports.toComparators = toComparators
949967
function toComparators (range, options) {

test/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,15 +875,19 @@ test('missing comparator parameter in intersect comparators', function (t) {
875875
test('ranges intersect', function (t) {
876876
[
877877
['1.3.0 || <1.0.0 >2.0.0', '1.3.0 || <1.0.0 >2.0.0', true],
878-
['<1.0.0 >2.0.0', '>0.0.0', true],
878+
['<1.0.0 >2.0.0', '>0.0.0', false],
879+
['>0.0.0', '<1.0.0 >2.0.0', false],
879880
['<1.0.0 >2.0.0', '>1.4.0 <1.6.0', false],
880881
['<1.0.0 >2.0.0', '>1.4.0 <1.6.0 || 2.0.0', false],
881882
['>1.0.0 <=2.0.0', '2.0.0', true],
882883
['<1.0.0 >=2.0.0', '2.1.0', false],
883884
['<1.0.0 >=2.0.0', '>1.4.0 <1.6.0 || 2.0.0', false],
884885
['1.5.x', '<1.5.0 || >=1.6.0', false],
885886
['<1.5.0 || >=1.6.0', '1.5.x', false],
886-
['<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]
887+
['<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],
888+
['<=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],
889+
['>=1.0.0', '<=1.0.0', true],
890+
['>1.0.0 <1.0.0', '<=0.0.0', false]
887891
].forEach(function (v) {
888892
var range1 = new Range(v[0])
889893
var range2 = new Range(v[1])

0 commit comments

Comments
 (0)