@trivago/prettier-plugin-sort-imports
Version:
A prettier plugins to sort imports in provided RegEx order
30 lines (29 loc) • 1.29 kB
JavaScript
import { addComments, removeComments } from '@babel/types';
import { clone, isEqual } from 'lodash-es';
/**
* Takes the original nodes before sorting and the final nodes after sorting.
* Adjusts the comments on the final nodes so that they match the comments as
* they were in the original nodes.
* @param nodes A list of nodes in the order as they were originally.
* @param finalNodes The same set of nodes, but in the final sorting order.
*/
export const adjustCommentsOnSortedNodes = (nodes, finalNodes) => {
// maintain a copy of the nodes to extract comments from
const finalNodesClone = finalNodes.map(clone);
const firstNodesComments = nodes[0].leadingComments;
// Remove all comments from sorted nodes
finalNodes.forEach(removeComments);
// insert comments other than the first comments
finalNodes.forEach((node, index) => {
if (isEqual(nodes[0].loc, node.loc))
return;
// remove comments location to not confuse print AST
firstNodesComments?.forEach((comment) => {
delete comment.loc;
});
addComments(node, 'leading', finalNodesClone[index].leadingComments || []);
});
if (firstNodesComments) {
addComments(finalNodes[0], 'leading', firstNodesComments);
}
};