Skip to content

Commit 1bd5bdd

Browse files
committed
fix: Handle prereleases properly in 'X - Y' ranges
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.
1 parent 5d0dcda commit 1bd5bdd

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

classes/range.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Range {
6868
range = range.trim()
6969
// `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
7070
const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
71-
range = range.replace(hr, hyphenReplace)
71+
range = range.replace(hr, hyphenReplace(this.options.includePrerelease))
7272
debug('hyphen replace', range)
7373
// `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
7474
range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
@@ -384,27 +384,31 @@ const replaceStars = (comp, options) => {
384384
// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
385385
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
386386
// 1.2 - 3.4 => >=1.2.0 <3.5.0
387-
const hyphenReplace = ($0,
387+
const hyphenReplace = incPr => ($0,
388388
from, fM, fm, fp, fpr, fb,
389389
to, tM, tm, tp, tpr, tb) => {
390390
if (isX(fM)) {
391391
from = ''
392392
} else if (isX(fm)) {
393-
from = `>=${fM}.0.0`
393+
from = `>=${fM}.0.0${incPr ? '-0' : ''}`
394394
} else if (isX(fp)) {
395-
from = `>=${fM}.${fm}.0`
396-
} else {
395+
from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
396+
} else if (fpr) {
397397
from = `>=${from}`
398+
} else {
399+
from = `>=${from}${incPr ? '-0' : ''}`
398400
}
399401

400402
if (isX(tM)) {
401403
to = ''
402404
} else if (isX(tm)) {
403-
to = `<${+tM + 1}.0.0`
405+
to = `<${+tM + 1}.0.0${incPr ? '-0' : ''}`
404406
} else if (isX(tp)) {
405-
to = `<${tM}.${+tm + 1}.0`
407+
to = `<${tM}.${+tm + 1}.0${incPr ? '-0' : ''}`
406408
} else if (tpr) {
407409
to = `<=${tM}.${tm}.${tp}-${tpr}`
410+
} else if (incPr) {
411+
to = `<${tM}.${tm}.${+tp + 1}-0`
408412
} else {
409413
to = `<=${to}`
410414
}

test/fixtures/range-exclude.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@ module.exports = [
8080
['^1.2.3-rc2', '2.0.0', { includePrerelease: true }],
8181
['^1.0.0', '2.0.0-rc1', { includePrerelease: true }],
8282
['^1.0.0', '2.0.0-rc1'],
83+
84+
['1 - 2', '3.0.0-pre', { includePrerelease: true }],
85+
['1 - 2', '2.0.0-pre'],
86+
['1 - 2', '1.0.0-pre'],
87+
['1.0 - 2', '1.0.0-pre'],
8388
]

test/fixtures/range-include.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,7 @@ module.exports = [
114114
['^1.0.0-rc2', '1.0.1-rc1', { includePrerelease: true }],
115115
['^1.0.0', '1.0.1-rc1', { includePrerelease: true }],
116116
['^1.0.0', '1.1.0-rc1', { includePrerelease: true }],
117+
['1 - 2', '2.0.0-pre', { includePrerelease: true }],
118+
['1 - 2', '1.0.0-pre', { includePrerelease: true }],
119+
['1.0 - 2', '1.0.0-pre', { includePrerelease: true }],
117120
]

test/fixtures/range-parse.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
// new Range().range will be '' in those cases
55
module.exports = [
66
['1.0.0 - 2.0.0', '>=1.0.0 <=2.0.0'],
7+
['1.0.0 - 2.0.0', '>=1.0.0-0 <2.0.1-0', { includePrerelease: true }],
8+
['1 - 2', '>=1.0.0 <3.0.0'],
9+
['1 - 2', '>=1.0.0-0 <3.0.0-0', { includePrerelease: true }],
10+
['1.0 - 2.0', '>=1.0.0 <2.1.0'],
11+
['1.0 - 2.0', '>=1.0.0-0 <2.1.0-0', { includePrerelease: true }],
712
['1.0.0', '1.0.0', { loose: false }],
813
['>=*', '*'],
914
['', '*'],

0 commit comments

Comments
 (0)