Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Range {
// First reduce all whitespace as much as possible so we do not have to rely
// on potentially slow regexes like \s*. This is then stored and used for
// future error messages as well.
this.raw = range.trim().replace(SPACE_CHARACTERS, ' ')
this.raw = range.trim().replace(SPACE_CHARACTERS, ' ').replace(/\+[^ ]*/g, '')

// First, split on ||
this.set = this.raw
Expand Down Expand Up @@ -399,6 +399,11 @@ const replaceXRange = (comp, options) => {
const xp = xm || isX(p)
const anyX = xp

// Disallow prerelease with any X-range or partial version
if ((xM || xm || xp) && pr) {
throw new TypeError('Prerelease not allowed with X-ranges or partial versions')
}

if (gtlt === '=' && anyX) {
gtlt = ''
}
Expand Down
50 changes: 50 additions & 0 deletions test/classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,53 @@ test('cache', (t) => {
t.equal(r2.set[0][cached], true) // Will be true, showing it's cached.
t.end()
})

test('Build metadata is allowed and ignored for X-ranges and partials', t => {
const buildCases = [
'1.x.x+build >2.x.x+build',
'>=1.x.x+build <2.x.x+build',
'1.x.x+build || 2.x.x+build',
'1.x.x+build.123',
'1.x.x+meta-data',
'1.x.x+build.123 >2.x.x+meta-data',
'1.x.x+build <2.x.x+meta',
'>1.x.x+build <=2.x.x+meta',
' 1.x.x+build >2.x.x+build ',
]
t.plan(buildCases.length)
buildCases.forEach(range => {
t.doesNotThrow(() => new Range(range), `${range} should not throw`)
})
})

test('Build metadata with prerelease in X-ranges/partials throws', t => {
const cases = [
'1.x.x-alpha+build',
'1.x-alpha+build',
'1-alpha+build',
'>1.x.x-alpha+build',
'>=1.x.x-alpha+build <2.x.x+build',
'1.x.x-alpha+build || 2.x.x+build',
]
t.plan(cases.length)
cases.forEach(range => {
t.throws(() => new Range(range), TypeError, `${range} should throw TypeError`)
})
})

test('Prerelease is NOT allowed with X-ranges or partials', t => {
const prereleaseCases = [
'1.x-alpha',
'1-alpha',
'1.x.x-alpha',
'>1.x-alpha',
'>1-alpha',
'>1.x.x-alpha',
'1.x.x-alpha <2.x.x-alpha',
'>1.x.x-alpha <=2.x-alpha',
]
t.plan(prereleaseCases.length)
prereleaseCases.forEach(range => {
t.throws(() => new Range(range), TypeError, `${range} should throw TypeError`)
})
})