Skip to content

Commit

Permalink
Let '.' start non-numeric prerelease in loose mode
Browse files Browse the repository at this point in the history
Allowing 1.2.3.4 is confusing and weird.

However, 1.2.3.beta should be allowed.
  • Loading branch information
isaacs committed Apr 15, 2020
1 parent 226e6dc commit 174477e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/re.js
Expand Up @@ -58,8 +58,13 @@ createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NUMERICIDENTIFIERLOOSE]
createToken('PRERELEASE', `(?:-(${src[t.PRERELEASEIDENTIFIER]
}(?:\\.${src[t.PRERELEASEIDENTIFIER]})*))`)

createToken('PRERELEASELOOSE', `(?:-?(${src[t.PRERELEASEIDENTIFIERLOOSE]
}(?:\\.${src[t.PRERELEASEIDENTIFIERLOOSE]})*))`)
// loose mode prereleases can start with a dot, but only if they're not numeric
// 1.2.3.foo is allowed, 1.2.3.4 is not.
createToken('PRERELEASELOOSE', `(?:(?:-|\\.*(?=[a-zA-Z-]))(${
src[t.PRERELEASEIDENTIFIERLOOSE]
}(?:\\.${
src[t.PRERELEASEIDENTIFIERLOOSE]
})*))`)

// ## Build Metadata Identifier
// Any combination of digits, letters, or hyphens.
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/invalid-versions.js
Expand Up @@ -12,4 +12,7 @@ module.exports = [
[ /a regexp/, 'regexp is not a string'],
[ /1.2.3/, 'semver-ish regexp is not a string'],
[ {toString: () => '1.2.3'}, 'obj with a tostring is not a string'],
[ '1.2.3.foo', 'no dot-led prerelease tag, strict'],
[ '1.2.3.4', 'no dot-led numeric prerelease tag, strict'],
[ '1.2.3.4', 'no dot-led numeric prerelease tag, loose', { loose: true }],
]
4 changes: 4 additions & 0 deletions test/functions/valid.js
Expand Up @@ -15,5 +15,9 @@ t.test('validate a version into a SemVer object', t => {
t.equal(valid(s), '4.5.6', 'return the version if a SemVer obj')
t.equal(valid('4.2.0foo', true), '4.2.0-foo', 'looseness as a boolean')
t.equal(valid('4.2.0foo', { loose: true }), '4.2.0-foo', 'looseness as an option')
t.equal(valid('4.2.0.foo', { loose: true }), '4.2.0-foo', 'dot-led prerelease allowed in loose mode')
t.equal(valid('4.2.0.-foo', { loose: true }), '4.2.0--foo', 'dot-and-dash-led prerelease allowed in loose mode')
t.equal(valid('4.2.0--foo', { loose: true }), '4.2.0--foo', 'double-dash, loose')
t.equal(valid('4.2.0--foo'), '4.2.0--foo', 'double-dash, strict')
t.end()
})

0 comments on commit 174477e

Please sign in to comment.