Skip to content

Commit

Permalink
fix: cloneNode(deep, withoutLoc) handles absent comments
Browse files Browse the repository at this point in the history
This fragment (maybe all fragments?) throw during cloning, as
'comments' is unset here. Handle it being unset, by returning
undefined.
  • Loading branch information
FauxFaux committed Jan 10, 2021
1 parent fce3e71 commit a95f58f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-types/src/clone/cloneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function cloneNode<T extends t.Node>(
}

function cloneCommentsWithoutLoc<T extends t.Comment>(comments: T[]): T[] {
return comments.map(
return comments?.map(
({ type, value }) =>
({
type,
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-types/test/cloning.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ describe("cloneNode", function () {
expect(t.isNodesEquivalent(node, cloned)).toBe(true);
});

it("should handle deep cloning without loc of fragments", function () {
const program = "foo();";
const node = parse(program);
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ true);
expect(node).not.toBe(cloned);
expect(t.isNodesEquivalent(node, cloned)).toBe(true);
});

it("should handle missing array element", function () {
const node = parse("[,0]");
const cloned = t.cloneNode(node);
Expand Down

0 comments on commit a95f58f

Please sign in to comment.