@babel/types#addComments TypeScript Examples
The following examples show how to use
@babel/types#addComments.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: get-sorted-nodes.ts From prettier-plugin-sort-imports with Apache License 2.0 | 4 votes |
getSortedNodes: GetSortedNodes = (nodes, options) => {
naturalSort.insensitive = options.importOrderCaseInsensitive;
let { importOrder } = options;
const {
importOrderSeparation,
importOrderSortSpecifiers,
importOrderGroupNamespaceSpecifiers,
} = options;
const originalNodes = nodes.map(clone);
const finalNodes: ImportOrLine[] = [];
if (!importOrder.includes(THIRD_PARTY_MODULES_SPECIAL_WORD)) {
importOrder = [THIRD_PARTY_MODULES_SPECIAL_WORD, ...importOrder];
}
const importOrderGroups = importOrder.reduce<ImportGroups>(
(groups, regexp) => ({
...groups,
[regexp]: [],
}),
{},
);
const importOrderWithOutThirdPartyPlaceholder = importOrder.filter(
(group) => group !== THIRD_PARTY_MODULES_SPECIAL_WORD,
);
for (const node of originalNodes) {
const matchedGroup = getImportNodesMatchedGroup(
node,
importOrderWithOutThirdPartyPlaceholder,
);
importOrderGroups[matchedGroup].push(node);
}
for (const group of importOrder) {
const groupNodes = importOrderGroups[group];
if (groupNodes.length === 0) continue;
const sortedInsideGroup = getSortedNodesGroup(groupNodes, {
importOrderGroupNamespaceSpecifiers,
});
// Sort the import specifiers
if (importOrderSortSpecifiers) {
sortedInsideGroup.forEach((node) =>
getSortedImportSpecifiers(node),
);
}
finalNodes.push(...sortedInsideGroup);
if (importOrderSeparation) {
finalNodes.push(newLineNode);
}
}
if (finalNodes.length > 0 && !importOrderSeparation) {
// a newline after all imports
finalNodes.push(newLineNode);
}
// 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;
addComments(
node,
'leading',
finalNodesClone[index].leadingComments || [],
);
});
if (firstNodesComments) {
addComments(finalNodes[0], 'leading', firstNodesComments);
}
return finalNodes;
}