fix: split dotted prerelease identifiers in inc()#883
Open
MerlijnW70 wants to merge 1 commit into
Open
Conversation
SemVer.prototype.inc('prerelease', identifier) stored the identifier as a
single element of the prerelease array, even when it contained dots, so a
dotted identifier such as 'x.y' was stored as ['x.y', 0] instead of
['x', 'y', 0]. The version string was still formatted correctly, but the
in-memory prerelease array was malformed, so comparePre() compared a
dotted element against a plain one and mis-ordered versions:
const a = new SemVer('1.2.3-alpha').inc('prerelease', 'x.y')
a.compare(new SemVer('1.2.3-x.y.1')) // 1 (x.y.0 > x.y.1?!)
semver.compare('1.2.3-x.y.0', '1.2.3-x.y.1') // -1
The same version compared as an object versus as its string produced
opposite results.
Split the identifier on '.' and numberify numeric ids, matching how the
SemVer constructor parses prerelease identifiers, so comparePre() sees
exactly one identifier per array element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SemVer.prototype.inc('prerelease', identifier)stores the identifier as a single element of theprereleasearray even when it contains dots. A dotted identifier such as'x.y'is stored as['x.y', 0]instead of['x', 'y', 0]. The formatted version string is correct, but the in-memoryprereleasearray is malformed, socomparePre()compares a dotted element against a plain identifier and mis-orders versions.Reproduction
The same version compared as a
SemVerobject versus as its string yields opposite results. BecausecomparePre()walks the array element-by-element, the malformed'x.y'element is compared withcompareIdentifiers('x.y', 'x')and wins lexically, so1.2.3-x.y.0is reported as greater than1.2.3-x.y.1.Root cause
In
classes/semver.js, theprecase ofinc()assigns the raw identifier straight into the array:The surrounding code already knows identifiers can be dotted — a few lines down it uses
identifier.split('.').length— but the assignment itself never splits. Every other consumer (comparePre, and the constructor) assumes one dot-free identifier per array element.Fix
Split the identifier on
.and numberify numeric ids, exactly as theSemVerconstructor does when parsing prerelease identifiers, socomparePre()sees one identifier per element:After the fix,
inc('1.2.3','prerelease','1.2').prereleasedeep-equalsparse('1.2.4-1.2.0').prerelease([1, 2, 0]), and object comparison agrees with string comparison. Non-dotted identifiers ('beta','dev', …) are unaffected.Tests
test/functions/inc.jsasserting theprereleasearray shape and that object comparison matches string comparison (the fixture suite only checks the version string, which is why this went unnoticed).test/fixtures/increments.jsfor dotted identifiers, including a numeric id and a numeric id ≥ 2^53 (kept as a string, matching the constructor).Full suite passes with 100% coverage and lint clean.