Skip to content
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

handling circular/shared structures in deep-clone #15366

Merged
merged 2 commits into from Feb 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/babel-core/src/transformation/util/clone-deep.ts
Expand Up @@ -5,12 +5,14 @@ function deepClone(value: any, cache: Map<any, any>): any {
let cloned: any;
if (Array.isArray(value)) {
cloned = new Array(value.length);
cache.set(value, cloned);
for (let i = 0; i < value.length; i++) {
cloned[i] =
typeof value[i] !== "object" ? value[i] : deepClone(value[i], cache);
}
} else {
cloned = {};
cache.set(value, cloned);
const keys = Object.keys(value);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
Expand All @@ -20,7 +22,6 @@ function deepClone(value: any, cache: Map<any, any>): any {
: deepClone(value[key], cache);
}
}
cache.set(value, cloned);
return cloned;
}
return value;
Expand Down