Skip to content

Commit

Permalink
fix: Handle prereleases properly in 'X - Y' ranges
Browse files Browse the repository at this point in the history
If includePrerelease is set, then you'd expect 1.0.0-pre and 2.0.0-pre
to be included in the range '1.0.0 - 2.0.0'.  You would NOT expect that
3.0.0-pre would be included in that range.

To accomplish this, the bounds of the range have to be set differently,
using the '-0' minimum prerelease version identifier, to include and
exclude prereleases appropriately when includePrerelease is set.
  • Loading branch information
isaacs committed Apr 13, 2020
1 parent 5d0dcda commit 1bd5bdd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
18 changes: 11 additions & 7 deletions classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Range {
range = range.trim()
// `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
range = range.replace(hr, hyphenReplace)
range = range.replace(hr, hyphenReplace(this.options.includePrerelease))
debug('hyphen replace', range)
// `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
Expand Down Expand Up @@ -384,27 +384,31 @@ const replaceStars = (comp, options) => {
// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0 <3.5.0
const hyphenReplace = ($0,
const hyphenReplace = incPr => ($0,
from, fM, fm, fp, fpr, fb,
to, tM, tm, tp, tpr, tb) => {
if (isX(fM)) {
from = ''
} else if (isX(fm)) {
from = `>=${fM}.0.0`
from = `>=${fM}.0.0${incPr ? '-0' : ''}`
} else if (isX(fp)) {
from = `>=${fM}.${fm}.0`
} else {
from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
} else if (fpr) {
from = `>=${from}`
} else {
from = `>=${from}${incPr ? '-0' : ''}`
}

if (isX(tM)) {
to = ''
} else if (isX(tm)) {
to = `<${+tM + 1}.0.0`
to = `<${+tM + 1}.0.0${incPr ? '-0' : ''}`
} else if (isX(tp)) {
to = `<${tM}.${+tm + 1}.0`
to = `<${tM}.${+tm + 1}.0${incPr ? '-0' : ''}`
} else if (tpr) {
to = `<=${tM}.${tm}.${tp}-${tpr}`
} else if (incPr) {
to = `<${tM}.${tm}.${+tp + 1}-0`
} else {
to = `<=${to}`
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/range-exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ module.exports = [
['^1.2.3-rc2', '2.0.0', { includePrerelease: true }],
['^1.0.0', '2.0.0-rc1', { includePrerelease: true }],
['^1.0.0', '2.0.0-rc1'],

['1 - 2', '3.0.0-pre', { includePrerelease: true }],
['1 - 2', '2.0.0-pre'],
['1 - 2', '1.0.0-pre'],
['1.0 - 2', '1.0.0-pre'],
]
3 changes: 3 additions & 0 deletions test/fixtures/range-include.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@ module.exports = [
['^1.0.0-rc2', '1.0.1-rc1', { includePrerelease: true }],
['^1.0.0', '1.0.1-rc1', { includePrerelease: true }],
['^1.0.0', '1.1.0-rc1', { includePrerelease: true }],
['1 - 2', '2.0.0-pre', { includePrerelease: true }],
['1 - 2', '1.0.0-pre', { includePrerelease: true }],
['1.0 - 2', '1.0.0-pre', { includePrerelease: true }],
]
5 changes: 5 additions & 0 deletions test/fixtures/range-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
// new Range().range will be '' in those cases
module.exports = [
['1.0.0 - 2.0.0', '>=1.0.0 <=2.0.0'],
['1.0.0 - 2.0.0', '>=1.0.0-0 <2.0.1-0', { includePrerelease: true }],
['1 - 2', '>=1.0.0 <3.0.0'],
['1 - 2', '>=1.0.0-0 <3.0.0-0', { includePrerelease: true }],
['1.0 - 2.0', '>=1.0.0 <2.1.0'],
['1.0 - 2.0', '>=1.0.0-0 <2.1.0-0', { includePrerelease: true }],
['1.0.0', '1.0.0', { loose: false }],
['>=*', '*'],
['', '*'],
Expand Down

0 comments on commit 1bd5bdd

Please sign in to comment.