Repository navigation
fix: snapshot array length before reviving elements - #391
Draft
Huzaifa-Asif wants to merge 1 commit into
Draft
Huzaifa-Asif wants to merge 1 commit into
Huzaifa-Asif wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Fixes #390.
A reviver that appends an element to its array currently causes JSON5 to revive that new element too. For example, tripling the numbers in
[2]while appending7produces[6, 21]instead of the[6, 7]returned byJSON.parse. Shrinking the array also prevents callbacks for some original indexes.Capture each array's length before visiting its elements, as in ECMAScript's InternalizeJSONProperty. Appended indexes are skipped, while the original indexes are still visited after truncation or deletion. Nested arrays capture their own bounds when their traversal begins.
Five tests compare both callback order and results with native
JSON.parse: appending, truncation with restoration, deletion, sparse expansion, and nested arrays. Four fail before the fix; the deletion test preserves existing behavior.Validation on macOS arm64, Node.js 24.18.1:
npm test: 193 passing, 100% line/branch/function coverage.npm run lintandnpm run test-types: pass.npm run build-packageandnpm pack --ignore-scripts: pass (pack used an isolated temporary npm cache).git diff --check: pass.Related open #386 and #361 add reviver source context and touch this function, but retain the live length bound. #362 is a broader parser rewrite that incidentally skips appended elements, but traverses existing keys in reverse order rather than visiting the original indexes forward, including holes. This patch targets the current implementation and may need rebasing if that work lands first.
Prepared with AI assistance; independently reviewed before submission.