-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generic algorithm to create a shallow, memberwise clone of a node. #5726
Conversation
// the original node. We also need to exclude specific properties and only include own- | ||
// properties (to skip members already defined on the shared prototype). | ||
const clone = location !== undefined | ||
? <T>createNode(node.kind, location.pos, location.end) |
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.
kind is copied again in the for loop, right? I just want to make sure I'm reading the code correctly.
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.
Good catch. I originally wrote the cloneNode
function before @vladima modified the Node constructor, when kind
was set on the prototype. I can probably drop isExcludedPropertyForClone
in favor of clone.hasOwnProperty(key)
.
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 will copy over flags
and parent
now, won't it? Which is bad unless they are provided by the caller.
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.
No, flags and parent are set in the Node constructor. See line 808 of core.ts:
function Node(kind: SyntaxKind, pos: number, end: number) {
this.kind = kind;
this.pos = pos;
this.end = end;
this.flags = NodeFlags.None;
this.parent = undefined;
}
And line 191 of services.ts:
constructor(kind: SyntaxKind, pos: number, end: number) {
this.kind = kind;
this.pos = pos;
this.end = end;
this.flags = NodeFlags.None;
this.parent = undefined;
}
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.
All right, sounds good.
Looks good to me. |
Generic algorithm to create a shallow, memberwise clone of a node.
Eventually will be needed for transformations.
Supports #5595.