Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add identifier validation to inc() #754

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 14 additions & 5 deletions classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SemVer {

if (version instanceof SemVer) {
if (version.loose === !!options.loose &&
version.includePrerelease === !!options.includePrerelease) {
version.includePrerelease === !!options.includePrerelease) {
return version
} else {
version = version.version
Expand Down Expand Up @@ -176,6 +176,19 @@ class SemVer {
// preminor will bump the version up to the next minor release, and immediately
// down to pre-release. premajor and prepatch work the same way.
inc (release, identifier, identifierBase) {
if (release.startsWith('pre')) {
if (!identifier && identifierBase === false) {
throw new Error('invalid increment argument: identifier is empty')
}
// Avoid an invalid semver results
if (identifier) {
const match = `-${identifier}`.match(this.options.loose ? re[t.PRERELEASELOOSE] : re[t.PRERELEASE])
if (!match || match[1] !== identifier) {
throw new Error(`invalid identifier: ${identifier}`)
}
}
}

switch (release) {
case 'premajor':
this.prerelease.length = 0
Expand Down Expand Up @@ -249,10 +262,6 @@ class SemVer {
case 'pre': {
const base = Number(identifierBase) ? 1 : 0

if (!identifier && identifierBase === false) {
throw new Error('invalid increment argument: identifier is empty')
}

if (this.prerelease.length === 0) {
this.prerelease = [base]
} else {
Expand Down
28 changes: 28 additions & 0 deletions test/classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ test('incrementing', t => {
}))
})

test('invalid increments', (t) => {
t.throws(
() => new SemVer('1.2.3').inc('prerelease', '', false),
Error('invalid increment argument: identifier is empty')
)
t.throws(
() => new SemVer('1.2.3-dev').inc('prerelease', 'dev', false),
Error('invalid increment argument: identifier already exists')
)
t.throws(
() => new SemVer('1.2.3').inc('prerelease', 'invalid/preid'),
Error('invalid identifier: invalid/preid')
)

t.end()
})

test('increment side-effects', (t) => {
const v = new SemVer('1.0.0')
try {
v.inc('prerelease', 'hot/mess')
} catch (er) {
// ignore but check that the version has not changed
}
t.equal(v.toString(), '1.0.0')
t.end()
})

test('compare main vs pre', (t) => {
const s = new SemVer('1.2.3')
t.equal(s.compareMain('2.3.4'), -1)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/increments.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ module.exports = [
['1.2.0-dev', 'prepatch', '1.2.1-dev', false, 'dev', false],
['1.2.0', 'prerelease', null, false, '', false],
['1.0.0-rc.1+build.4', 'prerelease', '1.0.0-rc.2', 'rc', false],
['1.2.0', 'prerelease', null, false, 'invalid/preid'],
['1.2.0', 'prerelease', null, false, 'invalid+build'],
['1.2.0beta', 'prerelease', null, { loose: true }, 'invalid/preid'],
]
Loading