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 8, 2018
1 parent 343f776 commit 63bc497
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/babel-types/src/clone/cloneNode.js
Expand Up @@ -66,6 +66,9 @@ export default function cloneNode<T: Object>(node: T, deep: boolean = true): T {
...node.extra,
};
}
if (has(node, "typeAnnotation")) {
newNode.typeAnnotation = node.typeAnnotation;
}

return newNode;
}
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).toBe(
node.declarations[0].id.typeAnnotation,
);
});
});

0 comments on commit 63bc497

Please sign in to comment.