Skip to content

Commit c44e124

Browse files
committed
Handle X ranges properly in includePrelease mode
Append a `-0` prerelease tag on the parsed comparators when in includePrerelease mode. Otherwise, 2.x will be satisfied by 3.0.0-beta, since it's in `>=2.0.0 <3.0.0`. However, `2.0.0-beta` will _not_ satisfy this range. By appending the `-0`, the version set is pinned to the appropriate boundary. Note that doing this in non-prerelease-including mode would be a mistake! Appending the `-0` to the comparator means that prereleases in the tuple are allowed, so `2.0.0-beta` would match `2.x` in non-prerelease-including mode as well. However, if we're including prereleases, then their inclusion is intentional. Fix: #282
1 parent ba19e8f commit c44e124

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

semver.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1208,11 +1208,12 @@ function replaceXRange (comp, options) {
12081208
}
12091209
}
12101210

1211-
ret = gtlt + M + '.' + m + '.' + p
1211+
ret = gtlt + M + '.' + m + '.' + p + pr
12121212
} else if (xm) {
1213-
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0'
1213+
ret = '>=' + M + '.0.0' + pr + ' <' + (+M + 1) + '.0.0' + pr
12141214
} else if (xp) {
1215-
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0'
1215+
ret = '>=' + M + '.' + m + '.0' + pr +
1216+
' <' + M + '.' + (+m + 1) + '.0' + pr
12161217
}
12171218

12181219
debug('xRange return', ret)

test/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,13 @@ test('range tests', function (t) {
249249
['1.0.0 - x', '1.9.7'],
250250
['1.x - x', '1.9.7'],
251251
['<=7.x', '7.9.9'],
252+
['2.x', '2.0.0-pre.0', { includePrerelease: true }],
253+
['2.x', '2.1.0-pre.0', { includePrerelease: true }],
252254
].forEach(function (v) {
253255
var range = v[0]
254256
var ver = v[1]
255-
var loose = v[2]
256-
t.ok(satisfies(ver, range, loose), range + ' satisfied by ' + ver)
257+
var options = v[2]
258+
t.ok(satisfies(ver, range, options), range + ' satisfied by ' + ver)
257259
})
258260
t.end()
259261
})
@@ -338,11 +340,12 @@ test('negative range tests', function (t) {
338340
// invalid versions never satisfy, but shouldn't throw
339341
['*', 'not a version'],
340342
['>=2', 'glorp'],
343+
['2.x', '3.0.0-pre.0', { includePrerelease: true }],
341344
].forEach(function (v) {
342345
var range = v[0]
343346
var ver = v[1]
344-
var loose = v[2]
345-
var found = satisfies(ver, range, loose)
347+
var options = v[2]
348+
var found = satisfies(ver, range, options)
346349
t.ok(!found, ver + ' not satisfied by ' + range)
347350
})
348351
t.end()

0 commit comments

Comments
 (0)