diff --git a/classes/semver.js b/classes/semver.js index 97049a40..6fbc062b 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -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') @@ -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}`) } diff --git a/internal/re.js b/internal/re.js index fd8920e7..2a956ba0 100644 --- a/internal/re.js +++ b/internal/re.js @@ -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 @@ -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) } diff --git a/test/fixtures/increments.js b/test/fixtures/increments.js index a9b06358..f0839df5 100644 --- a/test/fixtures/increments.js +++ b/test/fixtures/increments.js @@ -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'], diff --git a/test/internal/re.js b/test/internal/re.js index 2851b325..1db0586a 100644 --- a/test/internal/re.js +++ b/test/internal/re.js @@ -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') }