Skip to content

Commit

Permalink
Handle X ranges properly in includePrelease mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
isaacs committed Jul 1, 2019
1 parent ba19e8f commit c44e124
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,11 +1208,12 @@ function replaceXRange (comp, options) {
}
}

ret = gtlt + M + '.' + m + '.' + p
ret = gtlt + M + '.' + m + '.' + p + pr
} else if (xm) {
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0'
ret = '>=' + M + '.0.0' + pr + ' <' + (+M + 1) + '.0.0' + pr
} else if (xp) {
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0'
ret = '>=' + M + '.' + m + '.0' + pr +
' <' + M + '.' + (+m + 1) + '.0' + pr
}

debug('xRange return', ret)
Expand Down
11 changes: 7 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ test('range tests', function (t) {
['1.0.0 - x', '1.9.7'],
['1.x - x', '1.9.7'],
['<=7.x', '7.9.9'],
['2.x', '2.0.0-pre.0', { includePrerelease: true }],
['2.x', '2.1.0-pre.0', { includePrerelease: true }],
].forEach(function (v) {
var range = v[0]
var ver = v[1]
var loose = v[2]
t.ok(satisfies(ver, range, loose), range + ' satisfied by ' + ver)
var options = v[2]
t.ok(satisfies(ver, range, options), range + ' satisfied by ' + ver)
})
t.end()
})
Expand Down Expand Up @@ -338,11 +340,12 @@ test('negative range tests', function (t) {
// invalid versions never satisfy, but shouldn't throw
['*', 'not a version'],
['>=2', 'glorp'],
['2.x', '3.0.0-pre.0', { includePrerelease: true }],
].forEach(function (v) {
var range = v[0]
var ver = v[1]
var loose = v[2]
var found = satisfies(ver, range, loose)
var options = v[2]
var found = satisfies(ver, range, options)
t.ok(!found, ver + ' not satisfied by ' + range)
})
t.end()
Expand Down

0 comments on commit c44e124

Please sign in to comment.