Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create multiple copies of comments when cloning nodes #14551

Merged
merged 3 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 48 additions & 11 deletions packages/babel-types/src/clone/cloneNode.ts
Expand Up @@ -5,19 +5,19 @@ import { isFile, isIdentifier } from "../validators/generated";
const has = Function.call.bind(Object.prototype.hasOwnProperty);

// This function will never be called for comments, only for real nodes.
function cloneIfNode(obj, deep, withoutLoc) {
function cloneIfNode(obj, deep, withoutLoc, commentsCache) {
if (obj && typeof obj.type === "string") {
return cloneNode(obj, deep, withoutLoc);
return cloneNodeInternal(obj, deep, withoutLoc, commentsCache);
}

return obj;
}

function cloneIfNodeOrArray(obj, deep, withoutLoc) {
function cloneIfNodeOrArray(obj, deep, withoutLoc, commentsCache) {
if (Array.isArray(obj)) {
return obj.map(node => cloneIfNode(node, deep, withoutLoc));
return obj.map(node => cloneIfNode(node, deep, withoutLoc, commentsCache));
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
}
return cloneIfNode(obj, deep, withoutLoc);
return cloneIfNode(obj, deep, withoutLoc, commentsCache);
}

/**
Expand All @@ -29,6 +29,15 @@ export default function cloneNode<T extends t.Node>(
node: T,
deep: boolean = true,
withoutLoc: boolean = false,
): T {
return cloneNodeInternal(node, deep, withoutLoc, new Map());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be a WeakMap? If a comment node has been removed from AST, I don't think the cache should prevent it from being GC'ed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Map will be destroyed after the function returns.

Because a comment in ast will be referenced by the two expressions before and after respectively, and the generator judges whether it is the same by comparing the comment object instance.
After deep cloning, it will become two different comment objects, resulting in repeated generation.

Use comment caching to avoid duplicate comments that are deeply cloned together.
If it is to be more perfect, it needs to be refactored in the future.

}

function cloneNodeInternal<T extends t.Node>(
node: T,
deep: boolean = true,
withoutLoc: boolean = false,
commentsCache: Map<t.Comment, t.Comment>,
): T {
if (!node) return node;

Expand All @@ -45,7 +54,12 @@ export default function cloneNode<T extends t.Node>(

if (has(node, "typeAnnotation")) {
newNode.typeAnnotation = deep
? cloneIfNodeOrArray(node.typeAnnotation, true, withoutLoc)
? cloneIfNodeOrArray(
node.typeAnnotation,
true,
withoutLoc,
commentsCache,
)
: node.typeAnnotation;
}
} else if (!has(NODE_FIELDS, type)) {
Expand All @@ -56,8 +70,18 @@ export default function cloneNode<T extends t.Node>(
if (deep) {
newNode[field] =
isFile(node) && field === "comments"
? maybeCloneComments(node.comments, deep, withoutLoc)
: cloneIfNodeOrArray(node[field], true, withoutLoc);
? maybeCloneComments(
node.comments,
deep,
withoutLoc,
commentsCache,
)
: cloneIfNodeOrArray(
node[field],
true,
withoutLoc,
commentsCache,
);
} else {
newNode[field] = node[field];
}
Expand All @@ -77,20 +101,23 @@ export default function cloneNode<T extends t.Node>(
node.leadingComments,
deep,
withoutLoc,
commentsCache,
);
}
if (has(node, "innerComments")) {
newNode.innerComments = maybeCloneComments(
node.innerComments,
deep,
withoutLoc,
commentsCache,
);
}
if (has(node, "trailingComments")) {
newNode.trailingComments = maybeCloneComments(
node.trailingComments,
deep,
withoutLoc,
commentsCache,
);
}
if (has(node, "extra")) {
Expand All @@ -106,14 +133,24 @@ function maybeCloneComments<T extends t.Comment>(
comments: ReadonlyArray<T> | null,
deep: boolean,
withoutLoc: boolean,
commentsCache: Map<T, T>,
): ReadonlyArray<T> | null {
if (!comments || !deep) {
return comments;
}
return comments.map(({ type, value, loc }) => {
return comments.map(comment => {
const cache = commentsCache.get(comment);
if (cache) return cache;

const { type, value, loc } = comment;

const ret = { type, value, loc } as T;
if (withoutLoc) {
return { type, value, loc: null } as T;
ret.loc = null;
}
return { type, value, loc } as T;

commentsCache.set(comment, ret);

return ret;
});
}
21 changes: 21 additions & 0 deletions packages/babel-types/test/cloning.js
@@ -1,5 +1,6 @@
import * as t from "../lib/index.js";
import { parse } from "@babel/parser";
import { CodeGenerator } from "@babel/generator";

describe("cloneNode", function () {
it("should handle undefined", function () {
Expand Down Expand Up @@ -151,4 +152,24 @@ describe("cloneNode", function () {
expect(cloned.declarations[0].id.innerComments[0].loc).toBe(null);
expect(cloned.declarations[0].id.trailingComments[0].loc).toBe(null);
});

it("should generate same code after deep cloning", function () {
let code = `//test1
/*test2*/var/*test3*/ a = 1/*test4*/;//test5
//test6
var b;
`;
code = new CodeGenerator(parse(code), { retainLines: true }).generate()
.code;

const ast = t.cloneNode(
parse(code),
/* deep */ true,
/* withoutLoc */ false,
);
const newCode = new CodeGenerator(ast, { retainLines: true }).generate()
.code;

expect(newCode).toBe(code);
});
});