Skip to content

Commit

Permalink
Avoid initial new line when using custom group separator (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Jun 9, 2022
1 parent 3b72a60 commit d7d2894
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/utils/get-sorted-nodes-by-import-order.ts
Expand Up @@ -70,8 +70,13 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => {
for (const group of importOrder) {
// If it's a custom separator, all we need to do is add a newline
if (isCustomGroupSeparator(group)) {
const lastNode = finalNodes[finalNodes.length - 1];
// Avoid empty new line if first group is empty
if (!lastNode) {
continue;
}
// Don't add multiple newlines
if (isLastNodeANewline(finalNodes)) {
if (isNodeANewline(lastNode)) {
continue;
}
finalNodes.push(newLineNode);
Expand Down Expand Up @@ -111,7 +116,6 @@ function isCustomGroupSeparator(pattern: string) {
return pattern.trim() === '';
}

function isLastNodeANewline(nodes: ImportOrLine[]) {
const lastNode = nodes[nodes.length - 1];
return lastNode?.type === 'ExpressionStatement';
function isNodeANewline(node: ImportOrLine) {
return node.type === 'ExpressionStatement';
}
Expand Up @@ -241,7 +241,6 @@ import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// I am top level comment
import abc from "@core/abc";
import otherthing from "@core/otherthing";
import something from "@server/something";
Expand Down

0 comments on commit d7d2894

Please sign in to comment.