fix: order pre-release by field count when one is a prefix of the other - #81
Open
spokodev wants to merge 1 commit into
Open
fix: order pre-release by field count when one is a prefix of the other#81spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
`1.0.0-alpha` compared equal to `1.0.0-alpha.0`, and `1.0.0-rc.1` equal to `1.0.0-rc.1.0`. Pre-release identifiers were compared with `compareSegments`, which pads the shorter list with `'0'` — correct for the numeric core (the `1.0` == `1.0.0` partial-version feature) but wrong inside a pre-release, where a missing field must make the shorter set lower. Per SemVer 2.0.0 section 11.4.4, when all preceding identifiers are equal the larger set of pre-release fields has the higher precedence. Compare the shared identifiers, then break ties on the field count.
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.
Problem
When one version's pre-release identifier set is a prefix of the other's, they compare as equal instead of the shorter one being lower:
Both inputs in each pair are valid (
validate()istrue), andnode-semverorders them as-1. This affectssort(),satisfies(), and any dedupe that relies on the ordering of adjacent pre-releases.Cause
src/compareVersions.tscompared pre-release identifiers withcompareSegments, which pads the shorter list with'0'(a[i] || '0'). That default is correct for the numeric core — it powers the documented partial-version feature (1.0==1.0.0) — but inside a pre-release a missing field must rank lower, regardless of the other side's value. With the'0'padding,['alpha']vs['alpha','0']becomes['alpha','0']vs['alpha','0']→ equal.Fix
SemVer 2.0.0 §11.4.4: "A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal." Compare the shared identifiers, then break ties on the field count:
Verification
test/compare.ts(#spec-item-9); fail before, pass after.node-semverover 500,000 valid version pairs (pre-releases of varying length, numeric + alphanumeric identifiers): 16 divergences before → 0 after.