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

Generic algorithm to create a shallow, memberwise clone of a node. #5726

Merged
merged 3 commits into from
Nov 20, 2015

Conversation

rbuckton
Copy link
Member

Eventually will be needed for transformations.

Supports #5595.

// 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)
Copy link
Member

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.

Copy link
Member Author

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).

Copy link
Member

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.

Copy link
Member Author

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;
        }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, sounds good.

@sandersn
Copy link
Member

Looks good to me.

rbuckton added a commit that referenced this pull request Nov 20, 2015
Generic algorithm to create a shallow, memberwise clone of a node.
@rbuckton rbuckton merged commit c94dcbd into master Nov 20, 2015
@rbuckton rbuckton deleted the cloneNode branch November 20, 2015 18:54
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants