Skip to content

fix: snapshot array length before reviving elements - #391

Draft
Huzaifa-Asif wants to merge 1 commit into
json5:mainfrom
Huzaifa-Asif:fix/reviver-array-length
Draft

Huzaifa-Asif wants to merge 1 commit into
json5:mainfrom
Huzaifa-Asif:fix/reviver-array-length

Conversation

@Huzaifa-Asif

Copy link
Copy Markdown

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 appending 7 produces [6, 21] instead of the [6, 7] returned by JSON.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 lint and npm run test-types: pass.
  • npm run build-package and npm 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reviver visits elements added to an array during revival

1 participant