fix: preserve a trailing hole in sparse arrays#46
Open
spokodev wants to merge 1 commit into
Open
Conversation
`Array.prototype.map` keeps holes, but joining the results drops a trailing one: `[1, ,]` (length 2) was serialized as `[1,]`, which is an array of length 1. So a sparse array ending in a hole came back shorter after `eval`. Append a separator when the last element is a hole to keep the length.
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
A sparse array whose last element is a hole loses that hole on a round-trip, so the array comes back shorter:
Leading and interior holes are fine (
[,1],[1,,3]); only a trailing hole is dropped.Root cause
arrayToStringmaps the values (Array.prototype.mappreserves holes) and joins them:joinrenders a hole as an empty string between separators, which works for leading and interior holes. But a trailing hole produces a single trailing comma — and[1,]is a length-1 array literal, so the hole is lost.Fix
When the last element is a hole, append one more separator so the array literal keeps its length (
[1,,]has length 2):Test plan
main, the others already passed).