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: correctly increase version for "pre" versions #643

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 16 additions & 16 deletions classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SemVer {
// this isn't actually relevant for versions, but keep it so that we
// don't run into trouble passing this.options around.
this.includePrerelease = !!options.includePrerelease
this.isPrerelease = false

const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])

Expand Down Expand Up @@ -178,32 +179,28 @@ class SemVer {
inc (release, identifier, identifierBase) {
switch (release) {
case 'premajor':
this.prerelease.length = 0
this.patch = 0
this.minor = 0
this.major++
this.isPrerelease = true
this.inc('major', identifier, identifierBase)
this.inc('pre', identifier, identifierBase)
break
case 'preminor':
this.prerelease.length = 0
this.patch = 0
this.minor++
this.isPrerelease = true
this.inc('minor', identifier, identifierBase)
this.inc('pre', identifier, identifierBase)
break
case 'prepatch':
this.isPrerelease = true
// If this is already a prerelease, it will bump to the next version
// drop any prereleases that might already exist, since they are not
// relevant at this point.
this.prerelease.length = 0
this.inc('patch', identifier, identifierBase)
this.inc('pre', identifier, identifierBase)
break
// If the input is a non-prerelease version, this acts the same as
// prepatch.
case 'prerelease':
if (this.prerelease.length === 0) {
this.inc('patch', identifier, identifierBase)
}
this.isPrerelease = true
this.inc('patch', identifier, identifierBase)
this.inc('pre', identifier, identifierBase)
break

Expand All @@ -218,10 +215,10 @@ class SemVer {
this.prerelease.length === 0
) {
this.major++
this.prerelease = []
}
this.minor = 0
this.patch = 0
this.prerelease = []
break
case 'minor':
// If this is a pre-minor version, bump up to the same minor version.
Expand All @@ -230,19 +227,19 @@ class SemVer {
// 1.2.1 bumps to 1.3.0
if (this.patch !== 0 || this.prerelease.length === 0) {
this.minor++
this.prerelease = []
}
this.patch = 0
this.prerelease = []
break
case 'patch':
// If this is not a pre-release version, it will increment the patch.
// If it is a pre-release it will bump up to the same patch version.
// 1.2.0-5 patches to 1.2.0
// 1.2.0-5 patches to 1.2.1
// 1.2.0 patches to 1.2.1
if (this.prerelease.length === 0) {
if (this.prerelease.length === 0 || this.patch === 0) {
this.patch++
this.prerelease = []
}
this.prerelease = []
break
// This probably shouldn't be used publicly.
// 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
Expand Down Expand Up @@ -291,6 +288,9 @@ class SemVer {
default:
throw new Error(`invalid increment argument: ${release}`)
}
if (!this.isPrerelease) {
this.prerelease = []
}
this.raw = this.format()
if (this.build.length) {
this.raw += `+${this.build.join('.')}`
Expand Down
17 changes: 11 additions & 6 deletions test/fixtures/increments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = [
['1.2.3tag', 'major', '2.0.0', true],
['1.2.3-tag', 'major', '2.0.0'],
['1.2.3', 'fake', null],
['1.2.0-0', 'patch', '1.2.0'],
['1.2.0-0', 'patch', '1.2.1'],
['fake', 'major', null],
['1.2.3-4', 'major', '2.0.0'],
['1.2.3-4', 'minor', '1.3.0'],
Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports = [
['1.2.3tag', 'major', '2.0.0', true, 'dev'],
['1.2.3-tag', 'major', '2.0.0', false, 'dev'],
['1.2.3', 'fake', null, false, 'dev'],
['1.2.0-0', 'patch', '1.2.0', false, 'dev'],
['1.2.0-0', 'patch', '1.2.1', false, 'dev'],
['fake', 'major', null, false, 'dev'],
['1.2.3-4', 'major', '2.0.0', false, 'dev'],
['1.2.3-4', 'minor', '1.3.0', false, 'dev'],
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = [
['1.2.3-1.1', 'prerelease', '1.2.3-2.0', false, '2'],

// [version, inc, result, identifierIndex, loose, identifier]
['1.2.0-1', 'prerelease', '1.2.0-alpha.0', false, 'alpha', '0'],
['1.2.0-1', 'prerelease', '1.2.1-alpha.0', false, 'alpha', '0'],
['1.2.1', 'prerelease', '1.2.2-alpha.0', false, 'alpha', '0'],
['0.2.0', 'prerelease', '0.2.1-alpha.0', false, 'alpha', '0'],
['1.2.2', 'prerelease', '1.2.3-alpha.1', false, 'alpha', '1'],
Expand All @@ -107,7 +107,7 @@ module.exports = [
['1.2.3-1', 'preminor', '1.3.0-dev.0', false, 'dev'],
['1.2.0', 'prerelease', '1.2.1-1', false, '', '1'],

['1.2.0-1', 'prerelease', '1.2.0-alpha', false, 'alpha', false],
['1.2.0-1', 'prerelease', '1.2.1-alpha', false, 'alpha', false],
['1.2.1', 'prerelease', '1.2.2-alpha', false, 'alpha', false],
['1.2.2', 'prerelease', '1.2.3-alpha', false, 'alpha', false],
['1.2.0', 'prepatch', '1.2.1-dev', false, 'dev', false],
Expand All @@ -120,8 +120,13 @@ module.exports = [
['1.2.3-1', 'preminor', '1.3.0-dev', false, 'dev', false],
['1.2.3-dev', 'prerelease', null, false, 'dev', false],
['1.2.0-dev', 'premajor', '2.0.0-dev', false, 'dev', false],
['1.2.0-dev', 'preminor', '1.3.0-beta', false, 'beta', false],
['1.2.0-dev', 'preminor', '1.2.0-beta', false, 'beta', false],
['1.0.0-dev', 'premajor', '1.0.0-beta', false, 'beta', false],
['1.0.0-dev', 'prepatch', '1.0.1-beta', false, 'beta', false],
['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.0.0-rc.1+build.4', 'prerelease', '1.0.1-rc', false, 'rc', false],
['1.0.0-rc.1+build.4', 'premajor', '1.0.0-rc.2', false, 'rc', false],
['1.2.0-dev.2', 'preminor', '1.2.0-dev.3', false, 'dev', false],

]