Skip to content

Commit

Permalink
fix(inc): fully capture prerelease identifier (#764)
Browse files Browse the repository at this point in the history
Closes: #763
  • Loading branch information
wraithgar authored Feb 3, 2025
1 parent 2cfcbb5 commit af761c0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 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, t } = require('../internal/re')
const { safeRe: re, safeSrc: src, t } = require('../internal/re')

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

Expand Down Expand Up @@ -42,6 +43,7 @@ const createToken = (name, value, isGlobal) => {
debug(name, index, value)
t[name] = index
src[index] = value
safeSrc[index] = safe
re[index] = new RegExp(value, isGlobal ? 'g' : undefined)
safeRe[index] = new RegExp(safe, isGlobal ? 'g' : undefined)
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/increments.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = [
['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-alpha.10.beta'],
['1.2.3-alpha.10.beta', 'prerelease', '1.2.3-alpha.11.beta'],
['1.2.3-alpha.11.beta', 'prerelease', '1.2.3-alpha.12.beta'],
['1.0.0', 'prepatch', '1.0.1-alpha.1.1a.0', null, 'alpha.1.1a'],
['1.2.0', 'prepatch', '1.2.1-0'],
['1.2.0-1', 'prepatch', '1.2.1-0'],
['1.2.0', 'preminor', '1.3.0-0'],
Expand Down
11 changes: 7 additions & 4 deletions test/internal/re.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const { test } = require('tap')
const { src, re, safeRe } = require('../../internal/re')
const { src, re, safeRe, safeSrc } = require('../../internal/re')
const semver = require('../../')

test('has a list of src, re, and tokens', (t) => {
test('Semver itself has a list of src, re, and tokens', (t) => {
t.match(Object.assign({}, semver), {
src: Array,
re: Array,
src: Array,
tokens: Object,
})
re.forEach(r => t.match(r, RegExp, 'regexps are regexps'))
src.forEach(s => t.match(s, String, 'src is strings'))
safeRe.forEach(r => t.match(r, RegExp, 'safe regexps are regexps'))
src.forEach(s => t.match(s, String, 'src are strings'))
safeSrc.forEach(s => t.match(s, String, 'safe srcare strings'))
t.ok(Object.keys(semver.tokens).length, 'there are tokens')
for (const i in semver.tokens) {
t.match(semver.tokens[i], Number, 'tokens are numbers')
}
Expand Down

0 comments on commit af761c0

Please sign in to comment.