fix: uneval grows sparse arrays by one slot when first hole is past index 0#160
Open
spokodev wants to merge 2 commits into
Open
fix: uneval grows sparse arrays by one slot when first hole is past index 0#160spokodev wants to merge 2 commits into
spokodev wants to merge 2 commits into
Conversation
When the first hole in an array literal was at an index greater than 0, uneval emitted two commas for it: one from `if (i > 0) result += ','` at the top of the iteration, and a second after `i -= 1` re-processed the same index. So `uneval([1, , 3])` produced `[1,,,3]` (length 4) instead of `[1,,3]` (length 3), and the round-tripped array gained a slot. The comma at the top of the iteration already represents the hole, so drop the `i -= 1` re-processing.
🦋 Changeset detectedLatest commit: fcddf82 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
What
unevalgrows a sparse array by one slot when its first hole is not at index 0:stringify/parseround-trips these correctly — onlyunevalis affected. ([, 'a'], where the first hole is at index 0, was already correct, which is why this slipped through.)Why
In the array-literal branch, a comma is written at the top of every iteration (
if (i > 0) result += ','). When the first hole is reached, the code sethas_holes = trueand then didi -= 1to "re-process this index as a hole" — but the re-processed iteration writes a second comma for the same slot, so each such array gains one element.Fix
The comma written at the top of the iteration already represents the hole, so the
i -= 1re-processing is unnecessary (and is what caused the extra comma). Removing it makes every case round-trip:[1, , 3][1,,,3](len 4)[1,,3](len 3)[1, , ][1,,,](len 3)[1,,](len 2)[1, , , 4][1,,,,4](len 5)[1,,,4](len 4)[, 'a', , 'b'][,"a",,"b"](unchanged)[,"a",,"b"]The
Object.assignpath for very sparse arrays returns earlier and is unaffected.Tests
Added a round-trip test for sparse arrays whose first hole is not at index 0 (red before the change, green after). Full
npm testpasses (659).