Skip to content

Commit

Permalink
Always exclude prereleases from range maximums
Browse files Browse the repository at this point in the history
In semver v4, the ranges were simplified in 2331a9e "to remove -0
everywhere". This had the unintended side effect of including the
endpoint version's prerelease versions in the resulting range, as
lt('1.0.0-0', '1.0.0') === true.

Tests are updated, adding -0 to the maximal endpoints. One test is moved
from range-include to range-exclude, comparing 2.0.0-rc1 against ^1.0.0

Fixes #223
Fixes #254

EDIT(isaacs): Updated to resolve conflicts on master branch.

PR-URL: #320
Credit: @eemeli
Close: #320
Reviewed-by: @isaacs
  • Loading branch information
eemeli authored and isaacs committed Apr 14, 2020
1 parent f27dcf5 commit 059a5ad
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 115 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,37 +256,37 @@ inclusive range, then all versions that start with the supplied parts
of the tuple are accepted, but nothing that would be greater than the
provided tuple parts.

* `1.2.3 - 2.3` := `>=1.2.3 <2.4.0`
* `1.2.3 - 2` := `>=1.2.3 <3.0.0`
* `1.2.3 - 2.3` := `>=1.2.3 <2.4.0-0`
* `1.2.3 - 2` := `>=1.2.3 <3.0.0-0`

#### X-Ranges `1.2.x` `1.X` `1.2.*` `*`

Any of `X`, `x`, or `*` may be used to "stand in" for one of the
numeric values in the `[major, minor, patch]` tuple.

* `*` := `>=0.0.0` (Any version satisfies)
* `1.x` := `>=1.0.0 <2.0.0` (Matching major version)
* `1.2.x` := `>=1.2.0 <1.3.0` (Matching major and minor versions)
* `1.x` := `>=1.0.0 <2.0.0-0` (Matching major version)
* `1.2.x` := `>=1.2.0 <1.3.0-0` (Matching major and minor versions)

A partial version range is treated as an X-Range, so the special
character is in fact optional.

* `""` (empty string) := `*` := `>=0.0.0`
* `1` := `1.x.x` := `>=1.0.0 <2.0.0`
* `1.2` := `1.2.x` := `>=1.2.0 <1.3.0`
* `1` := `1.x.x` := `>=1.0.0 <2.0.0-0`
* `1.2` := `1.2.x` := `>=1.2.0 <1.3.0-0`

#### Tilde Ranges `~1.2.3` `~1.2` `~1`

Allows patch-level changes if a minor version is specified on the
comparator. Allows minor-level changes if not.

* `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0`
* `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0` (Same as `1.2.x`)
* `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0` (Same as `1.x`)
* `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0`
* `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0` (Same as `0.2.x`)
* `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0` (Same as `0.x`)
* `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0` Note that prereleases in
* `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0-0`
* `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0-0` (Same as `1.2.x`)
* `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0-0` (Same as `1.x`)
* `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0-0`
* `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0-0` (Same as `0.2.x`)
* `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0-0` (Same as `0.x`)
* `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0-0` Note that prereleases in
the `1.2.3` version will be allowed, if they are greater than or
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
`1.2.4-beta.2` would not, because it is a prerelease of a
Expand All @@ -308,32 +308,32 @@ However, it presumes that there will *not* be breaking changes between
`0.2.4` and `0.2.5`. It allows for changes that are presumed to be
additive (but non-breaking), according to commonly observed practices.

* `^1.2.3` := `>=1.2.3 <2.0.0`
* `^0.2.3` := `>=0.2.3 <0.3.0`
* `^0.0.3` := `>=0.0.3 <0.0.4`
* `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0` Note that prereleases in
* `^1.2.3` := `>=1.2.3 <2.0.0-0`
* `^0.2.3` := `>=0.2.3 <0.3.0-0`
* `^0.0.3` := `>=0.0.3 <0.0.4-0`
* `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0-0` Note that prereleases in
the `1.2.3` version will be allowed, if they are greater than or
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
`1.2.4-beta.2` would not, because it is a prerelease of a
different `[major, minor, patch]` tuple.
* `^0.0.3-beta` := `>=0.0.3-beta <0.0.4` Note that prereleases in the
* `^0.0.3-beta` := `>=0.0.3-beta <0.0.4-0` Note that prereleases in the
`0.0.3` version *only* will be allowed, if they are greater than or
equal to `beta`. So, `0.0.3-pr.2` would be allowed.

When parsing caret ranges, a missing `patch` value desugars to the
number `0`, but will allow flexibility within that value, even if the
major and minor versions are both `0`.

* `^1.2.x` := `>=1.2.0 <2.0.0`
* `^0.0.x` := `>=0.0.0 <0.1.0`
* `^0.0` := `>=0.0.0 <0.1.0`
* `^1.2.x` := `>=1.2.0 <2.0.0-0`
* `^0.0.x` := `>=0.0.0 <0.1.0-0`
* `^0.0` := `>=0.0.0 <0.1.0-0`

A missing `minor` and `patch` values will desugar to zero, but also
allow flexibility within those values, even if the major version is
zero.

* `^1.x` := `>=1.0.0 <2.0.0`
* `^0.x` := `>=0.0.0 <1.0.0`
* `^1.x` := `>=1.0.0 <2.0.0-0`
* `^0.x` := `>=0.0.0 <1.0.0-0`

### Range Grammar

Expand Down
62 changes: 31 additions & 31 deletions classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ const parseComparator = (comp, options) => {
const isX = id => !id || id.toLowerCase() === 'x' || id === '*'

// ~, ~> --> * (any, kinda silly)
// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0-0
// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0-0
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0-0
// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
const replaceTildes = (comp, options) =>
comp.trim().split(/\s+/).map((comp) => {
return replaceTilde(comp, options)
Expand All @@ -211,18 +211,18 @@ const replaceTilde = (comp, options) => {
if (isX(M)) {
ret = ''
} else if (isX(m)) {
ret = `>=${M}.0.0 <${+M + 1}.0.0`
ret = `>=${M}.0.0 <${+M + 1}.0.0-0`
} else if (isX(p)) {
// ~1.2 == >=1.2.0 <1.3.0
ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0`
// ~1.2 == >=1.2.0 <1.3.0-0
ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0-0`
} else if (pr) {
debug('replaceTilde pr', pr)
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${+m + 1}.0`
} <${M}.${+m + 1}.0-0`
} else {
// ~1.2.3 == >=1.2.3 <1.3.0
// ~1.2.3 == >=1.2.3 <1.3.0-0
ret = `>=${M}.${m}.${p
} <${M}.${+m + 1}.0`
} <${M}.${+m + 1}.0-0`
}

debug('tilde return', ret)
Expand All @@ -231,11 +231,11 @@ const replaceTilde = (comp, options) => {
}

// ^ --> * (any, kinda silly)
// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
// ^1.2.3 --> >=1.2.3 <2.0.0
// ^1.2.0 --> >=1.2.0 <2.0.0
// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
// ^1.2.3 --> >=1.2.3 <2.0.0-0
// ^1.2.0 --> >=1.2.0 <2.0.0-0
const replaceCarets = (comp, options) =>
comp.trim().split(/\s+/).map((comp) => {
return replaceCaret(comp, options)
Expand All @@ -252,40 +252,40 @@ const replaceCaret = (comp, options) => {
if (isX(M)) {
ret = ''
} else if (isX(m)) {
ret = `>=${M}.0.0${z} <${+M + 1}.0.0${z}`
ret = `>=${M}.0.0${z} <${+M + 1}.0.0-0`
} else if (isX(p)) {
if (M === '0') {
ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0${z}`
ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0-0`
} else {
ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0${z}`
ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0-0`
}
} else if (pr) {
debug('replaceCaret pr', pr)
if (M === '0') {
if (m === '0') {
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${m}.${+p + 1}${z}`
} <${M}.${m}.${+p + 1}-0`
} else {
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${+m + 1}.0${z}`
} <${M}.${+m + 1}.0-0`
}
} else {
ret = `>=${M}.${m}.${p}-${pr
} <${+M + 1}.0.0${z}`
} <${+M + 1}.0.0-0`
}
} else {
debug('no pr')
if (M === '0') {
if (m === '0') {
ret = `>=${M}.${m}.${p
}${z} <${M}.${m}.${+p + 1}${z}`
}${z} <${M}.${m}.${+p + 1}-0`
} else {
ret = `>=${M}.${m}.${p
}${z} <${M}.${+m + 1}.0${z}`
}${z} <${M}.${+m + 1}.0-0`
}
} else {
ret = `>=${M}.${m}.${p
} <${+M + 1}.0.0${z}`
} <${+M + 1}.0.0-0`
}
}

Expand Down Expand Up @@ -360,10 +360,10 @@ const replaceXRange = (comp, options) => {

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

debug('xRange return', ret)
Expand All @@ -389,8 +389,8 @@ const replaceGTE0 = (comp, options) => {
// This function is passed to string.replace(re[t.HYPHENRANGE])
// M, m, patch, prerelease, build
// 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
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0-0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0 <3.5.0-0
const hyphenReplace = incPr => ($0,
from, fM, fm, fp, fpr, fb,
to, tM, tm, tp, tpr, tb) => {
Expand All @@ -409,9 +409,9 @@ const hyphenReplace = incPr => ($0,
if (isX(tM)) {
to = ''
} else if (isX(tm)) {
to = `<${+tM + 1}.0.0${incPr ? '-0' : ''}`
to = `<${+tM + 1}.0.0-0`
} else if (isX(tp)) {
to = `<${tM}.${+tm + 1}.0${incPr ? '-0' : ''}`
to = `<${tM}.${+tm + 1}.0-0`
} else if (tpr) {
to = `<=${tM}.${tm}.${tp}-${tpr}`
} else if (incPr) {
Expand Down
2 changes: 1 addition & 1 deletion test/classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('negative range tests', t => {
test('strict vs loose ranges', (t) => {
[
['>=01.02.03', '>=1.2.3'],
['~1.02.03beta', '>=1.2.3-beta <1.3.0']
['~1.02.03beta', '>=1.2.3-beta <1.3.0-0']
].forEach(([loose, comps]) => {
t.throws(() => new Range(loose))
t.equal(new Range(loose, true).range, comps)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/range-exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module.exports = [

['2.x', '3.0.0-pre.0', { includePrerelease: true }],
['^1.0.0', '1.0.0-rc1', { includePrerelease: true }],
['^1.0.0', '2.0.0-rc1', { includePrerelease: true }],
['^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'],
Expand Down
70 changes: 35 additions & 35 deletions test/fixtures/range-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
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 <3.0.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 <2.1.0-0'],
['1.0 - 2.0', '>=1.0.0-0 <2.1.0-0', { includePrerelease: true }],
['1.0.0', '1.0.0', { loose: false }],
['>=*', '*'],
Expand All @@ -17,7 +17,7 @@ module.exports = [
['>=1.0.0', '>=1.0.0'],
['>1.0.0', '>1.0.0'],
['<=2.0.0', '<=2.0.0'],
['1', '>=1.0.0 <2.0.0'],
['1', '>=1.0.0 <2.0.0-0'],
['<=2.0.0', '<=2.0.0'],
['<=2.0.0', '<=2.0.0'],
['<2.0.0', '<2.0.0'],
Expand All @@ -39,50 +39,50 @@ module.exports = [
['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
['||', '||'],
['2.x.x', '>=2.0.0 <3.0.0'],
['1.2.x', '>=1.2.0 <1.3.0'],
['1.2.x || 2.x', '>=1.2.0 <1.3.0||>=2.0.0 <3.0.0'],
['1.2.x || 2.x', '>=1.2.0 <1.3.0||>=2.0.0 <3.0.0'],
['2.x.x', '>=2.0.0 <3.0.0-0'],
['1.2.x', '>=1.2.0 <1.3.0-0'],
['1.2.x || 2.x', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'],
['1.2.x || 2.x', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'],
['x', '*'],
['2.*.*', '>=2.0.0 <3.0.0'],
['1.2.*', '>=1.2.0 <1.3.0'],
['1.2.* || 2.*', '>=1.2.0 <1.3.0||>=2.0.0 <3.0.0'],
['2.*.*', '>=2.0.0 <3.0.0-0'],
['1.2.*', '>=1.2.0 <1.3.0-0'],
['1.2.* || 2.*', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'],
['*', '*'],
['2', '>=2.0.0 <3.0.0'],
['2.3', '>=2.3.0 <2.4.0'],
['~2.4', '>=2.4.0 <2.5.0'],
['~2.4', '>=2.4.0 <2.5.0'],
['~>3.2.1', '>=3.2.1 <3.3.0'],
['~1', '>=1.0.0 <2.0.0'],
['~>1', '>=1.0.0 <2.0.0'],
['~> 1', '>=1.0.0 <2.0.0'],
['~1.0', '>=1.0.0 <1.1.0'],
['~ 1.0', '>=1.0.0 <1.1.0'],
['^0', '<1.0.0'],
['^ 1', '>=1.0.0 <2.0.0'],
['^0.1', '>=0.1.0 <0.2.0'],
['^1.0', '>=1.0.0 <2.0.0'],
['^1.2', '>=1.2.0 <2.0.0'],
['^0.0.1', '>=0.0.1 <0.0.2'],
['^0.0.1-beta', '>=0.0.1-beta <0.0.2'],
['^0.1.2', '>=0.1.2 <0.2.0'],
['^1.2.3', '>=1.2.3 <2.0.0'],
['^1.2.3-beta.4', '>=1.2.3-beta.4 <2.0.0'],
['2', '>=2.0.0 <3.0.0-0'],
['2.3', '>=2.3.0 <2.4.0-0'],
['~2.4', '>=2.4.0 <2.5.0-0'],
['~2.4', '>=2.4.0 <2.5.0-0'],
['~>3.2.1', '>=3.2.1 <3.3.0-0'],
['~1', '>=1.0.0 <2.0.0-0'],
['~>1', '>=1.0.0 <2.0.0-0'],
['~> 1', '>=1.0.0 <2.0.0-0'],
['~1.0', '>=1.0.0 <1.1.0-0'],
['~ 1.0', '>=1.0.0 <1.1.0-0'],
['^0', '<1.0.0-0'],
['^ 1', '>=1.0.0 <2.0.0-0'],
['^0.1', '>=0.1.0 <0.2.0-0'],
['^1.0', '>=1.0.0 <2.0.0-0'],
['^1.2', '>=1.2.0 <2.0.0-0'],
['^0.0.1', '>=0.0.1 <0.0.2-0'],
['^0.0.1-beta', '>=0.0.1-beta <0.0.2-0'],
['^0.1.2', '>=0.1.2 <0.2.0-0'],
['^1.2.3', '>=1.2.3 <2.0.0-0'],
['^1.2.3-beta.4', '>=1.2.3-beta.4 <2.0.0-0'],
['<1', '<1.0.0'],
['< 1', '<1.0.0'],
['>=1', '>=1.0.0'],
['>= 1', '>=1.0.0'],
['<1.2', '<1.2.0'],
['< 1.2', '<1.2.0'],
['1', '>=1.0.0 <2.0.0'],
['1', '>=1.0.0 <2.0.0-0'],
['>01.02.03', '>1.2.3', true],
['>01.02.03', null],
['~1.2.3beta', '>=1.2.3-beta <1.3.0', { loose: true }],
['~1.2.3beta', '>=1.2.3-beta <1.3.0-0', { loose: true }],
['~1.2.3beta', null],
['^ 1.2 ^ 1', '>=1.2.0 <2.0.0 >=1.0.0 <2.0.0'],
['^ 1.2 ^ 1', '>=1.2.0 <2.0.0-0 >=1.0.0 <2.0.0-0'],
['1.2 - 3.4.5', '>=1.2.0 <=3.4.5'],
['1.2.3 - 3.4', '>=1.2.3 <3.5.0'],
['1.2 - 3.4', '>=1.2.0 <3.5.0'],
['1.2.3 - 3.4', '>=1.2.3 <3.5.0-0'],
['1.2 - 3.4', '>=1.2.0 <3.5.0-0'],
['>1', '>=2.0.0'],
['>1.2', '>=1.3.0'],
['>X', '<0.0.0-0'],
Expand Down
Loading

0 comments on commit 059a5ad

Please sign in to comment.