Skip to content

Commit

Permalink
Fix cloneNode with typeAnnotation.
Browse files Browse the repository at this point in the history
Fixes #8996
  • Loading branch information
gregberge committed Nov 9, 2018
1 parent 343f776 commit cb8403c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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
13 changes: 13 additions & 0 deletions packages/babel-types/test/cloning.js
Expand Up @@ -56,4 +56,17 @@ 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,
);
});
});

0 comments on commit cb8403c

Please sign in to comment.