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

Fix cloneNode with typeAnnotation. #8997

Merged
merged 1 commit into from Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions packages/babel-types/src/clone/cloneNode.js
Expand Up @@ -37,6 +37,12 @@ export default function cloneNode<T: Object>(node: T, deep: boolean = true): T {
// Special-case identifiers since they are the most cloned nodes.
if (type === "Identifier") {
newNode.name = node.name;

if (has(node, "typeAnnotation")) {
newNode.typeAnnotation = deep
? cloneIfNodeOrArray(node.typeAnnotation, true)
: node.typeAnnotation;
}
} else if (!has(NODE_FIELDS, type)) {
throw new Error(`Unknown node type: "${type}"`);
} else {
Expand Down
16 changes: 16 additions & 0 deletions packages/babel-types/test/cloning.js
Expand Up @@ -56,4 +56,20 @@ describe("cloneNode", function() {
expect(node.object).toBe(cloned.object);
expect(node.property).toBe(cloned.property);
});

it("should preserve type annotations", function() {
const node = t.variableDeclaration("let", [
t.variableDeclarator({
...t.identifier("value"),
typeAnnotation: t.anyTypeAnnotation(),
}),
]);
const cloned = t.cloneNode(node, /* deep */ true);
expect(cloned.declarations[0].id.typeAnnotation).toEqual(
node.declarations[0].id.typeAnnotation,
);
expect(cloned.declarations[0].id.typeAnnotation).not.toBe(
node.declarations[0].id.typeAnnotation,
);
});
gregberge marked this conversation as resolved.
Show resolved Hide resolved
});