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

Avoid initial new line when using custom group separator #29

Merged
merged 1 commit into from Jun 9, 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
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) {
Copy link
Owner

Choose a reason for hiding this comment

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

This feels a little bit indirect. Could we check if finalNodes.length === 0? Would that accomplish the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be the same thing for this first checks, but then I would need to assert TS that the list is non empty for the next check (or add a useless runtime check)

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