Skip to content

Commit

Permalink
Fix perf
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 4, 2021
1 parent 0d1c954 commit 7e0ed3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
55 changes: 35 additions & 20 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -27,24 +27,21 @@ export type CommentWhitespace = {
containingNode: Node | null,
};

type Comments = Array<Comment>;

/**
* Merge comments with node's ${type}Comments or assign comments to be
* ${type}Comments. New comments will be placed before old comments
* Merge comments with node's leadingComments or assign comments to be
* leadingComments. New comments will be placed before old comments
* because the commentStack is enumerated reversely.
*
* @param {"leading" | "inner" | "trailing"} position
* @param {Node} node
* @param {Array<Comment>} comments
*/
function setComments(
position: "leading" | "inner" | "trailing",
node: Node,
comments: Array<Comment>,
) {
if (node[`${position}Comments`] === undefined) {
node[`${position}Comments`] = comments;
function setLeadingComments(node: Node, comments: Comments) {
if (node.leadingComments === undefined) {
node.leadingComments = comments;
} else {
node[`${position}Comments`].unshift(...comments);
node.leadingComments.unshift(...comments);
}
}

Expand All @@ -56,9 +53,27 @@ function setComments(
* @param {Node} node
* @param {Array<Comment>} comments
*/
export function setInnerComments(node: Node, comments: Array<Comment> | void) {
if (comments) {
setComments("inner", node, comments);
export function setInnerComments(node: Node, comments: Comments) {
if (node.innerComments === undefined) {
node.innerComments = comments;
} else {
node.innerComments.unshift(...comments);
}
}

/**
* Merge comments with node's trailingComments or assign comments to be
* trailingComments. New comments will be placed before old comments
* because the commentStack is enumerated reversely.
*
* @param {Node} node
* @param {Array<Comment>} comments
*/
function setTrailingComments(node: Node, comments: Comments) {
if (node.trailingComments === undefined) {
node.trailingComments = comments;
} else {
node.trailingComments.unshift(...comments);
}
}

Expand All @@ -82,9 +97,9 @@ function adjustInnerComments(
lastElement = elements[--i];
}
if (lastElement === null || lastElement.start > commentWS.start) {
setComments("inner", node, commentWS.comments);
setInnerComments(node, commentWS.comments);
} else {
setComments("trailing", lastElement, commentWS.comments);
setTrailingComments(lastElement, commentWS.comments);
}
}

Expand Down Expand Up @@ -150,10 +165,10 @@ export default class CommentsParser extends BaseParser {
const { comments } = commentWS;
if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
if (commentWS.leadingNode !== null) {
setComments("trailing", commentWS.leadingNode, comments);
setTrailingComments(commentWS.leadingNode, comments);
}
if (commentWS.trailingNode !== null) {
setComments("leading", commentWS.trailingNode, comments);
setLeadingComments(commentWS.trailingNode, comments);
}
} else {
/*:: invariant(commentWS.containingNode !== null) */
Expand Down Expand Up @@ -190,11 +205,11 @@ export default class CommentsParser extends BaseParser {
adjustInnerComments(node, node.specifiers, commentWS);
break;
default: {
setComments("inner", node, comments);
setInnerComments(node, comments);
}
}
} else {
setComments("inner", node, comments);
setInnerComments(node, comments);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1023,9 +1023,13 @@ export default class ExpressionParser extends LValParser {
call.extra?.trailingComma,
);
// mark inner comments of `async()` as inner comments of `async () =>`
setInnerComments(node, call.innerComments);
if (call.innerComments) {
setInnerComments(node, call.innerComments);
}
// mark trailing comments of `async` to be inner comments
setInnerComments(node, call.callee.trailingComments);
if (call.callee.trailingComments) {
setInnerComments(node, call.callee.trailingComments);
}
return node;
}

Expand Down

0 comments on commit 7e0ed3d

Please sign in to comment.