-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Handle circular reference with toJS #1860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
List, | ||
Map, | ||
OrderedMap, | ||
Record as ImmutableRecord, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of aliasing the Record
name here, can you just make an explicit type in the unit test? (Does that value even need to be explicitly typed?)
expect(() => simplerTest.toJS()).toThrow( | ||
'Cannot convert circular structure to JS' | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior surprises me, I would have expected a return of [obj]
} | ||
|
||
if (circularStack.has(value)) { | ||
throw new TypeError('Cannot convert circular structure to JS'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of throwing here, how about returning circularStack.get(value)
to recreate the cycle?
} | ||
|
||
function toJSWithCircularCheck(circularStack, value) { | ||
checkCircular(circularStack, value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can inline this function since it's only used here
IMHO the original mistake was the My advice here is to attempt to resolve the cycle instead of simply throwing, since it's probably not uncommon to find this kind of cycle in regular JS objects stored within a collection, but perhaps we should consider a future breaking change which simply stops the recursive conversion when it finds a non-Immutable.js collection/value |
Fixes #1789