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;
|
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>(
|
function maybeCloneComments<T extends t.Comment>(
|
||||||
comments: ReadonlyArray<T> | null,
|
comments: ReadonlyArray<T> | null,
|
||||||
deep: boolean,
|
deep: boolean,
|
||||||
withoutLoc: boolean,
|
withoutLoc: boolean,
|
||||||
): ReadonlyArray<T> | null {
|
): ReadonlyArray<T> | null {
|
||||||
return deep && withoutLoc && comments
|
if (!comments || !deep) {
|
||||||
? cloneCommentsWithoutLoc(comments)
|
return comments;
|
||||||
: 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 = {};
|
node.declarations[0].id.loc = {};
|
||||||
|
|
||||||
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ false);
|
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(
|
expect(cloned.declarations[0].id.leadingComments[0].loc).toBe(
|
||||||
node.declarations[0].id.leadingComments[0].loc,
|
node.declarations[0].id.leadingComments[0].loc,
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user