fix: clone comments in cloneNode (#13178)
This commit is contained in:
parent
86d9cf5020
commit
ab06ccad49
@ -102,25 +102,18 @@ export default function cloneNode<T extends t.Node>(
|
||||
return newNode;
|
||||
}
|
||||
|
||||
function cloneCommentsWithoutLoc<T extends t.Comment>(
|
||||
comments: ReadonlyArray<T>,
|
||||
): T[] {
|
||||
return comments.map(
|
||||
({ type, value }) =>
|
||||
({
|
||||
type,
|
||||
value,
|
||||
loc: null,
|
||||
} as T),
|
||||
);
|
||||
}
|
||||
|
||||
function maybeCloneComments<T extends t.Comment>(
|
||||
comments: ReadonlyArray<T> | null,
|
||||
deep: boolean,
|
||||
withoutLoc: boolean,
|
||||
): ReadonlyArray<T> | null {
|
||||
return deep && withoutLoc && comments
|
||||
? cloneCommentsWithoutLoc(comments)
|
||||
: comments;
|
||||
if (!comments || !deep) {
|
||||
return comments;
|
||||
}
|
||||
return comments.map(({ type, value, loc }) => {
|
||||
if (withoutLoc) {
|
||||
return { type, value, loc: null } as T;
|
||||
}
|
||||
return { type, value, loc } as T;
|
||||
});
|
||||
}
|
||||
|
||||
@ -120,6 +120,9 @@ describe("cloneNode", function () {
|
||||
node.declarations[0].id.loc = {};
|
||||
|
||||
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ false);
|
||||
expect(cloned.declarations[0].id.leadingComments[0]).not.toBe(
|
||||
node.declarations[0].id.leadingComments[0],
|
||||
);
|
||||
expect(cloned.declarations[0].id.leadingComments[0].loc).toBe(
|
||||
node.declarations[0].id.leadingComments[0].loc,
|
||||
);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user