fix(clone): support circular references - #105
Merged
ASafaeirad merged 1 commit intoSep 10, 2026
Merged
Conversation
clone recursed unconditionally, so a value referencing itself overflowed the call stack. Track visited values in a WeakMap and register each container before cloning its children, so a cycle resolves to the clone in progress and a repeated reference is cloned once and shared. cloneMap no longer round-trips through Object.fromEntries, which coerced non-string keys to strings.
xvyashar
reviewed
Sep 7, 2026
ASafaeirad
approved these changes
Sep 10, 2026
|
馃帀 This PR is included in version 4.24.1 馃帀 The release is available on: Your semantic-release bot 馃摝馃殌 |
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.
Closes #104
Problem
clonerecurses unconditionally, so a value that references itself overflows the call stack:Fix
Track visited values in a
WeakMapand register each container in it before cloning its children, so a cycle resolves to the clone already in progress. A value that appears more than once is now cloned once and shared by every reference in the output, which matchesstructuredClonesemantics.cloneMappreviously round-tripped throughObject.fromEntries/Object.entries, which coerced non-string keys to strings. It now iterates the map directly, sonew Map([[{ id: 1 }, 'foo']])keeps its object key.The
WeakMapis created per top-levelclonecall, so nothing is retained between calls.Tests
Added regression coverage for self-referencing objects, arrays, maps and sets, a mutually referencing pair, shared-reference identity, and non-string map keys.
Full suite passes (616 tests), plus
type-check,eslintandbuild.