Skip to content

Commit 4299208

Browse files
author
i529015
committed
x-range partial version build metadata support
1 parent d17aebf commit 4299208

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

classes/range.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Range {
3333
// First reduce all whitespace as much as possible so we do not have to rely
3434
// on potentially slow regexes like \s*. This is then stored and used for
3535
// future error messages as well.
36-
this.raw = range.trim().replace(SPACE_CHARACTERS, ' ')
36+
this.raw = range.trim().replace(SPACE_CHARACTERS, ' ').replace(/\+[^ ]*/g, '')
3737

3838
// First, split on ||
3939
this.set = this.raw
@@ -399,6 +399,11 @@ const replaceXRange = (comp, options) => {
399399
const xp = xm || isX(p)
400400
const anyX = xp
401401

402+
// Disallow prerelease with any X-range or partial version
403+
if ((xM || xm || xp) && pr) {
404+
throw new TypeError('Prerelease not allowed with X-ranges or partial versions')
405+
}
406+
402407
if (gtlt === '=' && anyX) {
403408
gtlt = ''
404409
}

test/classes/range.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,53 @@ test('cache', (t) => {
126126
t.equal(r2.set[0][cached], true) // Will be true, showing it's cached.
127127
t.end()
128128
})
129+
130+
test('Build metadata is allowed and ignored for X-ranges and partials', t => {
131+
const buildCases = [
132+
'1.x.x+build >2.x.x+build',
133+
'>=1.x.x+build <2.x.x+build',
134+
'1.x.x+build || 2.x.x+build',
135+
'1.x.x+build.123',
136+
'1.x.x+meta-data',
137+
'1.x.x+build.123 >2.x.x+meta-data',
138+
'1.x.x+build <2.x.x+meta',
139+
'>1.x.x+build <=2.x.x+meta',
140+
' 1.x.x+build >2.x.x+build ',
141+
]
142+
t.plan(buildCases.length)
143+
buildCases.forEach(range => {
144+
t.doesNotThrow(() => new Range(range), `${range} should not throw`)
145+
})
146+
})
147+
148+
test('Build metadata with prerelease in X-ranges/partials throws', t => {
149+
const cases = [
150+
'1.x.x-alpha+build',
151+
'1.x-alpha+build',
152+
'1-alpha+build',
153+
'>1.x.x-alpha+build',
154+
'>=1.x.x-alpha+build <2.x.x+build',
155+
'1.x.x-alpha+build || 2.x.x+build',
156+
]
157+
t.plan(cases.length)
158+
cases.forEach(range => {
159+
t.throws(() => new Range(range), TypeError, `${range} should throw TypeError`)
160+
})
161+
})
162+
163+
test('Prerelease is NOT allowed with X-ranges or partials', t => {
164+
const prereleaseCases = [
165+
'1.x-alpha',
166+
'1-alpha',
167+
'1.x.x-alpha',
168+
'>1.x-alpha',
169+
'>1-alpha',
170+
'>1.x.x-alpha',
171+
'1.x.x-alpha <2.x.x-alpha',
172+
'>1.x.x-alpha <=2.x-alpha',
173+
]
174+
t.plan(prereleaseCases.length)
175+
prereleaseCases.forEach(range => {
176+
t.throws(() => new Range(range), TypeError, `${range} should throw TypeError`)
177+
})
178+
})

0 commit comments

Comments
 (0)