fix(semver): Allow unsetting build metadata #3157
Merged
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.
The main issue is if you pass
""
as the build metadata to thesemver.increment
function it doesn't actually unset the build metadata as expected because of a falsy comparison of the parameter rather than a strictmetadata === undefined
.This will now alter the behavior of the increment function to have no changes when you specify
undefined
but to actually set the build metadata to[]
if you were to pass""
into the function.Additionally, there was what I think to be a "bug" discovered in the constructor which was making it very hard to test, which is a bug where the
this.raw
field was set before the call tothis.format()
, which ends up retaining a wrong / different version than if you call increment.Namely if you create a semver with build metadata the build metadata will be in the raw field. If you then increment it, then it will fall out of the raw field even if the build metadata is unchanged.
This happens because in the increment function, it is calling
this.format()
and then setting the raw field to the version but in the constructor it does the reverse. This means the raw will change based on if you've called increment or not.Also, the whole design of this class probably needs a major revamp to be immutable. Having mutations happen on functions such as
.format()
is really problematic when the object is this complex. If the std lib ever hits1.0.0
I would strongly recommend putting this chore onto the backlog.Before
After