Skip to content

Commit

Permalink
fix: Group index file imports with relative modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Drago committed Jun 29, 2021
1 parent 248de58 commit 3efc41a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (styleApi: IStyleAPI): IStyleItem[] {
isRelativeModule,
moduleName,
not,
or,
unicode,
} = styleApi;

Expand All @@ -25,6 +26,14 @@ export default function (styleApi: IStyleAPI): IStyleItem[] {
);
};

/**
* Checks whether the import is only dots and slashes. In this case, in Node,
* we would be importing an index file, which belongs in relative imports.
*/
const isIndexImport: IMatcherFunction = (imported) => {
return /^[\.\/]+$/.test(imported.moduleName);
};

return [
// import 'foo';
{
Expand All @@ -44,23 +53,27 @@ export default function (styleApi: IStyleAPI): IStyleItem[] {

// import React, { useEffect } from 'react';
{
match: isInstalledModule(__filename),
match: and(isInstalledModule(__filename), not(isIndexImport)),
sort: moduleName(unicode),
sortNamedMembers: alias(unicode),
},
{ separator: true },

// import { isPhoneValid } from 'utils/isPhoneValid'
{
match: and(isAbsoluteModule, not(isInstalledModule(__filename))),
match: and(
isAbsoluteModule,
not(isInstalledModule(__filename)),
not(isIndexImport),
),
sort: moduleName(unicode),
sortNamedMembers: alias(unicode),
},
{ separator: true },

// import { Heading } from "./styles"
{
match: isRelativeModule,
match: or(isRelativeModule, isIndexImport),
sort: [dotSegmentCount, moduleName(unicode)],
sortNamedMembers: alias(unicode),
},
Expand Down

0 comments on commit 3efc41a

Please sign in to comment.