Skip to content

fix: prerelease identifier starting with digits #781

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

Merged
merged 2 commits into from
May 8, 2025
Merged
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
5 changes: 2 additions & 3 deletions classes/semver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const debug = require('../internal/debug')
const { MAX_LENGTH, MAX_SAFE_INTEGER } = require('../internal/constants')
const { safeRe: re, safeSrc: src, t } = require('../internal/re')
const { safeRe: re, t } = require('../internal/re')

const parseOptions = require('../internal/parse-options')
const { compareIdentifiers } = require('../internal/identifiers')
Expand Down Expand Up @@ -182,8 +182,7 @@ class SemVer {
}
// Avoid an invalid semver results
if (identifier) {
const r = new RegExp(`^${this.options.loose ? src[t.PRERELEASELOOSE] : src[t.PRERELEASE]}$`)
const match = `-${identifier}`.match(r)
const match = `-${identifier}`.match(this.options.loose ? re[t.PRERELEASELOOSE] : re[t.PRERELEASE])
if (!match || match[1] !== identifier) {
throw new Error(`invalid identifier: ${identifier}`)
}
Expand Down
10 changes: 6 additions & 4 deletions internal/re.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ createToken('MAINVERSIONLOOSE', `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +

// ## Pre-release Version Identifier
// A numeric identifier, or a non-numeric identifier.
// Non-numberic identifiers include numberic identifiers but can be longer.
// Therefore non-numberic identifiers must go first.

createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NUMERICIDENTIFIER]
}|${src[t.NONNUMERICIDENTIFIER]})`)
createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NONNUMERICIDENTIFIER]
}|${src[t.NUMERICIDENTIFIER]})`)

createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NUMERICIDENTIFIERLOOSE]
}|${src[t.NONNUMERICIDENTIFIER]})`)
createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NONNUMERICIDENTIFIER]
}|${src[t.NUMERICIDENTIFIERLOOSE]})`)

// ## Pre-release Version
// Hyphen, followed by one or more dot-separated pre-release version
Expand Down
17 changes: 16 additions & 1 deletion test/classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ const SemVer = require('../../classes/semver')
const increments = require('../fixtures/increments.js')
const comparisons = require('../fixtures/comparisons.js')
const equality = require('../fixtures/equality.js')
const invalidVersions = require('../fixtures/invalid-versions')
const invalidVersions = require('../fixtures/invalid-versions.js')
const validVersions = require('../fixtures/valid-versions.js')

test('valid versions', t => {
t.plan(validVersions.length)
validVersions.forEach(([v, major, minor, patch, prerelease, build]) => t.test(v, t => {
const s = new SemVer(v)
t.strictSame(s.major, major)
t.strictSame(s.minor, minor)
t.strictSame(s.patch, patch)
t.strictSame(s.prerelease, prerelease)
t.strictSame(s.build, build)
t.strictSame(s.raw, v)
t.end()
}))
})

test('comparisons', t => {
t.plan(comparisons.length)
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/valid-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// [version, major, minor, patch, prerelease[], build[]]
module.exports = [
['1.0.0', 1, 0, 0, [], []],
['2.1.0', 2, 1, 0, [], []],
['3.2.1', 3, 2, 1, [], []],
['v1.2.3', 1, 2, 3, [], []],

// prerelease
['1.2.3-0', 1, 2, 3, [0], []],
['1.2.3-123', 1, 2, 3, [123], []],
['1.2.3-1.2.3', 1, 2, 3, [1, 2, 3], []],
['1.2.3-1a', 1, 2, 3, ['1a'], []],
['1.2.3-a1', 1, 2, 3, ['a1'], []],
['1.2.3-alpha', 1, 2, 3, ['alpha'], []],
['1.2.3-alpha.1', 1, 2, 3, ['alpha', 1], []],
['1.2.3-alpha-1', 1, 2, 3, ['alpha-1'], []],
['1.2.3-alpha-.-beta', 1, 2, 3, ['alpha-', '-beta'], []],

// build
['1.2.3+456', 1, 2, 3, [], ['456']],
['1.2.3+build', 1, 2, 3, [], ['build']],
['1.2.3+new-build', 1, 2, 3, [], ['new-build']],
['1.2.3+build.1', 1, 2, 3, [], ['build', '1']],
['1.2.3+build.1a', 1, 2, 3, [], ['build', '1a']],
['1.2.3+build.a1', 1, 2, 3, [], ['build', 'a1']],
['1.2.3+build.alpha', 1, 2, 3, [], ['build', 'alpha']],
['1.2.3+build.alpha.beta', 1, 2, 3, [], ['build', 'alpha', 'beta']],

// mixed
['1.2.3-alpha+build', 1, 2, 3, ['alpha'], ['build']],
]
7 changes: 7 additions & 0 deletions test/functions/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ test('coerce tests', (t) => {
['1.2.3.4-rc.5', '1.2.3', { includePrerelease: true }],
['1.2.3.4+rev.6', '1.2.3', { includePrerelease: true }],

['1.0.0-1a', '1.0.0-1a', { includePrerelease: true }],
['1.0.0-alpha.12ab', '1.0.0-alpha.12ab', { includePrerelease: true }],
['1.0.0-alpha.1234.23cd', '1.0.0-alpha.1234.23cd', { includePrerelease: true }],
['1.0.0-nightly.abc123', '1.0.0-nightly.abc123', { includePrerelease: true }],
['1.0.0-nightly.abcdef', '1.0.0-nightly.abcdef', { includePrerelease: true }],
['1.0.0-nightly.123456', '1.0.0-nightly.123456', { includePrerelease: true }],

['1+rev.6', '1.0.0+rev.6', { includePrerelease: true }],
['1.2+rev.6', '1.2.0+rev.6', { includePrerelease: true }],
['1.2.3+rev.6', '1.2.3+rev.6', { includePrerelease: true }],
Expand Down
Loading